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

ruby-changes:33208

From: nobu <ko1@a...>
Date: Sat, 8 Mar 2014 12:55:05 +0900 (JST)
Subject: [ruby-changes:33208] nobu:r45287 (trunk): process.c: tmp buffer instead of alloca

nobu	2014-03-08 12:55:01 +0900 (Sat, 08 Mar 2014)

  New Revision: 45287

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=45287

  Log:
    process.c: tmp buffer instead of alloca
    
    * process.c (OBJ2UID1, OBJ2GID1): separate from OBJ2UID and
      OBJ2GID respectively, need given buffers.
    
    * process.c (OBJ2UID, OBJ2GID): no longer need PREPARE_GETPWNAM
      and PREPARE_GETGRNAM.
    
    * process.c (obj2uid, obj2gid): use tmp buffer instead of alloca
      to get rid of potential stack overflow.

  Modified files:
    trunk/process.c
Index: process.c
===================================================================
--- process.c	(revision 45286)
+++ process.c	(revision 45287)
@@ -154,17 +154,30 @@ static void check_gid_switch(void); https://github.com/ruby/ruby/blob/trunk/process.c#L154
 # endif
 # ifdef USE_GETPWNAM_R
 #   define PREPARE_GETPWNAM \
-    long getpw_buf_len = sysconf(_SC_GETPW_R_SIZE_MAX); \
-    char *getpw_buf = ALLOCA_N(char, (getpw_buf_len < 0 ? (getpw_buf_len = 4096) : getpw_buf_len));
-#   define OBJ2UID(id) obj2uid((id), getpw_buf, getpw_buf_len)
-static rb_uid_t obj2uid(VALUE id, char *getpw_buf, size_t getpw_buf_len);
+    VALUE getpw_buf = 0
+#   define FINISH_GETPWNAM \
+    ALLOCV_END(getpw_buf)
+#   define OBJ2UID1(id) obj2uid((id), &getpw_buf)
+#   define OBJ2UID(id) obj2uid0(id)
+static rb_uid_t obj2uid(VALUE id, VALUE *getpw_buf);
+static inline rb_uid_t
+obj2uid0(VALUE id)
+{
+    rb_uid_t uid;
+    PREPARE_GETPWNAM;
+    uid = OBJ2UID1(id);
+    FINISH_GETPWNAM;
+    return uid;
+}
 # else
 #   define PREPARE_GETPWNAM	/* do nothing */
+#   define FINISH_GETPWNAM	/* do nothing */
 #   define OBJ2UID(id) obj2uid((id))
 static rb_uid_t obj2uid(VALUE id);
 # endif
 #else
 # define PREPARE_GETPWNAM	/* do nothing */
+# define FINISH_GETPWNAM	/* do nothing */
 # define OBJ2UID(id) NUM2UIDT(id)
 # ifdef p_uid_from_name
 #   undef p_uid_from_name
@@ -178,17 +191,31 @@ static rb_uid_t obj2uid(VALUE id); https://github.com/ruby/ruby/blob/trunk/process.c#L191
 # endif
 # ifdef USE_GETGRNAM_R
 #   define PREPARE_GETGRNAM \
-    long getgr_buf_len = sysconf(_SC_GETGR_R_SIZE_MAX); \
-    char *getgr_buf = ALLOCA_N(char, (getgr_buf_len < 0 ? (getgr_buf_len = 4096) : getgr_buf_len));
-#   define OBJ2GID(id) obj2gid((id), getgr_buf, getgr_buf_len)
-static rb_gid_t obj2gid(VALUE id, char *getgr_buf, size_t getgr_buf_len);
+    VALUE getgr_buf = 0
+#   define FINISH_GETGRNAM \
+    ALLOCV_END(getgr_buf)
+#   define OBJ2GID1(id) obj2gid((id), &getgr_buf)
+#   define OBJ2GID(id) obj2gid0(id)
+static rb_gid_t obj2gid(VALUE id, VALUE *getgr_buf);
+static inline rb_gid_t
+obj2gid0(VALUE id)
+{
+    rb_gid_t gid;
+    PREPARE_GETGRNAM;
+    gid = OBJ2GID1(id);
+    FINISH_GETGRNAM;
+    return gid;
+}
+static rb_gid_t obj2gid(VALUE id, VALUE *getgr_buf);
 # else
 #   define PREPARE_GETGRNAM	/* do nothing */
