From ff5402a378e7718bead0627f66469a5219c56150 Mon Sep 17 00:00:00 2001 From: Philip Hazel Date: Sat, 23 Apr 2022 17:34:35 +0100 Subject: [PATCH] Add some casts and other tidies to pcre2test formatting of size_t values --- ChangeLog | 5 +++++ src/pcre2test.c | 16 +++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/ChangeLog b/ChangeLog index 240200f..d0c1781 100644 --- a/ChangeLog +++ b/ChangeLog @@ -11,6 +11,11 @@ Alpine Linux). 2. Merged patch from @carenas (GitHub #110) for pthreads support in CMake. +3. SSF scorecards grumbled about possible overflow in an expression in +pcre2test. It never would have overflowed in practice, but some casts have been +added and at the some time there's been some tidying of fprints that output +size_t values. + Version 10.40 15-April-2022 --------------------------- diff --git a/src/pcre2test.c b/src/pcre2test.c index ea52a20..2aabcce 100644 --- a/src/pcre2test.c +++ b/src/pcre2test.c @@ -927,7 +927,6 @@ static BOOL jit_was_used; static BOOL restrict_for_perl_test = FALSE; static BOOL show_memory = FALSE; -static int code_unit_size; /* Bytes */ static int jitrc; /* Return from JIT compile */ static int test_mode = DEFAULT_TEST_MODE; static int timeit = 0; @@ -937,6 +936,7 @@ clock_t total_compile_time = 0; clock_t total_jit_compile_time = 0; clock_t total_match_time = 0; +static uint32_t code_unit_size; /* Bytes */ static uint32_t dfa_matched; static uint32_t forbid_utf = 0; static uint32_t maxlookbehind; @@ -4307,12 +4307,18 @@ if (test_mode == PCRE32_MODE) cblock_size = sizeof(pcre2_real_code_32); (void)pattern_info(PCRE2_INFO_SIZE, &size, FALSE); (void)pattern_info(PCRE2_INFO_NAMECOUNT, &name_count, FALSE); (void)pattern_info(PCRE2_INFO_NAMEENTRYSIZE, &name_entry_size, FALSE); -fprintf(outfile, "Memory allocation (code space): %d\n", - (int)(size - name_count*name_entry_size*code_unit_size - cblock_size)); + +/* The uint32_t variables are cast before multiplying to stop code analyzers +grumbling about potential overflow. */ + +fprintf(outfile, "Memory allocation (code space): %" SIZ_FORM "\n", size - + (size_t)name_count * (size_t)name_entry_size * (size_t)code_unit_size - + cblock_size); + if (pat_patctl.jit != 0) { (void)pattern_info(PCRE2_INFO_JITSIZE, &size, FALSE); - fprintf(outfile, "Memory allocation (JIT code): %d\n", (int)size); + fprintf(outfile, "Memory allocation (JIT code): %" SIZ_FORM "\n", size); } } @@ -4327,7 +4333,7 @@ show_framesize(void) { size_t frame_size; (void)pattern_info(PCRE2_INFO_FRAMESIZE, &frame_size, FALSE); -fprintf(outfile, "Frame size for pcre2_match(): %d\n", (int)frame_size); +fprintf(outfile, "Frame size for pcre2_match(): %" SIZ_FORM "\n", frame_size); }