RE: [PATCH] STDCXX-1020 memchk in utility should honor TMPDIR variable
Fix to original patch so that only getenv gets called once and use PATH_MAX for fname array size. Also use strlen to get the len of string instead of sizeof. Index: util/memchk.cpp === --- util/memchk.cpp (revision 702657) +++ util/memchk.cpp (working copy) @@ -67,6 +67,9 @@ # endif #endif // P_tmpdir +#ifndef PATH_MAX +# define PATH_MAX 1024 +#endif #if defined (_RWSTD_EDG_ECCP) && !defined (_WIN32) @@ -116,9 +119,14 @@ // operation away (as SunOS does, for instance) // fd = open ("/dev/null", O_WRONLY); +const char *tmpdir = getenv ("TMPDIR"); +if (tmpdir == NULL) { +tmpdir = P_tmpdir; +} + #ifdef _WIN32 -char* const fname = tempnam (P_tmpdir, ".rwmemchk.tmp"); +char* const fname = tempnam (tmpdir, ".rwmemchk.tmp"); if (!fname) return size_t (-1); @@ -137,10 +145,13 @@ #else // !_WIN32 -# define TMP_TEMPLATE P_tmpdir "/rwmemchk-XX" +char fname_buf [PATH_MAX]; -char fname_buf [] = TMP_TEMPLATE; +size_t len = strlen (tmpdir) - 1; +memcpy (fname_buf, tmpdir, len); +memcpy (fname_buf+len, "/rwmemchk-XX", sizeof ("/rwmemchk-XX")); + fd = mkstemp (fname_buf); if (fd < 0) { > -Original Message- > From: Scott Zhong [mailto:[EMAIL PROTECTED] > Sent: Wednesday, October 08, 2008 11:14 AM > To: dev@stdcxx.apache.org > Subject: [PATCH] STDCXX-1020 memchk in utility should honor TMPDIR > variable > > https://issues.apache.org/jira/browse/STDCXX-1020 > > Index: util/memchk.cpp > === > --- util/memchk.cpp (revision 702657) > +++ util/memchk.cpp (working copy) > @@ -116,9 +116,11 @@ > // operation away (as SunOS does, for instance) > // fd = open ("/dev/null", O_WRONLY); > > + char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv > ("TMPDIR"); > + > #ifdef _WIN32 > > -char* const fname = tempnam (P_tmpdir, ".rwmemchk.tmp"); > +char* const fname = tempnam (tmpdir, ".rwmemchk.tmp"); > > if (!fname) > return size_t (-1); > @@ -137,10 +139,13 @@ > > #else // !_WIN32 > > -# define TMP_TEMPLATE P_tmpdir "/rwmemchk-XX" > +char fname_buf [sizeof (tmpdir) + sizeof ("/rwmemchk-XX")]; > > -char fname_buf [] = TMP_TEMPLATE; > +size_t len = sizeof (tmpdir) - 1; > > +memcpy (fname_buf, tmpdir, len); > +memcpy (fname_buf+len, "/rwmemchk-XX", sizeof > ("/rwmemchk-XX")); > + > fd = mkstemp (fname_buf); > > if (fd < 0) {
RE: [PATCH] STDCXX-1019 __rw_mkstemp in file.cpp should honor TMPDIR environment variable
Fix to fnamebuf array size and invoke getenv only once. Index: src/file.cpp === --- src/file.cpp(revision 702657) +++ src/file.cpp(working copy) @@ -42,6 +42,7 @@ #include // for P_tmpdir, std{err,in,out}, tmpnam() #include// for mkstemp(), strtoul(), size_t #include // for isalpha(), isspace(), toupper() +#include// for memcpy() #if defined (_WIN32) && !defined (__CYGWIN__) @@ -58,6 +59,9 @@ # define _BINARY 0 #endif +#ifndef PATH_MAX +# define PATH_MAX 1024 +#endif #include #include @@ -257,8 +261,18 @@ #define P_tmpdir "/tmp" # endif // P_tmpdir -char fnamebuf[] = P_tmpdir "/.rwtmpXX"; +const char *tmpdir = getenv ("TMPDIR"); +if (tmpdir == NULL) { +tmpdir = P_tmpdir; +} +char fnamebuf [PATH_MAX]; + +size_t len = strlen (tmpdir) - 1; + +memcpy (fnamebuf, tmpdir, len); +memcpy (fnamebuf+len, "/.rwtmpXX", sizeof ("/.rwtmpXX")); + fd = mkstemp (fnamebuf); if (fd >= 0) @@ -294,7 +308,7 @@ // names that have no extension. tempnam uses malloc to allocate // space for the filename; the program is responsible for freeing // this space when it is no longer needed. -char* const fname = tempnam (P_tmpdir, ".rwtmp"); +char* const fname = tempnam (tmpdir, ".rwtmp"); if (!fname) return -1; > -Original Message- > From: Scott Zhong [mailto:[EMAIL PROTECTED] > Sent: Wednesday, October 08, 2008 11:03 AM > To: dev@stdcxx.apache.org > Subject: RE: [PATCH] STDCXX-1019 __rw_mkstemp in file.cpp should honor > TMPDIR environment variable > > Posted wrong diff. here is the correct diff > > Index: src/file.cpp > === > --- src/file.cpp(revision 702657) > +++ src/file.cpp(working copy) > @@ -42,6 +42,7 @@ > #include // for P_tmpdir, std{err,in,out}, tmpnam() > #include// for mkstemp(), strtoul(), size_t > #include // for isalpha(), isspace(), toupper() > +#include// for memcpy() > > > #if defined (_WIN32) && !defined (__CYGWIN__) > @@ -257,8 +258,15 @@ > #define P_tmpdir "/tmp" > # endif // P_tmpdir > > -char fnamebuf[] = P_tmpdir "/.rwtmpXX"; > +char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv > ("TMPDIR"); > > +char fnamebuf [sizeof (tmpdir) + sizeof ("/.rwtmpXX")]; > + > +size_t len = sizeof (tmpdir) - 1; > + > +memcpy (fnamebuf, tmpdir, len); > +memcpy (fnamebuf+len, "/.rwtmpXX", sizeof ("/.rwtmpXX")); > + > fd = mkstemp (fnamebuf); > > if (fd >= 0) > @@ -294,7 +302,7 @@ > // names that have no extension. tempnam uses malloc to allocate > // space for the filename; the program is responsible for freeing > // this space when it is no longer needed. > -char* const fname = tempnam (P_tmpdir, ".rwtmp"); > +char* const fname = tempnam (tmpdir, ".rwtmp"); > > if (!fname) > return -1; > > > -Original Message- > > From: Scott Zhong [mailto:[EMAIL PROTECTED] > > Sent: Wednesday, October 08, 2008 10:15 AM > > To: dev@stdcxx.apache.org > > Subject: [PATCH] STDCXX-1019 __rw_mkstemp in file.cpp should honor > TMPDIR > > environment variable > > > > https://issues.apache.org/jira/browse/STDCXX-1019 > > > > file affected: /src/file.cpp > > > > Index: src/file.cpp > > === > > --- src/file.cpp(revision 702657) > > +++ src/file.cpp(working copy) > > @@ -257,7 +257,9 @@ > > #define P_tmpdir "/tmp" > > # endif // P_tmpdir > > > > -char fnamebuf[] = P_tmpdir "/.rwtmpXX"; > > +char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv > > ("TMPDIR"); > > + > > +char fnamebuf[] = tmpdir "/.rwtmpXX"; > > > > fd = mkstemp (fnamebuf); > > > > @@ -294,7 +296,7 @@ > > // names that have no extension. tempnam uses malloc to allocate > > // space for the filename; the program is responsible for freeing > > // this space when it is no longer needed. > > -char* const fname = tempnam (P_tmpdir, ".rwtmp"); > > +char* const fname = tempnam (tmpdir, ".rwtmp"); > > > > if (!fname) > > return -1;
RE: [PATCH] STDCXX-401 test suite should honor TMPDIR
We need to use PATH_MAX for the fname_buf array size. Index: tests/src/file.cpp === --- tests/src/file.cpp (revision 702657) +++ tests/src/file.cpp (working copy) @@ -208,8 +208,13 @@ #ifndef _RWSTD_NO_MKSTEMP # define TMP_TEMPLATE "tmpfile-XX" +const char *tmpdir = getenv ("TMPDIR"); +if (tmpdir == NULL) { +tmpdir = P_tmpdir; +} + if (!buf) { -static char fname_buf [sizeof (P_tmpdir) + sizeof (TMP_TEMPLATE)]; +static char fname_buf [PATH_MAX]; buf = fname_buf; *buf = '\0'; @@ -217,13 +222,13 @@ if ('\0' == *buf) { // copy the template to the buffer; make sure there is exactly -// one path separator character between P_tmpdir and the file +// one path separator character between tmpdir and the file // name template (it doesn't really matter how many there are // as long as it's at least one, but one looks better than two // in diagnostic messages) -size_t len = sizeof (P_tmpdir) - 1; +size_t len = strlen (tmpdir) - 1; -memcpy (buf, P_tmpdir, len); +memcpy (buf, tmpdir, len); if (_RWSTD_PATH_SEP != buf [len - 1]) buf [len++] = _RWSTD_PATH_SEP; @@ -251,7 +256,7 @@ # ifdef _WIN32 // create a temporary file name -char* fname = tempnam (P_tmpdir, ".rwtest-tmp"); +char* fname = tempnam (tmpdir, ".rwtest-tmp"); if (fname) { @@ -272,7 +277,7 @@ else { fprintf (stderr, "%s:%d: tempnam(\"%s\", \"%s\") failed: %s\n", __FILE__, __LINE__, - P_tmpdir, ".rwtest-tmp", strerror (errno)); + tmpdir, ".rwtest-tmp", strerror (errno)); } # else > -Original Message- > From: Scott Zhong [mailto:[EMAIL PROTECTED] > Sent: Thursday, October 09, 2008 9:31 AM > To: dev@stdcxx.apache.org > Subject: RE: [PATCH] STDCXX-401 test suite should honor TMPDIR > > That const_cast isn't suppose to be there. Here is the correct version: > > Index: tests/src/file.cpp > === > --- tests/src/file.cpp (revision 702657) > +++ tests/src/file.cpp (working copy) > @@ -208,8 +208,13 @@ > #ifndef _RWSTD_NO_MKSTEMP > # define TMP_TEMPLATE "tmpfile-XX" > > +const char *tmpdir = getenv ("TMPDIR"); > +if (tmpdir == NULL) { > +tmpdir = P_tmpdir; > +} > + > if (!buf) { > -static char fname_buf [sizeof (P_tmpdir) + sizeof > (TMP_TEMPLATE)]; > +static char fname_buf [strlen (tmpdir) + sizeof > (TMP_TEMPLATE)]; > > buf = fname_buf; > *buf = '\0'; > @@ -217,13 +222,13 @@ > > if ('\0' == *buf) { > // copy the template to the buffer; make sure there is exactly > -// one path separator character between P_tmpdir and the file > +// one path separator character between tmpdir and the file > // name template (it doesn't really matter how many there are > // as long as it's at least one, but one looks better than two > // in diagnostic messages) > -size_t len = sizeof (P_tmpdir) - 1; > +size_t len = strlen (tmpdir) - 1; > > -memcpy (buf, P_tmpdir, len); > +memcpy (buf, tmpdir, len); > if (_RWSTD_PATH_SEP != buf [len - 1]) > buf [len++] = _RWSTD_PATH_SEP; > > @@ -251,7 +256,7 @@ > # ifdef _WIN32 > > // create a temporary file name > -char* fname = tempnam (P_tmpdir, ".rwtest-tmp"); > +char* fname = tempnam (tmpdir, ".rwtest-tmp"); > > if (fname) { > > @@ -272,7 +277,7 @@ > else { > fprintf (stderr, "%s:%d: tempnam(\"%s\", \"%s\") failed: %s\n", > __FILE__, __LINE__, > - P_tmpdir, ".rwtest-tmp", strerror (errno)); > + tmpdir, ".rwtest-tmp", strerror (errno)); > } > > # else > > > -Original Message- > > From: Scott Zhong [mailto:[EMAIL PROTECTED] > > Sent: Thursday, October 09, 2008 9:25 AM > > To: dev@stdcxx.apache.org > > Subject: RE: [PATCH] STDCXX-401 test suite should honor TMPDIR > > > > Hi Farid, thanks for the quick response, here is the new version with > > the changes suggested. > > > > Index: tests/src/file.cpp > > === > > --- tests/src/file.cpp (revision 702657) > > +++ tests/src/file.cpp (working copy) > > @@ -208,8 +208,13 @@ > > #ifndef _RWSTD_NO_MKSTEMP > > # define TMP_TEMPLATE "tmpfile-XX" > > > > +const char *tmpdir = getenv ("TMPDIR"); > > +if (tmpdir == NULL) { > > +tmpdir = const_cast(P_tmpdir); > > +} > > + > > if (!buf) { > > -static char fname_buf [sizeof (P_tmpdir) + sizeof > > (TMP_TEMPLATE)]; > > +static char fname_buf [strlen (tmpdir) + sizeof > > (TMP_TEMPLATE)]; > > > > buf = fname_buf; > > *buf =
RE: [PATCH] STDCXX-401 test suite should honor TMPDIR
That const_cast isn't suppose to be there. Here is the correct version: Index: tests/src/file.cpp === --- tests/src/file.cpp (revision 702657) +++ tests/src/file.cpp (working copy) @@ -208,8 +208,13 @@ #ifndef _RWSTD_NO_MKSTEMP # define TMP_TEMPLATE "tmpfile-XX" +const char *tmpdir = getenv ("TMPDIR"); +if (tmpdir == NULL) { +tmpdir = P_tmpdir; +} + if (!buf) { -static char fname_buf [sizeof (P_tmpdir) + sizeof (TMP_TEMPLATE)]; +static char fname_buf [strlen (tmpdir) + sizeof (TMP_TEMPLATE)]; buf = fname_buf; *buf = '\0'; @@ -217,13 +222,13 @@ if ('\0' == *buf) { // copy the template to the buffer; make sure there is exactly -// one path separator character between P_tmpdir and the file +// one path separator character between tmpdir and the file // name template (it doesn't really matter how many there are // as long as it's at least one, but one looks better than two // in diagnostic messages) -size_t len = sizeof (P_tmpdir) - 1; +size_t len = strlen (tmpdir) - 1; -memcpy (buf, P_tmpdir, len); +memcpy (buf, tmpdir, len); if (_RWSTD_PATH_SEP != buf [len - 1]) buf [len++] = _RWSTD_PATH_SEP; @@ -251,7 +256,7 @@ # ifdef _WIN32 // create a temporary file name -char* fname = tempnam (P_tmpdir, ".rwtest-tmp"); +char* fname = tempnam (tmpdir, ".rwtest-tmp"); if (fname) { @@ -272,7 +277,7 @@ else { fprintf (stderr, "%s:%d: tempnam(\"%s\", \"%s\") failed: %s\n", __FILE__, __LINE__, - P_tmpdir, ".rwtest-tmp", strerror (errno)); + tmpdir, ".rwtest-tmp", strerror (errno)); } # else > -Original Message- > From: Scott Zhong [mailto:[EMAIL PROTECTED] > Sent: Thursday, October 09, 2008 9:25 AM > To: dev@stdcxx.apache.org > Subject: RE: [PATCH] STDCXX-401 test suite should honor TMPDIR > > Hi Farid, thanks for the quick response, here is the new version with > the changes suggested. > > Index: tests/src/file.cpp > === > --- tests/src/file.cpp (revision 702657) > +++ tests/src/file.cpp (working copy) > @@ -208,8 +208,13 @@ > #ifndef _RWSTD_NO_MKSTEMP > # define TMP_TEMPLATE "tmpfile-XX" > > +const char *tmpdir = getenv ("TMPDIR"); > +if (tmpdir == NULL) { > +tmpdir = const_cast(P_tmpdir); > +} > + > if (!buf) { > -static char fname_buf [sizeof (P_tmpdir) + sizeof > (TMP_TEMPLATE)]; > +static char fname_buf [strlen (tmpdir) + sizeof > (TMP_TEMPLATE)]; > > buf = fname_buf; > *buf = '\0'; > @@ -217,13 +222,13 @@ > > if ('\0' == *buf) { > // copy the template to the buffer; make sure there is exactly > -// one path separator character between P_tmpdir and the file > +// one path separator character between tmpdir and the file > // name template (it doesn't really matter how many there are > // as long as it's at least one, but one looks better than two > // in diagnostic messages) > -size_t len = sizeof (P_tmpdir) - 1; > +size_t len = strlen (tmpdir) - 1; > > -memcpy (buf, P_tmpdir, len); > +memcpy (buf, tmpdir, len); > if (_RWSTD_PATH_SEP != buf [len - 1]) > buf [len++] = _RWSTD_PATH_SEP; > > @@ -251,7 +256,7 @@ > # ifdef _WIN32 > > // create a temporary file name > -char* fname = tempnam (P_tmpdir, ".rwtest-tmp"); > +char* fname = tempnam (tmpdir, ".rwtest-tmp"); > > if (fname) { > > @@ -272,7 +277,7 @@ > else { > fprintf (stderr, "%s:%d: tempnam(\"%s\", \"%s\") failed: %s\n", > __FILE__, __LINE__, > - P_tmpdir, ".rwtest-tmp", strerror (errno)); > + tmpdir, ".rwtest-tmp", strerror (errno)); > } > > # else > > > -Original Message- > > From: Farid Zaripov [mailto:[EMAIL PROTECTED] > > Sent: Thursday, October 09, 2008 2:20 AM > > To: dev@stdcxx.apache.org > > Subject: Re: [PATCH] STDCXX-401 test suite should honor TMPDIR > > > > Hi Scott. > > > > > Index: file.cpp > > > === > > > --- file.cpp(revision 702657) > > > +++ file.cpp(working copy) > > > @@ -208,8 +208,10 @@ > > > #ifndef _RWSTD_NO_MKSTEMP > > > # define TMP_TEMPLATE "tmpfile-XX" > > > > > > +char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv > > ("TMPDIR"); > > > > tmpdir might be const char*. And why getenv("TMPDIR") is called > twice? > > > > > + > > > if (!buf) { > > > -static char fname_buf [sizeof (P_tmpdir) + sizeof > > (TMP_TEMPLATE)]; > > > +static char fname_buf [sizeof (tmpdir) + sizeof > > (TMP_TEMPLATE)]; > > > > Here si
RE: [PATCH] STDCXX-401 test suite should honor TMPDIR
Hi Farid, thanks for the quick response, here is the new version with the changes suggested. Index: tests/src/file.cpp === --- tests/src/file.cpp (revision 702657) +++ tests/src/file.cpp (working copy) @@ -208,8 +208,13 @@ #ifndef _RWSTD_NO_MKSTEMP # define TMP_TEMPLATE "tmpfile-XX" +const char *tmpdir = getenv ("TMPDIR"); +if (tmpdir == NULL) { +tmpdir = const_cast(P_tmpdir); +} + if (!buf) { -static char fname_buf [sizeof (P_tmpdir) + sizeof (TMP_TEMPLATE)]; +static char fname_buf [strlen (tmpdir) + sizeof (TMP_TEMPLATE)]; buf = fname_buf; *buf = '\0'; @@ -217,13 +222,13 @@ if ('\0' == *buf) { // copy the template to the buffer; make sure there is exactly -// one path separator character between P_tmpdir and the file +// one path separator character between tmpdir and the file // name template (it doesn't really matter how many there are // as long as it's at least one, but one looks better than two // in diagnostic messages) -size_t len = sizeof (P_tmpdir) - 1; +size_t len = strlen (tmpdir) - 1; -memcpy (buf, P_tmpdir, len); +memcpy (buf, tmpdir, len); if (_RWSTD_PATH_SEP != buf [len - 1]) buf [len++] = _RWSTD_PATH_SEP; @@ -251,7 +256,7 @@ # ifdef _WIN32 // create a temporary file name -char* fname = tempnam (P_tmpdir, ".rwtest-tmp"); +char* fname = tempnam (tmpdir, ".rwtest-tmp"); if (fname) { @@ -272,7 +277,7 @@ else { fprintf (stderr, "%s:%d: tempnam(\"%s\", \"%s\") failed: %s\n", __FILE__, __LINE__, - P_tmpdir, ".rwtest-tmp", strerror (errno)); + tmpdir, ".rwtest-tmp", strerror (errno)); } # else > -Original Message- > From: Farid Zaripov [mailto:[EMAIL PROTECTED] > Sent: Thursday, October 09, 2008 2:20 AM > To: dev@stdcxx.apache.org > Subject: Re: [PATCH] STDCXX-401 test suite should honor TMPDIR > > Hi Scott. > > > Index: file.cpp > > === > > --- file.cpp(revision 702657) > > +++ file.cpp(working copy) > > @@ -208,8 +208,10 @@ > > #ifndef _RWSTD_NO_MKSTEMP > > # define TMP_TEMPLATE "tmpfile-XX" > > > > +char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv > ("TMPDIR"); > > tmpdir might be const char*. And why getenv("TMPDIR") is called twice? > > > + > > if (!buf) { > > -static char fname_buf [sizeof (P_tmpdir) + sizeof > (TMP_TEMPLATE)]; > > +static char fname_buf [sizeof (tmpdir) + sizeof > (TMP_TEMPLATE)]; > > Here sizeof (tmpdir) != strlen (tmpdir). I think that using here > PATH_MAX is ok. > > [...] > > -size_t len = sizeof (P_tmpdir) - 1; > > +size_t len = sizeof (tmpdir) - 1; > > Same. > > Farid.
Re: [PATCH] STDCXX-401 test suite should honor TMPDIR
Hi Scott. > Index: file.cpp > === > --- file.cpp(revision 702657) > +++ file.cpp(working copy) > @@ -208,8 +208,10 @@ > #ifndef _RWSTD_NO_MKSTEMP > # define TMP_TEMPLATE "tmpfile-XX" > > +char *tmpdir = getenv ("TMPDIR") == NULL ? P_tmpdir : getenv ("TMPDIR"); tmpdir might be const char*. And why getenv("TMPDIR") is called twice? > + > if (!buf) { > -static char fname_buf [sizeof (P_tmpdir) + sizeof (TMP_TEMPLATE)]; > +static char fname_buf [sizeof (tmpdir) + sizeof (TMP_TEMPLATE)]; Here sizeof (tmpdir) != strlen (tmpdir). I think that using here PATH_MAX is ok. [...] > -size_t len = sizeof (P_tmpdir) - 1; > +size_t len = sizeof (tmpdir) - 1; Same. Farid.