+#   define FINISH_GETGRNAM	/* do nothing */
 #   define OBJ2GID(id) obj2gid((id))
 static rb_gid_t obj2gid(VALUE id);
 # endif
 #else
 # define PREPARE_GETGRNAM	/* do nothing */
+# define FINISH_GETGRNAM	/* do nothing */
 # define OBJ2GID(id) NUM2GIDT(id)
 # ifdef p_gid_from_name
 #   undef p_gid_from_name
@@ -1703,7 +1730,6 @@ rb_execarg_addopt(VALUE execarg_obj, VAL https://github.com/ruby/ruby/blob/trunk/process.c#L1730
 	    }
 	    check_uid_switch();
 	    {
-		PREPARE_GETPWNAM;
 		eargp->uid = OBJ2UID(val);
 		eargp->uid_given = 1;
 	    }
@@ -1719,7 +1745,6 @@ rb_execarg_addopt(VALUE execarg_obj, VAL https://github.com/ruby/ruby/blob/trunk/process.c#L1745
 	    }
 	    check_gid_switch();
 	    {
-		PREPARE_GETGRNAM;
 		eargp->gid = OBJ2GID(val);
 		eargp->gid_given = 1;
 	    }
@@ -4686,7 +4711,7 @@ check_gid_switch(void) https://github.com/ruby/ruby/blob/trunk/process.c#L4711
 static rb_uid_t
 obj2uid(VALUE id
 # ifdef USE_GETPWNAM_R
-	, char *getpw_buf, size_t getpw_buf_len
+	, VALUE *getpw_tmp
 # endif
     )
 {
@@ -4701,6 +4726,16 @@ obj2uid(VALUE id https://github.com/ruby/ruby/blob/trunk/process.c#L4726
 	struct passwd *pwptr;
 #ifdef USE_GETPWNAM_R
 	struct passwd pwbuf;
+	char *getpw_buf;
+	long getpw_buf_len;
+	if (!*getpw_tmp) {
+	    getpw_buf_len = sysconf(_SC_GETPW_R_SIZE_MAX);
+	    if (getpw_buf_len < 0) getpw_buf_len = 4096;
+	    getpw_buf = rb_alloc_tmp_buffer(getpw_tmp, getpw_buf_len);
+	}
+	else {
+	    RSTRING_GETMEM(*getpw_tmp, getpw_buf, getpw_buf_len);
+	}
 	if (getpwnam_r(usrname, &pwbuf, getpw_buf, getpw_buf_len, &pwptr))
 	    rb_sys_fail("getpwnam_r");
 #else
@@ -4735,7 +4770,6 @@ obj2uid(VALUE id https://github.com/ruby/ruby/blob/trunk/process.c#L4770
 static VALUE
 p_uid_from_name(VALUE self, VALUE id)
 {
-    PREPARE_GETPWNAM
     return UIDT2NUM(OBJ2UID(id));
 }
 # endif
@@ -4745,7 +4779,7 @@ p_uid_from_name(VALUE self, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L4779
 static rb_gid_t
 obj2gid(VALUE id
 # ifdef USE_GETGRNAM_R
-	, char *getgr_buf, size_t getgr_buf_len
+	, VALUE *getgr_tmp
 # endif
     )
 {
@@ -4760,6 +4794,16 @@ obj2gid(VALUE id https://github.com/ruby/ruby/blob/trunk/process.c#L4794
 	struct group *grptr;
 #ifdef USE_GETGRNAM_R
 	struct group grbuf;
+	char *getgr_buf;
+	long getgr_buf_len;
+	if (!*getgr_tmp) {
+	    getgr_buf_len = sysconf(_SC_GETGR_R_SIZE_MAX);
+	    if (getgr_buf_len < 0) getgr_buf_len = 4096;
+	    getgr_buf = rb_alloc_tmp_buffer(getgr_tmp, getgr_buf_len);
+	}
+	else {
+	    RSTRING_GETMEM(*getgr_tmp, getgr_buf, getgr_buf_len);
+	}
 	if (getgrnam_r(grpname, &grbuf, getgr_buf, getgr_buf_len, &grptr))
 	    rb_sys_fail("getgrnam_r");
 #else
@@ -4794,7 +4838,6 @@ obj2gid(VALUE id https://github.com/ruby/ruby/blob/trunk/process.c#L4838
 static VALUE
 p_gid_from_name(VALUE self, VALUE id)
 {
-    PREPARE_GETGRNAM;
     return GIDT2NUM(OBJ2GID(id));
 }
 # endif
@@ -4813,7 +4856,6 @@ p_gid_from_name(VALUE self, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L4856
 static VALUE
 p_sys_setuid(VALUE obj, VALUE id)
 {
-    PREPARE_GETPWNAM;
     check_uid_switch();
     if (setuid(OBJ2UID(id)) != 0) rb_sys_fail(0);
     return Qnil;
@@ -4836,7 +4878,6 @@ p_sys_setuid(VALUE obj, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L4878
 static VALUE
 p_sys_setruid(VALUE obj, VALUE id)
 {
-    PREPARE_GETPWNAM;
     check_uid_switch();
     if (setruid(OBJ2UID(id)) != 0) rb_sys_fail(0);
     return Qnil;
@@ -4859,7 +4900,6 @@ p_sys_setruid(VALUE obj, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L4900
 static VALUE
 p_sys_seteuid(VALUE obj, VALUE id)
 {
-    PREPARE_GETPWNAM;
     check_uid_switch();
     if (seteuid(OBJ2UID(id)) != 0) rb_sys_fail(0);
     return Qnil;
@@ -4884,9 +4924,13 @@ p_sys_seteuid(VALUE obj, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L4924
 static VALUE
 p_sys_setreuid(VALUE obj, VALUE rid, VALUE eid)
 {
+    rb_uid_t ruid, euid;
     PREPARE_GETPWNAM;
     check_uid_switch();
-    if (setreuid(OBJ2UID(rid), OBJ2UID(eid)) != 0) rb_sys_fail(0);
+    ruid = OBJ2UID1(rid);
+    euid = OBJ2UID1(eid);
+    FINISH_GETPWNAM;
+    if (setreuid(ruid, euid) != 0) rb_sys_fail(0);
     return Qnil;
 }
 #else
@@ -4909,9 +4953,14 @@ p_sys_setreuid(VALUE obj, VALUE rid, VAL https://github.com/ruby/ruby/blob/trunk/process.c#L4953
 static VALUE
 p_sys_setresuid(VALUE obj, VALUE rid, VALUE eid, VALUE sid)
 {
+    rb_uid_t ruid, euid, suid;
     PREPARE_GETPWNAM;
     check_uid_switch();
-    if (setresuid(OBJ2UID(rid), OBJ2UID(eid), OBJ2UID(sid)) != 0) rb_sys_fail(0);
+    ruid = OBJ2UID1(rid);
+    euid = OBJ2UID1(eid);
+    suid = OBJ2UID1(sid);
+    FINISH_GETPWNAM;
+    if (setresuid(ruid, euid, suid) != 0) rb_sys_fail(0);
     return Qnil;
 }
 #else
@@ -4951,7 +5000,6 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/process.c#L5000
 proc_setuid(VALUE obj, VALUE id)
 {
     rb_uid_t uid;
-    PREPARE_GETPWNAM;
 
     check_uid_switch();
 
@@ -5023,7 +5071,6 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/process.c#L5071
 p_uid_change_privilege(VALUE obj, VALUE id)
 {
     rb_uid_t uid;
-    PREPARE_GETPWNAM;
 
     check_uid_switch();
 
@@ -5193,7 +5240,6 @@ p_uid_change_privilege(VALUE obj, VALUE https://github.com/ruby/ruby/blob/trunk/process.c#L5240
 static VALUE
 p_sys_setgid(VALUE obj, VALUE id)
 {
-    PREPARE_GETGRNAM;
     check_gid_switch();
     if (setgid(OBJ2GID(id)) != 0) rb_sys_fail(0);
     return Qnil;
@@ -5216,7 +5262,6 @@ p_sys_setgid(VALUE obj, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L5262
 static VALUE
 p_sys_setrgid(VALUE obj, VALUE id)
 {
-    PREPARE_GETGRNAM;
     check_gid_switch();
     if (setrgid(OBJ2GID(id)) != 0) rb_sys_fail(0);
     return Qnil;
@@ -5239,7 +5284,6 @@ p_sys_setrgid(VALUE obj, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L5284
 static VALUE
 p_sys_setegid(VALUE obj, VALUE id)
 {
-    PREPARE_GETGRNAM;
     check_gid_switch();
     if (setegid(OBJ2GID(id)) != 0) rb_sys_fail(0);
     return Qnil;
@@ -5264,9 +5308,13 @@ p_sys_setegid(VALUE obj, VALUE id) https://github.com/ruby/ruby/blob/trunk/process.c#L5308
 static VALUE
 p_sys_setregid(VALUE obj, VALUE rid, VALUE eid)
 {
+    rb_gid_t rgid, egid;
     PREPARE_GETGRNAM;
     check_gid_switch();
-    if (setregid(OBJ2GID(rid), OBJ2GID(eid)) != 0) rb_sys_fail(0);
+    rgid = OBJ2GID(rid);
+    egid = OBJ2GID(eid);
+    FINISH_GETGRNAM;
+    if (setregid(rgid, egid) != 0) rb_sys_fail(0);
     return Qnil;
 }
 #else
@@ -5288,9 +5336,14 @@ p_sys_setregid(VALUE obj, VALUE rid, VAL https://github.com/ruby/ruby/blob/trunk/process.c#L5336
 static VALUE
 p_sys_setresgid(VALUE obj, VALUE rid, VALUE eid, VALUE sid)
 {
+    rb_gid_t rgid, egid, sgid;
     PREPARE_GETGRNAM;
     check_gid_switch();
-    if (setresgid(OBJ2GID(rid), OBJ2GID(eid), OBJ2GID(sid)) != 0) rb_sys_fail(0);
+    rgid = OBJ2GID(rid);
+    egid = OBJ2GID(eid);
+    sgid = OBJ2GID(sid);
+    FINISH_GETGRNAM;
+    if (setresgid(rgid, egid, sgid) != 0) rb_sys_fail(0);
     return Qnil;
 }
 #else
@@ -5358,7 +5411,6 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/process.c#L5411
 proc_setgid(VALUE obj, VALUE id)
 {
     rb_gid_t gid;
-    PREPARE_GETGRNAM;
 
     check_gid_switch();
 
@@ -5506,8 +5558,9 @@ proc_setgroups(VALUE obj, VALUE ary) https://github.com/ruby/ruby/blob/trunk/process.c#L5558
     for (i = 0; i < ngroups; i++) {
 	VALUE g = RARRAY_AREF(ary, i);
 
-	groups[i] = OBJ2GID(g);
+	groups[i] = OBJ2GID1(g);
     }
+    FINISH_GETGRNAM;
 
     if (setgroups(ngroups, groups) == -1) /* ngroups <= maxgroups */
 	rb_sys_fail(0);
@@ -5540,7 +5593,6 @@ proc_setgroups(VALUE obj, VALUE ary) https://github.com/ruby/ruby/blob/trunk/process.c#L5593
 static VALUE
 proc_initgroups(VALUE obj, VALUE uname, VALUE base_grp)
 {
-    PREPARE_GETGRNAM;
     if (initgroups(StringValuePtr(uname), OBJ2GID(base_grp)) != 0) {
 	rb_sys_fail(0);
     }
@@ -5722,7 +5774,6 @@ static VALUE https://github.com/ruby/ruby/blob/trunk/process.c#L5774
 p_gid_change_privilege(VALUE obj, VALUE id)
 {
     rb_gid_t gid;
-    PREPARE_GETGRNAM;
 
     check_gid_switch();
 
@@ -5932,7 +5983,6 @@ proc_seteuid(rb_uid_t uid) https://github.com/ruby/ruby/blob/trunk/process.c#L5983
 static VALUE
 proc_seteuid_m(VALUE mod, VALUE euid)
 {
-    PREPARE_GETPWNAM;
     check_uid_switch();
     proc_seteuid(OBJ2UID(euid));
     return euid;
@@ -5998,7 +6048,6 @@ rb_seteuid_core(rb_uid_t euid) https://github.com/ruby/ruby/blob/trunk/process.c#L6048
 static VALUE
 p_uid_grant_privilege(VALUE obj, VALUE id)
 {
-    PREPARE_GETPWNAM;
     rb_seteuid_core(OBJ2UID(id));
     return id;
 }
@@ -6038,7 +6087,6 @@ proc_setegid(VALUE obj, VALUE egid) https://github.com/ruby/ruby/blob/trunk/process.c#L6087
 {
 #if defined(HAVE_SETRESGID) || defined(HAVE_SETREGID) || defined(HAVE_SETEGID) || defined(HAVE_SETGID)
     rb_gid_t gid;
-    PREPARE_GETGRNAM;
 #endif
 
     check_gid_switch();
@@ -6130,7 +6178,6 @@ rb_setegid_core(rb_gid_t egid) https://github.com/ruby/ruby/blob/trunk/process.c#L6178
 static VALUE
 p_gid_grant_privilege(VALUE obj, VALUE id)
 {
-    PREPARE_GETGRNAM;
     rb_setegid_core(OBJ2GID(id));
     return id;
 }

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

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