ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:44859] [ruby-trunk - Bug #5615][Open] A memory leak in hash.c on Solaris (and every environment which defines __sun)
@ 2011-11-11  5:56 Daisuke Ikegami
  2011-11-11  8:02 ` [ruby-dev:44860] [ruby-trunk - Bug #5615][Rejected] " Usaku NAKAMURA
  0 siblings, 1 reply; 2+ messages in thread
From: Daisuke Ikegami @ 2011-11-11  5:56 UTC (permalink / raw
  To: ruby developers list


Issue #5615 has been reported by Daisuke Ikegami.

----------------------------------------
Bug #5615: A memory leak in hash.c on Solaris (and every environment which defines __sun)
http://redmine.ruby-lang.org/issues/5615

Author: Daisuke Ikegami
Status: Open
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0
ruby -v: ruby 2.0.0dev (2011-11-11 trunk 33704) [i686-linux]


Reproducing steps: I didn't test the patch. Please do.
Result of `ruby -v`: ruby 2.0.0dev (2011-11-11 trunk 33704) [i686-linux]
What you expected: ruby should not have any memory leak.
What you happened: It seems to have a memory leak of ruby on Solaris.
The bug is in the function ruby_setenv() in hash.c.

Here is the code snippet around the bug:
  2287  #elif defined __sun
  2288      size_t len;
  2289      char **env_ptr, *str;
  2290      if (strchr(name, '=')) {
  2291          errno = EINVAL;
  2292          rb_sys_fail("ruby_setenv");
  2293      }
  2294      len = strlen(name);
  2295      for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
  2296          if (!strncmp(str, name, len) && str[len] == '=') {
  2297              if (!in_origenv(str)) free(str);
  2298              while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
  2299              break;
  2300          }
  2301      }
  2302      if (value) {
  2303          str = malloc(len += strlen(value) + 2);
  2304          snprintf(str, len, "%s=%s", name, value);
  2305          if (putenv(str))
  2306              rb_sys_fail("putenv");
  2307      }
  2308  #else  /* WIN32 */
 
In line 2303, a variable 'str' is malloc-ed, but, seems never to free. Therefore it seems a memory leak.

This bug is found by using cppcheck technically.
Here is URL of cppcheck:
http://sourceforge.net/projects/cppcheck/

The result of cppcheck is following:
% cppcheck -f -I. hash.c
Checking hash.c...
Checking hash.c: ENV_IGNORECASE...
Checking hash.c: HAVE_SETENV;HAVE_UNSETENV...
Checking hash.c: _WIN32...
Checking hash.c: __APPLE__...
Checking hash.c: __sun...
[hash.c:2351]: (error) Memory leak: str
(information) Cppcheck cannot find all the include files (use --check-config for details)

I'll attach a patch to fix this bug. After applying this patch, cppcheck does not warn.



-- 
http://redmine.ruby-lang.org

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-dev:44860] [ruby-trunk - Bug #5615][Rejected] A memory leak in hash.c on Solaris (and every environment which defines __sun)
  2011-11-11  5:56 [ruby-dev:44859] [ruby-trunk - Bug #5615][Open] A memory leak in hash.c on Solaris (and every environment which defines __sun) Daisuke Ikegami
@ 2011-11-11  8:02 ` Usaku NAKAMURA
  0 siblings, 0 replies; 2+ messages in thread
From: Usaku NAKAMURA @ 2011-11-11  8:02 UTC (permalink / raw
  To: ruby developers list


Issue #5615 has been updated by Usaku NAKAMURA.

Status changed from Open to Rejected

putenv(3)は引数で与えたポインタをそのまま使うので、この時点では解放することができません。
ちなみに、該当部分の少し上で(現時点のtrunkなら2297行目)
	    if (!in_origenv(str)) free(str);
というコードがありますが、これが不要になったら解放するコードになります。

# ということを、私も今回初めて知りました。
----------------------------------------
Bug #5615: A memory leak in hash.c on Solaris (and every environment which defines __sun)
http://redmine.ruby-lang.org/issues/5615

Author: Daisuke Ikegami
Status: Rejected
Priority: Normal
Assignee: 
Category: 
Target version: 2.0.0
ruby -v: ruby 2.0.0dev (2011-11-11 trunk 33704) [i686-linux]


Reproducing steps: I didn't test the patch. Please do.
Result of `ruby -v`: ruby 2.0.0dev (2011-11-11 trunk 33704) [i686-linux]
What you expected: ruby should not have any memory leak.
What you happened: It seems to have a memory leak of ruby on Solaris.
The bug is in the function ruby_setenv() in hash.c.

Here is the code snippet around the bug:
  2287  #elif defined __sun
  2288      size_t len;
  2289      char **env_ptr, *str;
  2290      if (strchr(name, '=')) {
  2291          errno = EINVAL;
  2292          rb_sys_fail("ruby_setenv");
  2293      }
  2294      len = strlen(name);
  2295      for (env_ptr = GET_ENVIRON(environ); (str = *env_ptr) != 0; ++env_ptr) {
  2296          if (!strncmp(str, name, len) && str[len] == '=') {
  2297              if (!in_origenv(str)) free(str);
  2298              while ((env_ptr[0] = env_ptr[1]) != 0) env_ptr++;
  2299              break;
  2300          }
  2301      }
  2302      if (value) {
  2303          str = malloc(len += strlen(value) + 2);
  2304          snprintf(str, len, "%s=%s", name, value);
  2305          if (putenv(str))
  2306              rb_sys_fail("putenv");
  2307      }
  2308  #else  /* WIN32 */
 
In line 2303, a variable 'str' is malloc-ed, but, seems never to free. Therefore it seems a memory leak.

This bug is found by using cppcheck technically.
Here is URL of cppcheck:
http://sourceforge.net/projects/cppcheck/

The result of cppcheck is following:
% cppcheck -f -I. hash.c
Checking hash.c...
Checking hash.c: ENV_IGNORECASE...
Checking hash.c: HAVE_SETENV;HAVE_UNSETENV...
Checking hash.c: _WIN32...
Checking hash.c: __APPLE__...
Checking hash.c: __sun...
[hash.c:2351]: (error) Memory leak: str
(information) Cppcheck cannot find all the include files (use --check-config for details)

I'll attach a patch to fix this bug. After applying this patch, cppcheck does not warn.



-- 
http://redmine.ruby-lang.org

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-11-11  8:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-11-11  5:56 [ruby-dev:44859] [ruby-trunk - Bug #5615][Open] A memory leak in hash.c on Solaris (and every environment which defines __sun) Daisuke Ikegami
2011-11-11  8:02 ` [ruby-dev:44860] [ruby-trunk - Bug #5615][Rejected] " Usaku NAKAMURA

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).