Change 30007 by [EMAIL PROTECTED] on 2007/01/26 13:50:55
Integrate:
[ 28525]
Add Russ Allbery's public domain implementations of strlcat and
strlcpy as Perl_my_strlcat and Perl_my_strlcpy to the Perl core.
Thanks Russ!
[ 28528]
Change existing uses of strlcpy()/strlcat() to use new my_strlcpy()/
my_strlcat() API. Convert ext/File/Glob/bsd_glob.c to use
my_strlcat(). Add to the strlcy()/strlcat() todo entry.
[ 28533]
Convert some low hanging fruit to my_strlcpy/my_strlcat.
[ 28545]
Fix change #28533: my_strlcpy does not return the dest str!
Affected files ...
... //depot/maint-5.8/perl/doio.c#98 integrate
... //depot/maint-5.8/perl/embed.fnc#197 integrate
... //depot/maint-5.8/perl/embed.h#148 integrate
... //depot/maint-5.8/perl/ext/File/Glob/bsd_glob.c#4 integrate
... //depot/maint-5.8/perl/global.sym#56 integrate
... //depot/maint-5.8/perl/mg.c#138 integrate
... //depot/maint-5.8/perl/perl.h#141 integrate
... //depot/maint-5.8/perl/pp_ctl.c#163 integrate
... //depot/maint-5.8/perl/pp_sys.c#134 integrate
... //depot/maint-5.8/perl/proto.h#187 integrate
... //depot/maint-5.8/perl/toke.c#154 integrate
... //depot/maint-5.8/perl/util.c#134 integrate
Differences ...
==== //depot/maint-5.8/perl/doio.c#98 (text) ====
Index: perl/doio.c
--- perl/doio.c#97~30006~ 2007-01-26 04:19:35.000000000 -0800
+++ perl/doio.c 2007-01-26 05:50:55.000000000 -0800
@@ -257,17 +257,10 @@
}
mode[0] = 'w';
writing = 1;
-#ifdef HAS_STRLCAT
if (out_raw)
- strlcat(mode, "b", PERL_MODE_MAX - 1);
+ my_strlcat(mode, "b", PERL_MODE_MAX - 1);
else if (out_crlf)
- strlcat(mode, "t", PERL_MODE_MAX - 1);
-#else
- if (out_raw)
- strcat(mode, "b");
- else if (out_crlf)
- strcat(mode, "t");
-#endif
+ my_strlcat(mode, "t", PERL_MODE_MAX - 1);
if (num_svs > 1) {
fp = PerlProc_popen_list(mode, num_svs, svp);
}
@@ -295,17 +288,10 @@
}
writing = 1;
-#ifdef HAS_STRLCAT
if (out_raw)
- strlcat(mode, "b", PERL_MODE_MAX - 1);
+ my_strlcat(mode, "b", PERL_MODE_MAX - 1);
else if (out_crlf)
- strlcat(mode, "t", PERL_MODE_MAX - 1);
-#else
- if (out_raw)
- strcat(mode, "b");
- else if (out_crlf)
- strcat(mode, "t");
-#endif
+ my_strlcat(mode, "t", PERL_MODE_MAX - 1);
if (*type == '&') {
duplicity:
dodup = PERLIO_DUP_FD;
@@ -428,17 +414,10 @@
type++;
} while (isSPACE(*type));
mode[0] = 'r';
-#ifdef HAS_STRLCAT
if (in_raw)
- strlcat(mode, "b", PERL_MODE_MAX - 1);
+ my_strlcat(mode, "b", PERL_MODE_MAX - 1);
else if (in_crlf)
- strlcat(mode, "t", PERL_MODE_MAX - 1);
-#else
- if (in_raw)
- strcat(mode, "b");
- else if (in_crlf)
- strcat(mode, "t");
-#endif
+ my_strlcat(mode, "t", PERL_MODE_MAX - 1);
if (*type == '&') {
goto duplicity;
}
@@ -489,17 +468,10 @@
TAINT_PROPER("piped open");
mode[0] = 'r';
-#ifdef HAS_STRLCAT
if (in_raw)
- strlcat(mode, "b", PERL_MODE_MAX - 1);
+ my_strlcat(mode, "b", PERL_MODE_MAX - 1);
else if (in_crlf)
- strlcat(mode, "t", PERL_MODE_MAX - 1);
-#else
- if (in_raw)
- strcat(mode, "b");
- else if (in_crlf)
- strcat(mode, "t");
-#endif
+ my_strlcat(mode, "t", PERL_MODE_MAX - 1);
if (num_svs > 1) {
fp = PerlProc_popen_list(mode,num_svs,svp);
@@ -527,17 +499,10 @@
;
mode[0] = 'r';
-#ifdef HAS_STRLCAT
if (in_raw)
- strlcat(mode, "b", PERL_MODE_MAX - 1);
+ my_strlcat(mode, "b", PERL_MODE_MAX - 1);
else if (in_crlf)
- strlcat(mode, "t", PERL_MODE_MAX - 1);
-#else
- if (in_raw)
- strcat(mode, "b");
- else if (in_crlf)
- strcat(mode, "t");
-#endif
+ my_strlcat(mode, "t", PERL_MODE_MAX - 1);
if (*name == '-' && name[1] == '\0') {
fp = PerlIO_stdin();
@@ -1461,10 +1426,9 @@
char *cmd;
/* Make a copy so we can change it */
- const int cmdlen = strlen(incmd);
- Newx(cmd, cmdlen+1, char);
- strncpy(cmd, incmd, cmdlen);
- cmd[cmdlen] = 0;
+ const Size_t cmdlen = strlen(incmd) + 1;
+ Newx(cmd, cmdlen, char);
+ my_strlcpy(cmd, incmd, cmdlen);
while (*cmd && isSPACE(*cmd))
cmd++;
@@ -1476,19 +1440,11 @@
char flags[PERL_FLAGS_MAX];
if (strnEQ(cmd,PL_cshname,PL_cshlen) &&
strnEQ(cmd+PL_cshlen," -c",3)) {
-#ifdef HAS_STRLCPY
- strlcpy(flags, "-c", PERL_FLAGS_MAX);
-#else
- strcpy(flags,"-c");
-#endif
+ my_strlcpy(flags, "-c", PERL_FLAGS_MAX);
s = cmd+PL_cshlen+3;
if (*s == 'f') {
s++;
-#ifdef HAS_STRLCPY
- strlcat(flags, "f", PERL_FLAGS_MAX - 2);
-#else
- strcat(flags,"f");
-#endif
+ my_strlcat(flags, "f", PERL_FLAGS_MAX - 2);
}
if (*s == ' ')
s++;
@@ -2290,7 +2246,7 @@
STRLEN len;
const char *mbuf = SvPV_const(mstr, len);
- const I32 n = ((I32)len > msize) ? msize : (I32)len;
+ const I32 n = (len > msize) ? msize : len;
Copy(mbuf, shm + mpos, n, char);
if (n < msize)
memzero(shm + mpos + n, msize - n);
==== //depot/maint-5.8/perl/embed.fnc#197 (text) ====
Index: perl/embed.fnc
--- perl/embed.fnc#196~30004~ 2007-01-26 03:57:08.000000000 -0800
+++ perl/embed.fnc 2007-01-26 05:50:55.000000000 -0800
@@ -1653,6 +1653,14 @@
px |void |my_clearenv
+#ifndef HAS_STRLCAT
+Apno |Size_t |my_strlcat |NULLOK char *dst|NULLOK const char *src|Size_t
size
+#endif
+
+#ifndef HAS_STRLCPY
+Apno |Size_t |my_strlcpy |NULLOK char *dst|NULLOK const char
*src|Size_t size
+#endif
+
Ap |GV* |gv_fetchpvn_flags|NN const char* name|STRLEN len|I32 flags|I32
sv_type
Ap |GV* |gv_fetchsv|NN SV *name|I32 flags|I32 sv_type
dp |bool |is_gv_magical_sv|NN SV *name|U32 flags
==== //depot/maint-5.8/perl/embed.h#148 (text+w) ====
Index: perl/embed.h
--- perl/embed.h#147~29997~ 2007-01-26 02:30:23.000000000 -0800
+++ perl/embed.h 2007-01-26 05:50:55.000000000 -0800
@@ -1743,6 +1743,10 @@
#ifdef PERL_CORE
#define my_clearenv Perl_my_clearenv
#endif
+#ifndef HAS_STRLCAT
+#endif
+#ifndef HAS_STRLCPY
+#endif
#define gv_fetchpvn_flags Perl_gv_fetchpvn_flags
#define gv_fetchsv Perl_gv_fetchsv
#ifdef PERL_CORE
@@ -3830,6 +3834,10 @@
#ifdef PERL_CORE
#define my_clearenv() Perl_my_clearenv(aTHX)
#endif
+#ifndef HAS_STRLCAT
+#endif
+#ifndef HAS_STRLCPY
+#endif
#define gv_fetchpvn_flags(a,b,c,d) Perl_gv_fetchpvn_flags(aTHX_ a,b,c,d)
#define gv_fetchsv(a,b,c) Perl_gv_fetchsv(aTHX_ a,b,c)
#ifdef PERL_CORE
==== //depot/maint-5.8/perl/ext/File/Glob/bsd_glob.c#4 (text) ====
Index: perl/ext/File/Glob/bsd_glob.c
--- perl/ext/File/Glob/bsd_glob.c#3~25572~ 2005-09-22 09:46:28.000000000
-0700
+++ perl/ext/File/Glob/bsd_glob.c 2007-01-26 05:50:55.000000000 -0800
@@ -1131,9 +1131,9 @@
if (!*str) {
#ifdef MACOS_TRADITIONAL
- strcpy(buf, ":");
+ my_strlcpy(buf, ":", sizeof(buf));
#else
- strcpy(buf, ".");
+ my_strlcpy(buf, ".", sizeof(buf));
#endif
} else {
if (g_Ctoc(str, buf, sizeof(buf)))
==== //depot/maint-5.8/perl/global.sym#56 (text+w) ====
Index: perl/global.sym
--- perl/global.sym#55~29984~ 2007-01-25 14:41:11.000000000 -0800
+++ perl/global.sym 2007-01-26 05:50:55.000000000 -0800
@@ -739,4 +739,6 @@
Perl_my_sprintf
Perl_my_snprintf
Perl_my_vsnprintf
+Perl_my_strlcat
+Perl_my_strlcpy
# ex: set ro:
==== //depot/maint-5.8/perl/mg.c#138 (text) ====
Index: perl/mg.c
--- perl/mg.c#137~30004~ 2007-01-26 03:57:08.000000000 -0800
+++ perl/mg.c 2007-01-26 05:50:55.000000000 -0800
@@ -1078,8 +1078,7 @@
Stat_t sbuf;
int i = 0, j = 0;
- strncpy(eltbuf, s, 255);
- eltbuf[255] = 0;
+ my_strlcpy(eltbuf, s, sizeof(eltbuf));
elt = eltbuf;
do { /* DCL$PATH may be a search list */
while (1) { /* as may dev portion of any element */
==== //depot/maint-5.8/perl/perl.h#141 (text) ====
Index: perl/perl.h
--- perl/perl.h#140~30006~ 2007-01-26 04:19:35.000000000 -0800
+++ perl/perl.h 2007-01-26 05:50:55.000000000 -0800
@@ -1427,6 +1427,18 @@
# define PERL_MY_VSNPRINTF_GUARDED
#endif
+#ifdef HAS_STRLCAT
+# define my_strlcat strlcat
+#else
+# define my_strlcat Perl_my_strlcat
+#endif
+
+#ifdef HAS_STRLCPY
+# define my_strlcpy strlcpy
+#else
+# define my_strlcpy Perl_my_strlcpy
+#endif
+
/* Configure gets this right but the UTS compiler gets it wrong.
-- Hal Morris <[EMAIL PROTECTED]> */
#ifdef UTS
==== //depot/maint-5.8/perl/pp_ctl.c#163 (text) ====
Index: perl/pp_ctl.c
--- perl/pp_ctl.c#162~30006~ 2007-01-26 04:19:35.000000000 -0800
+++ perl/pp_ctl.c 2007-01-26 05:50:55.000000000 -0800
@@ -3411,9 +3411,7 @@
CV* runcv;
U32 seq;
const char * const fakestr = "_<(eval )";
-#ifdef HAS_STRLCPY
const int fakelen = 9 + 1;
-#endif
if (!SvPV_nolen_const(sv))
RETPUSHUNDEF;
@@ -3490,11 +3488,7 @@
if (PERLDB_INTER && was != (I32)PL_sub_generation /* Some subs defined
here. */
&& ret != PL_op->op_next) { /* Successive compilation. */
/* Copy in anything fake and short. */
-#ifdef HAS_STRLCPY
- strlcpy(safestr, fakestr, fakelen);
-#else
- strcpy(safestr, fakestr);
-#endif /* #ifdef HAS_STRLCPY */
+ my_strlcpy(safestr, fakestr, fakelen);
}
return DOCATCH(ret);
}
==== //depot/maint-5.8/perl/pp_sys.c#134 (text) ====
Index: perl/pp_sys.c
--- perl/pp_sys.c#133~30006~ 2007-01-26 04:19:35.000000000 -0800
+++ perl/pp_sys.c 2007-01-26 05:50:55.000000000 -0800
@@ -3518,10 +3518,11 @@
char *s;
PerlIO *myfp;
int anum = 1;
+ Size_t size = strlen(cmd) + (strlen(filename) * 2) + 10;
- Newx(cmdline, strlen(cmd) + (strlen(filename) * 2) + 10, char);
- strcpy(cmdline, cmd);
- strcat(cmdline, " ");
+ Newx(cmdline, size, char);
+ my_strlcpy(cmdline, cmd, size);
+ my_strlcat(cmdline, " ", size);
for (s = cmdline + strlen(cmdline); *filename; ) {
*s++ = '\\';
*s++ = *filename++;
==== //depot/maint-5.8/perl/proto.h#187 (text+w) ====
Index: perl/proto.h
--- perl/proto.h#186~30004~ 2007-01-26 03:57:08.000000000 -0800
+++ perl/proto.h 2007-01-26 05:50:55.000000000 -0800
@@ -2335,6 +2335,14 @@
PERL_CALLCONV OP* Perl_ck_rvconst(pTHX_ OP *o)
__attribute__warn_unused_result__;
+#ifndef HAS_STRLCAT
+PERL_CALLCONV Size_t Perl_my_strlcat(char *dst, const char *src, Size_t
size);
+#endif
+
+#ifndef HAS_STRLCPY
+PERL_CALLCONV Size_t Perl_my_strlcpy(char *dst, const char *src, Size_t
size);
+#endif
+
PERL_CALLCONV OP* Perl_ck_sassign(pTHX_ OP *o)
__attribute__warn_unused_result__;
==== //depot/maint-5.8/perl/util.c#134 (text) ====
Index: perl/util.c
--- perl/util.c#133~30006~ 2007-01-26 04:19:35.000000000 -0800
+++ perl/util.c 2007-01-26 05:50:55.000000000 -0800
@@ -2988,7 +2988,7 @@
if ((strlen(tmpbuf) + strlen(scriptname)
+ MAX_EXT_LEN) >= sizeof tmpbuf)
continue; /* don't search dir with too-long name */
- strcat(tmpbuf, scriptname);
+ my_strlcat(tmpbuf, scriptname, sizeof(tmpbuf));
#else /* !VMS */
#ifdef DOSISH
@@ -3020,11 +3020,11 @@
len = strlen(scriptname);
if (len+MAX_EXT_LEN+1 >= sizeof(tmpbuf))
break;
- /* FIXME? Convert to memcpy */
- cur = strcpy(tmpbuf, scriptname);
+ my_strlcpy(tmpbuf, scriptname, sizeof(tmpbuf));
+ cur = tmpbuf;
}
} while (extidx >= 0 && ext[extidx] /* try an extension? */
- && strcpy(tmpbuf+len, ext[extidx++]));
+ && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) -
len));
#endif
}
#endif
@@ -3084,13 +3084,7 @@
if (len == 2 && tmpbuf[0] == '.')
seen_dot = 1;
#endif
-#ifdef HAS_STRLCAT
- (void)strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
-#else
- /* FIXME? Convert to memcpy by storing previous strlen(scriptname)
- */
- (void)strcpy(tmpbuf + len, scriptname);
-#endif /* #ifdef HAS_STRLCAT */
+ (void)my_strlcpy(tmpbuf + len, scriptname, sizeof(tmpbuf) - len);
#endif /* !VMS */
#ifdef SEARCH_EXTS
@@ -3107,7 +3101,7 @@
#ifdef SEARCH_EXTS
} while ( retval < 0 /* not there */
&& extidx>=0 && ext[extidx] /* try an extension? */
- && strcpy(tmpbuf+len, ext[extidx++])
+ && my_strlcpy(tmpbuf+len, ext[extidx++], sizeof(tmpbuf) -
len)
);
#endif
if (retval < 0)
@@ -5062,6 +5056,39 @@
#endif /* PERL_MICRO */
}
+#ifndef HAS_STRLCAT
+Size_t
+Perl_my_strlcat(char *dst, const char *src, Size_t size)
+{
+ Size_t used, length, copy;
+
+ used = strlen(dst);
+ length = strlen(src);
+ if (size > 0 && used < size - 1) {
+ copy = (length >= size - used) ? size - used - 1 : length;
+ memcpy(dst + used, src, copy);
+ dst[used + copy] = '\0';
+ }
+ return used + length;
+}
+#endif
+
+#ifndef HAS_STRLCPY
+Size_t
+Perl_my_strlcpy(char *dst, const char *src, Size_t size)
+{
+ Size_t length, copy;
+
+ length = strlen(src);
+ if (size > 0) {
+ copy = (length >= size) ? size - 1 : length;
+ memcpy(dst, src, copy);
+ dst[copy] = '\0';
+ }
+ return length;
+}
+#endif
+
/*
* Local variables:
* c-indentation-style: bsd
End of Patch.