From 6bf5c34cf20002491423ca69dc3b0d037afaaefd Mon Sep 17 00:00:00 2001 From: kazu Date: Sun, 7 Sep 2008 08:57:53 +0000 Subject: [PATCH] * configure.in: Mac OS X's crypt(2) is broken with invalid salt. [ruby-dev:35899] * string.c (rb_str_crypt): ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@19213 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 6 ++++++ configure.in | 20 ++++++++++++++++++++ string.c | 15 +++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/ChangeLog b/ChangeLog index b570c145f5..54384f75c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +Sun Sep 7 17:54:45 2008 Kazuhiro NISHIYAMA + + * configure.in: Mac OS X's crypt(2) is broken with invalid salt. + [ruby-dev:35899] + * string.c (rb_str_crypt): ditto. + Sun Sep 7 17:29:49 2008 Tanaka Akira * tool/transcode-tblgen.rb: o4 is usable only if the first byte is diff --git a/configure.in b/configure.in index 26c2e00aca..3e1130d050 100644 --- a/configure.in +++ b/configure.in @@ -523,6 +523,26 @@ darwin*) LIBS="-lobjc $LIBS" AC_DEFINE(BROKEN_SETREUID, 1) AC_DEFINE(BROKEN_SETREGID, 1) ]) + ac_cv_lib_crypt_crypt=no + AC_CACHE_CHECK(for broken crypt with 8bit chars, rb_cv_broken_crypt, + [AC_TRY_RUN([ +#include +#include +#include +int +main() +{ + char buf[256]; + strcpy(buf, crypt("", "\xE0\xA0")); + return strcmp(buf, crypt("", "\xE0\xA0")); +} +], + rb_cv_broken_crypt=no, + rb_cv_broken_crypt=yes, + rb_cv_broken_crypt=yes)]) + if test "$rb_cv_broken_crypt" = yes; then + AC_DEFINE(BROKEN_CRYPT, 1) + fi ;; hpux*) LIBS="-lm $LIBS" ac_cv_c_inline=no;; diff --git a/string.c b/string.c index 080d6c9f3f..60908ef233 100644 --- a/string.c +++ b/string.c @@ -5862,6 +5862,10 @@ rb_str_crypt(VALUE str, VALUE salt) extern char *crypt(const char *, const char *); VALUE result; const char *s; +#ifdef BROKEN_CRYPT + VALUE salt_8bit_clean; + rb_encoding *enc; +#endif StringValue(salt); if (RSTRING_LEN(salt) < 2) @@ -5869,7 +5873,18 @@ rb_str_crypt(VALUE str, VALUE salt) if (RSTRING_PTR(str)) s = RSTRING_PTR(str); else s = ""; +#ifdef BROKEN_CRYPT + salt_8bit_clean = rb_str_dup(salt); + enc = rb_ascii8bit_encoding(); + str_modifiable(salt_8bit_clean); + rb_enc_associate(salt_8bit_clean, enc); + salt_8bit_clean = rb_str_tr(salt_8bit_clean, + rb_enc_str_new("\x80-\xFF", 3, enc), + rb_usascii_str_new("\x00-\x7F", 3)); + result = rb_str_new2(crypt(s, RSTRING_PTR(salt_8bit_clean))); +#else result = rb_str_new2(crypt(s, RSTRING_PTR(salt))); +#endif OBJ_INFECT(result, str); OBJ_INFECT(result, salt); return result;