From 1c01bcdc7b0c43aeba4533005809bce17e08ce77 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:09:49 +0100 Subject: [PATCH 1/5] ext/standard/pack: Inline constant single use variables They serve no purpose and are just confusing --- ext/standard/pack.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index ec30be436741d..6ed9d8ac591fb 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -917,7 +917,6 @@ PHP_FUNCTION(unpack) } case 'A': { /* A will strip any trailing whitespace */ - char padn = '\0'; char pads = ' '; char padt = '\t'; char padc = '\r'; char padl = '\n'; zend_long len = inputlen - inputpos; /* Remaining string */ /* If size was given take minimum of len and size */ @@ -929,11 +928,11 @@ PHP_FUNCTION(unpack) /* Remove trailing white space and nulls chars from unpacked data */ while (--len >= 0) { - if (input[inputpos + len] != padn - && input[inputpos + len] != pads - && input[inputpos + len] != padt - && input[inputpos + len] != padc - && input[inputpos + len] != padl + if (input[inputpos + len] != '\0' + && input[inputpos + len] != ' ' + && input[inputpos + len] != '\t' + && input[inputpos + len] != '\r' + && input[inputpos + len] != '\n' ) break; } @@ -945,7 +944,6 @@ PHP_FUNCTION(unpack) /* New option added for Z to remain in-line with the Perl implementation */ case 'Z': { /* Z will strip everything after the first null character */ - char pad = '\0'; zend_long s, len = inputlen - inputpos; /* Remaining string */ @@ -958,7 +956,7 @@ PHP_FUNCTION(unpack) /* Remove everything after the first null */ for (s=0 ; s < len ; s++) { - if (input[inputpos + s] == pad) + if (input[inputpos + s] == '\0') break; } len = s; From 55b17923fbcfc2867c708562b066566e849fd378 Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:14:22 +0100 Subject: [PATCH 2/5] ext/standard/pack: Remove useless casts And use char instead of widening to int for no reason --- ext/standard/pack.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index 6ed9d8ac591fb..f1c4c96655eb1 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -274,7 +274,7 @@ PHP_FUNCTION(pack) } /* Handle special arg '*' for all codes and check argv overflows */ - switch ((int) code) { + switch (code) { /* Never uses any args */ case 'x': case 'X': @@ -380,10 +380,10 @@ PHP_FUNCTION(pack) /* Calculate output length and upper bound while processing*/ for (i = 0; i < formatcount; i++) { - int code = (int) formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; - switch ((int) code) { + switch (code) { case 'h': case 'H': INC_OUTPUTPOS((arg + (arg % 2)) / 2,1) /* 4 bit per arg */ @@ -463,10 +463,10 @@ PHP_FUNCTION(pack) /* Do actual packing */ for (i = 0; i < formatcount; i++) { - int code = (int) formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; - switch ((int) code) { + switch (code) { case 'a': case 'A': case 'Z': { @@ -632,7 +632,7 @@ PHP_FUNCTION(pack) case 'd': { while (arg-- > 0) { - double v = (double) zval_get_double(&argv[currentarg++]); + double v = zval_get_double(&argv[currentarg++]); memcpy(&ZSTR_VAL(output)[outputpos], &v, sizeof(v)); outputpos += sizeof(v); } @@ -642,7 +642,7 @@ PHP_FUNCTION(pack) case 'e': { /* pack little endian double */ while (arg-- > 0) { - double v = (double) zval_get_double(&argv[currentarg++]); + double v = zval_get_double(&argv[currentarg++]); php_pack_copy_double(1, &ZSTR_VAL(output)[outputpos], v); outputpos += sizeof(v); } @@ -652,7 +652,7 @@ PHP_FUNCTION(pack) case 'E': { /* pack big endian double */ while (arg-- > 0) { - double v = (double) zval_get_double(&argv[currentarg++]); + double v = zval_get_double(&argv[currentarg++]); php_pack_copy_double(0, &ZSTR_VAL(output)[outputpos], v); outputpos += sizeof(v); } @@ -784,7 +784,7 @@ PHP_FUNCTION(unpack) if (namelen > 200) namelen = 200; - switch ((int) type) { + switch (type) { /* Never use any input */ case 'X': size = -1; @@ -899,7 +899,7 @@ PHP_FUNCTION(unpack) real_name = zend_string_concat2(name, namelen, res, digits); } - switch ((int) type) { + switch (type) { case 'a': { /* a will not strip any trailing whitespace or null padding */ zend_long len = inputlen - inputpos; /* Remaining string */ From 06feab6b155e7f8daa34b1df683afa3c8a81416d Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:20:04 +0100 Subject: [PATCH 3/5] ext/standard/pack: Reduce scope of variable --- ext/standard/pack.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index f1c4c96655eb1..43832b3d11f6b 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -737,7 +737,6 @@ PHP_FUNCTION(unpack) while (formatlen-- > 0) { char type = *(format++); - char c; int repetitions = 1, argb; char *name; int namelen; @@ -745,7 +744,7 @@ PHP_FUNCTION(unpack) /* Handle format arguments if any */ if (formatlen > 0) { - c = *format; + char c = *format; if (c >= '0' && c <= '9') { errno = 0; From 93c4002e5cae24cb461ea0989ecdf5b41a7cbcfb Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 13:24:51 +0100 Subject: [PATCH 4/5] ext/standard/pack: Remove unused header includes --- ext/standard/pack.c | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index 43832b3d11f6b..89ae5be5f112a 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -16,30 +16,10 @@ #include "php.h" -#include #include #include #include -#include -#include -#ifdef PHP_WIN32 -#define O_RDONLY _O_RDONLY -#include "win32/param.h" -#else -#include -#endif #include "pack.h" -#ifdef HAVE_PWD_H -#ifdef PHP_WIN32 -#include "win32/pwd.h" -#else -#include -#endif -#endif -#include "fsock.h" -#ifdef HAVE_NETINET_IN_H -#include -#endif #define INC_OUTPUTPOS(a,b) \ if ((a) < 0 || ((INT_MAX - outputpos)/((int)b)) < (a)) { \ From 95e77b907f090f473777cfa36ce4ab9f683dc9fd Mon Sep 17 00:00:00 2001 From: Gina Peter Banyard Date: Tue, 10 Jun 2025 15:14:07 +0100 Subject: [PATCH 5/5] [skip ci] ext/standard/pack: Fix indentation to use tabs --- ext/standard/pack.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/standard/pack.c b/ext/standard/pack.c index 89ae5be5f112a..712321471d704 100644 --- a/ext/standard/pack.c +++ b/ext/standard/pack.c @@ -360,7 +360,7 @@ PHP_FUNCTION(pack) /* Calculate output length and upper bound while processing*/ for (i = 0; i < formatcount; i++) { - char code = formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; switch (code) { @@ -443,7 +443,7 @@ PHP_FUNCTION(pack) /* Do actual packing */ for (i = 0; i < formatcount; i++) { - char code = formatcodes[i]; + char code = formatcodes[i]; int arg = formatargs[i]; switch (code) {