[前][次][番号順一覧][スレッド一覧]

ruby-changes:48633

From: rhe <ko1@a...>
Date: Mon, 13 Nov 2017 00:55:09 +0900 (JST)
Subject: [ruby-changes:48633] rhe:r60748 (trunk): string.c: fix memory leak in String#crypt

rhe	2017-11-13 00:55:04 +0900 (Mon, 13 Nov 2017)

  New Revision: 60748

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60748

  Log:
    string.c: fix memory leak in String#crypt
    
    Use ALLOCV to allocate struct crypt_data for slightly cleaner and less
    error-prone code. It is currently possible it leaks when an invalid
    argument is passed to String#crypt or rb_str_new_cstr() fails to
    allocate memory.
    
    SIZEOF_CRYPT_DATA macro in missing/crypt.h is removed since it is not
    used any longer.

  Modified files:
    trunk/missing/crypt.h
    trunk/string.c
Index: missing/crypt.h
===================================================================
--- missing/crypt.h	(revision 60747)
+++ missing/crypt.h	(revision 60748)
@@ -237,8 +237,6 @@ struct crypt_data { https://github.com/ruby/ruby/blob/trunk/missing/crypt.h#L237
 	char	cryptresult[1+4+4+11+1];	/* encrypted result */
 };
 
-#define SIZEOF_CRYPT_DATA (KS_SIZE*8+(1+4+4+11+1))
-
 char *crypt(const char *key, const char *setting);
 void setkey(const char *key);
 void encrypt(char *block, int flag);
Index: string.c
===================================================================
--- string.c	(revision 60747)
+++ string.c	(revision 60748)
@@ -8839,14 +8839,9 @@ rb_str_oct(VALUE str) https://github.com/ruby/ruby/blob/trunk/string.c#L8839
 static VALUE
 rb_str_crypt(VALUE str, VALUE salt)
 {
-#undef LARGE_CRYPT_DATA
 #ifdef HAVE_CRYPT_R
-# if defined SIZEOF_CRYPT_DATA && SIZEOF_CRYPT_DATA <= 256
-    struct crypt_data cdata, *const data = &cdata;
-# else
-#   define LARGE_CRYPT_DATA
-    struct crypt_data *data = ALLOC(struct crypt_data);
-# endif
+    VALUE databuf;
+    struct crypt_data *data;
 #else
     extern char *crypt(const char *, const char *);
 #endif
@@ -8877,6 +8872,7 @@ rb_str_crypt(VALUE str, VALUE salt) https://github.com/ruby/ruby/blob/trunk/string.c#L8872
     }
 #endif
 #ifdef HAVE_CRYPT_R
+    data = ALLOCV(databuf, sizeof(struct crypt_data));
 # ifdef HAVE_STRUCT_CRYPT_DATA_INITIALIZED
     data->initialized = 0;
 # endif
@@ -8885,17 +8881,15 @@ rb_str_crypt(VALUE str, VALUE salt) https://github.com/ruby/ruby/blob/trunk/string.c#L8881
     res = crypt(s, saltp);
 #endif
     if (!res) {
-#ifdef LARGE_CRYPT_DATA
+#ifdef HAVE_CRYPT_R
 	int err = errno;
-	xfree(data);
+	ALLOCV_END(databuf);
 	errno = err;
 #endif
 	rb_sys_fail("crypt");
     }
     result = rb_str_new_cstr(res);
-#ifdef LARGE_CRYPT_DATA
-    xfree(data);
-#endif
+    ALLOCV_END(databuf);
     FL_SET_RAW(result, OBJ_TAINTED_RAW(str) | OBJ_TAINTED_RAW(salt));
     return result;
 }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]