Change 27987 by [EMAIL PROTECTED] on 2006/04/28 02:12:03

        Subject: Re: [PATCH] use snprintf/strlcpy/strlcat when useful
        From: Jarkko Hietaniemi <[EMAIL PROTECTED]>
        Date: Tue, 25 Apr 2006 18:23:39 +0300
        Message-ID: <[EMAIL PROTECTED]>

Affected files ...

... //depot/perl/op.c#811 edit
... //depot/perl/perl.h#690 edit
... //depot/perl/perlio.c#318 edit
... //depot/perl/pp_ctl.c#559 edit
... //depot/perl/regcomp.c#424 edit
... //depot/perl/sv.c#1260 edit
... //depot/perl/toke.c#675 edit
... //depot/perl/universal.c#140 edit
... //depot/perl/util.c#558 edit

Differences ...

==== //depot/perl/op.c#811 (text) ====
Index: perl/op.c
--- perl/op.c#810~27962~        2006-04-26 00:53:36.000000000 -0700
+++ perl/op.c   2006-04-27 19:12:03.000000000 -0700
@@ -250,9 +250,13 @@
            p = strchr(name, '\0');
            /* The next block assumes the buffer is at least 205 chars
               long.  At present, it's always at least 256 chars. */
-           if (p-name > 200) {
-               strcpy(name+200, "...");
-               p = name+199;
+           if (p - name > 200) {
+#ifdef HAS_STRLCPY
+               strlcpy(name + 200, "...", 4);
+#else
+               strcpy(name + 200, "...");
+#endif
+               p = name + 199;
            }
            else {
                p[1] = '\0';

==== //depot/perl/perl.h#690 (text) ====
Index: perl/perl.h
--- perl/perl.h#689~27958~      2006-04-25 01:41:38.000000000 -0700
+++ perl/perl.h 2006-04-27 19:12:03.000000000 -0700
@@ -1435,6 +1435,12 @@
 */
 #ifdef SPRINTF_RETURNS_STRLEN
 #  define my_sprintf sprintf
+#  ifdef HAS_SNPRINTF
+#    define USE_SNPRINTF
+#  endif
+#  ifdef HAS_VSNPRINTF
+#    define USE_VSNPRINTF
+#  endif
 #else
 #  define my_sprintf Perl_my_sprintf
 #endif

==== //depot/perl/perlio.c#318 (text) ====
Index: perl/perlio.c
--- perl/perlio.c#317~27962~    2006-04-26 00:53:36.000000000 -0700
+++ perl/perlio.c       2006-04-27 19:12:03.000000000 -0700
@@ -475,7 +475,11 @@
        /* Use fixed buffer as sv_catpvf etc. needs SVs */
        char buffer[1024];
        const STRLEN len = my_sprintf(buffer, "%.40s:%" IVdf " ", s ? s : 
"(none)", (IV) CopLINE(PL_curcop));
+# ifdef USE_VSNPRINTF
+       const STRLEN len2 = vnsprintf(buffer+len, sizeof(buffer) - len, fmt, 
ap);
+# else
        const STRLEN len2 = vsprintf(buffer+len, fmt, ap);
+# endif /* USE_VSNPRINTF */
        PerlLIO_write(PL_perlio_debug_fd, buffer, len + len2);
 #else
        const char *s = CopFILE(PL_curcop);
@@ -5135,7 +5139,11 @@
 PerlIO_vsprintf(char *s, int n, const char *fmt, va_list ap)
 {
     dVAR;
+#ifdef USE_VSNPRINTF
+    const int val = vsnprintf(s, n, fmt, ap);
+#else
     const int val = vsprintf(s, fmt, ap);
+#endif /* #ifdef USE_VSNPRINTF */
     if (n >= 0) {
        if (strlen(s) >= (STRLEN) n) {
            dTHX;

==== //depot/perl/pp_ctl.c#559 (text) ====
Index: perl/pp_ctl.c
--- perl/pp_ctl.c#558~27962~    2006-04-26 00:53:36.000000000 -0700
+++ perl/pp_ctl.c       2006-04-27 19:12:03.000000000 -0700
@@ -831,7 +831,11 @@
            /* Formats aren't yet marked for locales, so assume "yes". */
            {
                STORE_NUMERIC_STANDARD_SET_LOCAL();
+#ifdef USE_SNPRINTF
+               snprintf(t, SvLEN(PL_formtarget) - (t - SvPVX(PL_formtarget)), 
fmt, (int) fieldsize, (int) arg & 255, value);
+#else
                sprintf(t, fmt, (int) fieldsize, (int) arg & 255, value);
+#endif /* ifdef USE_SNPRINTF */
                RESTORE_NUMERIC_STANDARD();
            }
            t += fieldsize;
@@ -2769,8 +2773,13 @@
        len = SvCUR(sv);
     }
     else
+#ifdef USE_SNPRINTF
+       len = snprintf(tmpbuf, sizeof(tbuf), "_<(%.10s_eval %lu)", code,
+                      (unsigned long)++PL_evalseq);
+#else
        len = my_sprintf(tmpbuf, "_<(%.10s_eval %lu)", code,
                         (unsigned long)++PL_evalseq);
+#endif /* ifdef USE_SNPRINTF */
     SAVECOPFILE_FREE(&PL_compiling);
     CopFILE_set(&PL_compiling, tmpbuf+2);
     SAVECOPLINE(&PL_compiling);
@@ -3422,6 +3431,10 @@
     CV* runcv;
     U32 seq;
     HV *saved_hh = NULL;
+    const char * const fakestr = "_<(eval )";
+#ifdef HAS_STRLCPY
+    const int fakelen = 9 + 1;
+#endif
     
     if (PL_op->op_private & OPpEVAL_HAS_HH) {
        saved_hh = (HV*) SvREFCNT_inc(POPs);
@@ -3447,7 +3460,11 @@
        len = SvCUR(temp_sv);
     }
     else
+#ifdef USE_SNPRINTF
+       len = snprintf(tmpbuf, sizeof(tbuf), "_<(eval %lu)", (unsigned 
long)++PL_evalseq);
+#else
        len = my_sprintf(tmpbuf, "_<(eval %lu)", (unsigned long)++PL_evalseq);
+#endif /* ifdef USE_SNPRINTF */
     SAVECOPFILE_FREE(&PL_compiling);
     CopFILE_set(&PL_compiling, tmpbuf+2);
     SAVECOPLINE(&PL_compiling);
@@ -3500,7 +3517,12 @@
     ret = doeval(gimme, NULL, runcv, seq);
     if (PERLDB_INTER && was != (I32)PL_sub_generation /* Some subs defined 
here. */
        && ret != PL_op->op_next) {     /* Successive compilation. */
-       strcpy(safestr, "_<(eval )");   /* Anything fake and short. */
+       /* Copy in anything fake and short. */
+#ifdef HAS_STRLCPY
+       strlcpy(safestr, fakestr, fakelen);
+#else
+       strcpy(safestr, fakestr);
+#endif /* #ifdef HAS_STRLCPY */
     }
     return DOCATCH(ret);
 }

==== //depot/perl/regcomp.c#424 (text) ====
Index: perl/regcomp.c
--- perl/regcomp.c#423~27962~   2006-04-26 00:53:36.000000000 -0700
+++ perl/regcomp.c      2006-04-27 19:12:03.000000000 -0700
@@ -6184,7 +6184,11 @@
            U32 i;
            for (i = 1; i <= rx->nparens; i++) {
                char digits[TYPE_CHARS(long)];
+#ifdef USE_SNPRINTF
+               const STRLEN len = snprintf(digits, sizeof(digits), "%lu", 
(long)i);
+#else
                const STRLEN len = my_sprintf(digits, "%lu", (long)i);
+#endif /* #ifdef USE_SNPRINTF */
                GV *const *const gvp
                    = (GV**)hv_fetch(PL_defstash, digits, len, 0);
 

==== //depot/perl/sv.c#1260 (text) ====
Index: perl/sv.c
--- perl/sv.c#1259~27976~       2006-04-27 05:29:47.000000000 -0700
+++ perl/sv.c   2006-04-27 19:12:03.000000000 -0700
@@ -2656,8 +2656,14 @@
            STRLEN len;
 
            if (SvIOKp(sv)) {
-               len = SvIsUV(sv) ? my_sprintf(tbuf,"%"UVuf, (UV)SvUVX(sv))
-                   : my_sprintf(tbuf,"%"IVdf, (IV)SvIVX(sv));
+               len = SvIsUV(sv)
+#ifdef USE_SNPRINTF
+                   ? snprintf(tbuf, sizeof(tbuf), "%"UVuf, (UV)SvUVX(sv))
+                   : snprintf(tbuf, sizeof(tbuf), "%"IVdf, (IV)SvIVX(sv));
+#else
+                   ? my_sprintf(tbuf, "%"UVuf, (UV)SvUVX(sv))
+                   : my_sprintf(tbuf, "%"IVdf, (IV)SvIVX(sv));
+#endif /* #ifdef USE_SNPRINTF */
            } else {
                Gconvert(SvNVX(sv), NV_DIG, 0, tbuf);
                len = strlen(tbuf);
@@ -9260,8 +9266,13 @@
                 * --jhi */
 #if defined(HAS_LONG_DOUBLE)
                elen = ((intsize == 'q')
+# ifdef USE_SNPRINTF
+                       ? snprintf(PL_efloatbuf, PL_efloatsize, ptr, nv)
+                       : snprintf(PL_efloatbuf, PL_efloatsize, ptr, 
(double)nv));
+# else
                        ? my_sprintf(PL_efloatbuf, ptr, nv)
                        : my_sprintf(PL_efloatbuf, ptr, (double)nv));
+# endif /* #ifdef USE_SNPRINTF */
 #else
                elen = my_sprintf(PL_efloatbuf, ptr, nv);
 #endif

==== //depot/perl/toke.c#675 (text) ====
Index: perl/toke.c
--- perl/toke.c#674~27962~      2006-04-26 00:53:36.000000000 -0700
+++ perl/toke.c 2006-04-27 19:12:03.000000000 -0700
@@ -5975,7 +5975,11 @@
                if (!PL_in_my_stash) {
                    char tmpbuf[1024];
                    PL_bufptr = s;
+#ifdef USE_SNPRINTF
+                   snprintf(tmpbuf, sizeof(tmpbuf), "No such class %.1000s", 
PL_tokenbuf);
+#else
                    sprintf(tmpbuf, "No such class %.1000s", PL_tokenbuf);
+#endif /* #ifdef USE_SNPRINTF */
                    yyerror(tmpbuf);
                }
 #ifdef PERL_MAD

==== //depot/perl/universal.c#140 (text) ====
Index: perl/universal.c
--- perl/universal.c#139~27542~ 2006-03-19 08:38:11.000000000 -0800
+++ perl/universal.c    2006-04-27 19:12:03.000000000 -0700
@@ -619,7 +619,11 @@
            if ( SvNOK(ver) ) /* may get too much accuracy */
            {
                char tbuf[64];
-               const STRLEN len = my_sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
+#ifdef USE_SNPRINTF
+               const STRLEN len = snprintf(tbuf, sizeof(tbuf), "%.9"NVgf, 
SvNVX(ver));
+#else
+               const STRLEN len = my_sprintf(tbuf, "%.9"NVgf, SvNVX(ver));
+#endif /* #ifdef USE_SNPRINTF  */
                version = savepvn(tbuf, len);
            }
            else

==== //depot/perl/util.c#558 (text) ====
Index: perl/util.c
--- perl/util.c#557~27962~      2006-04-26 00:53:36.000000000 -0700
+++ perl/util.c 2006-04-27 19:12:03.000000000 -0700
@@ -3115,9 +3115,13 @@
            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 */
 #endif  /* !VMS */
 
 #ifdef SEARCH_EXTS
@@ -4290,7 +4294,11 @@
     if ( SvNOK(ver) ) /* may get too much accuracy */ 
     {
        char tbuf[64];
-       const STRLEN len = my_sprintf(tbuf,"%.9"NVgf, SvNVX(ver));
+#ifdef USE_SNPRINTF
+       const STRLEN len = snprintf(tbuf, sizeof(tbuf), "%.9"NVgf, SvNVX(ver));
+#else
+       const STRLEN len = my_sprintf(tbuf, "%.9"NVgf, SvNVX(ver));
+#endif /* #ifdef USE_SNPRINTF */
        version = savepvn(tbuf, len);
     }
 #ifdef SvVOK
End of Patch.

Reply via email to