Repository navigation
Conversation
`BCrypt::Engine.generate_salt` aborts with `free(): invalid pointer` when Ruby is not linked to libcrypt and is built with `RUBY_DEBUG` from ruby/ruby@91619f0230 or later. `ext/mri/wrapper.c` includes `ruby/util.h`, which redefines `strdup` as `ruby_strdup`, so the bundled `crypt_gensalt_ra` allocates its result with `ruby_xmalloc`, while `bc_salt` releases it with `free`. Since ruby/ruby@91619f0230, `ruby_xmalloc` in a `RUBY_DEBUG` build returns a pointer after its own header, which `free` rejects. When Ruby is linked to libcrypt, `crypt_gensalt_ra` is resolved to the one in libxcrypt, which uses `malloc`, so `free` is correct there. This commit allocates the copy with `malloc` and `memcpy` instead of `strdup`, and stops including `ruby/util.h`. Not calling `strdup` also keeps avoiding the problem fixed by f48c58f. Fix bcrypt-ruby#305 Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This pull request fixes
BCrypt::Engine.generate_saltaborting withfree(): invalid pointerwhen Ruby is not linked to libcrypt and is built withRUBY_DEBUGfrom ruby/ruby@91619f0230 or later.ext/mri/wrapper.cincludesruby/util.h, which redefinesstrdupasruby_strdup, so the bundledcrypt_gensalt_raallocates its result withruby_xmalloc, whilebc_saltreleases it withfree. When Ruby is linked to libcrypt,crypt_gensalt_rais resolved to the one in libxcrypt, which usesmalloc, and the bundled one is not used.It can be reproduced with a
rubylang/ruby:master-debugimage built before ruby/docker-images#195 .Fix #305