From fae713c2098e747acae4f1cd194f72d4a5481fe2 Mon Sep 17 00:00:00 2001 From: Lukas Mai Date: Tue, 18 Mar 2025 10:44:01 +0100 Subject: [PATCH 1/2] eliminate more trivial format strings ("%s", "...") As a follow-up to commit 71d1d453e7, turn more trivial format strings (i.e. "%s" followed by a string literal) into just that string literal. - dump.c: String literals are more efficiently appended using sv_catpvs; C strings using sv_patv. No need to invoke the entire sv_catpvf machinery to parse a static "format string" of length 1. - malloc.c: Disentangle some warning messages and make them properly match their (previously orphaned) perldiag entries again. - os2/perlrexx.c: Turn sprintf(x, "...") into strcpy(x, "..."). --- dump.c | 8 ++++---- malloc.c | 30 +++++++++++++++++------------- os2/perlrexx.c | 6 +++--- pod/perldiag.pod | 4 ++-- t/porting/diag.t | 2 -- 5 files changed, 26 insertions(+), 24 deletions(-) diff --git a/dump.c b/dump.c index 4e0cbc5302ba..6fa955000ee7 100644 --- a/dump.c +++ b/dump.c @@ -540,14 +540,14 @@ Perl_sv_peek(pTHX_ SV *sv) } } if (is_tmp || SvREFCNT(sv) > 1 || SvPADTMP(sv)) { - sv_catpvf(t, "<"); + sv_catpvs(t, "<"); if (SvREFCNT(sv) > 1) sv_catpvf(t, "%" UVuf, (UV)SvREFCNT(sv)); if (SvPADTMP(sv)) - sv_catpvf(t, "%s", "P"); + sv_catpvs(t, "P"); if (is_tmp) - sv_catpvf(t, "%s", SvTEMP(t) ? "T" : "t"); - sv_catpvf(t, ">"); + sv_catpv(t, SvTEMP(t) ? "T" : "t"); + sv_catpvs(t, ">"); } } diff --git a/malloc.c b/malloc.c index b2841fc067a8..880d6a9be5b5 100644 --- a/malloc.c +++ b/malloc.c @@ -1704,7 +1704,7 @@ morecore(int bucket) #endif if (bucket == sizeof(MEM_SIZE)*8*BUCKETS_PER_POW2) { MALLOC_UNLOCK; - croak2("%s", "Out of memory during ridiculously large request"); + croak2("Out of memory during ridiculously large request"); } if (bucket > max_bucket) max_bucket = bucket; @@ -1842,16 +1842,20 @@ Perl_mfree(Malloc_t where) #ifdef RCHECK { dTHX; - if (!PERL_IS_ALIVE || !PL_curcop) - Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "%s free() ignored (RMAGIC, PERL_CORE)", - ovp->ov_rmagic == RMAGIC - 1 ? - "Duplicate" : "Bad"); + if (!PERL_IS_ALIVE || !PL_curcop) { + if (ovp->ov_rmagic == RMAGIC - 1) + Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), + "Duplicate free() ignored (%s)", "RMAGIC, PERL_CORE"); + else + Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), + "Bad free() ignored (%s)", "RMAGIC, PERL_CORE"); + } } #else { dTHX; if (!PERL_IS_ALIVE || !PL_curcop) - Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "%s", "Bad free() ignored (PERL_CORE)"); + Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad free() ignored (%s)", "PERL_CORE"); } #endif return; /* sanity */ @@ -1947,18 +1951,18 @@ Perl_realloc(void *mp, size_t nbytes) #ifdef RCHECK { dTHX; - if (!PERL_IS_ALIVE || !PL_curcop) - Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "%srealloc() %signored", - (ovp->ov_rmagic == RMAGIC - 1 ? "" : "Bad "), - ovp->ov_rmagic == RMAGIC - 1 - ? "of freed memory " : ""); + if (!PERL_IS_ALIVE || !PL_curcop) { + if (ovp->ov_rmagic == RMAGIC - 1) + Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "realloc() of freed memory ignored"); + else + Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad realloc() ignored"); + } } #else { dTHX; if (!PERL_IS_ALIVE || !PL_curcop) - Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "%s", - "Bad realloc() ignored"); + Perl_ck_warner_d(aTHX_ packWARN(WARN_MALLOC), "Bad realloc() ignored"); } #endif return NULL; /* sanity */ diff --git a/os2/perlrexx.c b/os2/perlrexx.c index 267805c84198..c129940c350c 100644 --- a/os2/perlrexx.c +++ b/os2/perlrexx.c @@ -135,7 +135,7 @@ ULONG PERL (PCSZ name, LONG rargc, const RXSTRING *rargv, ret = 1; else { ret = 0; - sprintf(retstr->strptr, "%s", "ok"); + strcpy(retstr->strptr, "ok"); retstr->strlength = strlen (retstr->strptr); } PERL_SYS_TERM1(0); @@ -162,7 +162,7 @@ ULONG PERLTERM (PCSZ name, LONG rargc, const RXSTRING *rargv, perl_free(my_perl); my_perl = 0; - sprintf(retstr->strptr, "%s", "ok"); + strcpy(retstr->strptr, "ok"); retstr->strlength = strlen (retstr->strptr); return 0; } @@ -176,7 +176,7 @@ ULONG PERLINIT (PCSZ name, LONG rargc, const RXSTRING *rargv, if (!init_perl(1)) return 1; - sprintf(retstr->strptr, "%s", "ok"); + strcpy(retstr->strptr, "ok"); retstr->strlength = strlen (retstr->strptr); return 0; } diff --git a/pod/perldiag.pod b/pod/perldiag.pod index e03f723ca165..6d337928b8cd 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -476,7 +476,7 @@ most likely an unexpected right brace '}'. symbol has no filehandle associated with it. Perhaps you didn't do an open(), or did it in another package. -=item Bad free() ignored +=item Bad free() ignored (%s) (S malloc) An internal routine called free() on something that had never been malloc()ed in the first place. Mandatory, but can be disabled by @@ -2324,7 +2324,7 @@ See L. (F) Your machine doesn't support dump/undump. -=item Duplicate free() ignored +=item Duplicate free() ignored (%s) (S malloc) An internal routine called free() on something that had already been freed. diff --git a/t/porting/diag.t b/t/porting/diag.t index 4c9d8eab784c..742f9a2d9aee 100644 --- a/t/porting/diag.t +++ b/t/porting/diag.t @@ -701,7 +701,6 @@ setnetent not implemented! setprotoent not implemented! set %s %p %p %p setservent not implemented! -%s free() ignored (RMAGIC, PERL_CORE) %s has too many errors. SIG%s handler "%s" not defined. %s in %s @@ -709,7 +708,6 @@ Size magic not implemented %s: name `%s' too long %s not implemented! %s number > %s non-portable -%srealloc() %signored %s on %s %s %s: %s Starting Full Screen process with flag=%d, mytype=%d From 52782a0531819ec363a59ccb4c3bdb0c644e147e Mon Sep 17 00:00:00 2001 From: Lukas Mai Date: Tue, 18 Mar 2025 10:55:30 +0100 Subject: [PATCH 2/2] malloc.c: silence "unused result" compiler warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before: malloc.c: In function ‘getpages’: malloc.c:273:34: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 273 | # define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2)) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ malloc.c:1546:21: note: in expansion of macro ‘fatalcroak’ 1546 | fatalcroak("panic: Off-page sbrk\n"); | ^~~~~~~~~~ malloc.c:273:34: warning: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Wunused-result] 273 | # define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2)) | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ malloc.c:1585:13: note: in expansion of macro ‘fatalcroak’ 1585 | fatalcroak("Misalignment of sbrk()\n"); | ^~~~~~~~~~ gcc -c -DPERL_CORE -fwrapv -fno-strict-aliasing -pipe -fstack-protector-strong -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -std=c99 -O2 -Wall -Werror=pointer-arith -Werror=vla -Wextra -Wno-long-long -Wno-declaration-after-statement -Wc++-compat -Wwrite-strings -Wno-use-after-free universal.c (The "make depend" comment was overlooked in commit b9e5552c5b.) --- malloc.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/malloc.c b/malloc.c index 880d6a9be5b5..91f282e3eb11 100644 --- a/malloc.c +++ b/malloc.c @@ -269,9 +269,9 @@ # define MALLOC_UNLOCK MUTEX_UNLOCK(&PL_malloc_mutex) #endif -# ifndef fatalcroak /* make depend */ -# define fatalcroak(mess) (write(2, (mess), strlen(mess)), exit(2)) -# endif +#ifndef fatalcroak +# define fatalcroak(mess) STMT_START { PERL_UNUSED_RESULT(write(2, (mess), strlen(mess))); exit(2); } STMT_END +#endif #ifdef DEBUGGING # undef DEBUG_m