From: Victor Julien Date: Thu, 13 Jul 2017 07:57:40 +0000 (+0200) Subject: gcc7: fix format-truncation warnings in runmodes X-Git-Tag: suricata-4.0.0-rc2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=5b84c01cd3567ac531413c0f4bec9bd59223b011;p=thirdparty%2Fsuricata.git gcc7: fix format-truncation warnings in runmodes Example: util-runmodes.c: In function ‘RunModeSetIPSAutoFp’: util-runmodes.c:496:40: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=] snprintf(qname, sizeof(qname), "pickup%d", thread+1); ^~~~~~~~~~ util-runmodes.c:496:9: note: ‘snprintf’ output between 8 and 17 bytes into a destination of size16 snprintf(qname, sizeof(qname), "pickup%d", thread+1); ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Solved by reducing 'thread' to a uint16_t and limiting the max thread count to 1024. --- diff --git a/src/runmode-erf-file.c b/src/runmode-erf-file.c index a5354bb422..5002030976 100644 --- a/src/runmode-erf-file.c +++ b/src/runmode-erf-file.c @@ -118,7 +118,7 @@ int RunModeErfFileAutoFp(void) char qname[TM_QUEUE_NAME_MAX]; uint16_t cpu = 0; char *queues = NULL; - int thread; + uint16_t thread; RunModeInitialize(); @@ -145,6 +145,8 @@ int RunModeErfFileAutoFp(void) thread_max = ncpus * threading_detect_ratio; if (thread_max < 1) thread_max = 1; + if (thread_max > 1024) + thread_max = 1024; queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); if (queues == NULL) { @@ -189,9 +191,9 @@ int RunModeErfFileAutoFp(void) exit(EXIT_FAILURE); } - for (thread = 0; thread < thread_max; thread++) { - snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread+1); - snprintf(qname, sizeof(qname), "pickup%d", thread+1); + for (thread = 0; thread < (uint16_t)thread_max; thread++) { + snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread+1); + snprintf(qname, sizeof(qname), "pickup%u", thread+1); SCLogDebug("tname %s, qname %s", tname, qname); diff --git a/src/runmode-pcap-file.c b/src/runmode-pcap-file.c index 29de79dc8f..ae5cb84c71 100644 --- a/src/runmode-pcap-file.c +++ b/src/runmode-pcap-file.c @@ -151,7 +151,7 @@ int RunModeFilePcapAutoFp(void) char qname[TM_QUEUE_NAME_MAX]; uint16_t cpu = 0; char *queues = NULL; - int thread; + uint16_t thread; RunModeInitialize(); @@ -180,6 +180,8 @@ int RunModeFilePcapAutoFp(void) thread_max = ncpus * threading_detect_ratio; if (thread_max < 1) thread_max = 1; + if (thread_max > 1024) + thread_max = 1024; queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); if (queues == NULL) { @@ -222,9 +224,9 @@ int RunModeFilePcapAutoFp(void) exit(EXIT_FAILURE); } - for (thread = 0; thread < thread_max; thread++) { + for (thread = 0; thread < (uint16_t)thread_max; thread++) { snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread+1); - snprintf(qname, sizeof(qname), "pickup%d", thread+1); + snprintf(qname, sizeof(qname), "pickup%u", thread+1); SCLogDebug("tname %s, qname %s", tname, qname); SCLogDebug("Assigning %s affinity to cpu %u", tname, cpu); diff --git a/src/util-runmodes.c b/src/util-runmodes.c index caaff71ec4..90fe21d34e 100644 --- a/src/util-runmodes.c +++ b/src/util-runmodes.c @@ -95,7 +95,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, char tname[TM_THREAD_NAME_MAX]; char qname[TM_QUEUE_NAME_MAX]; char *queues = NULL; - int thread = 0; + uint16_t thread = 0; + /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); int nlive = LiveGetDeviceCount(); @@ -105,6 +106,8 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, thread_max = ncpus * threading_detect_ratio; if (thread_max < 1) thread_max = 1; + if (thread_max > 1024) + thread_max = 1024; queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); if (queues == NULL) { @@ -224,9 +227,9 @@ int RunModeSetLiveCaptureAutoFp(ConfigIfaceParserFunc ConfigParser, } } - for (thread = 0; thread < thread_max; thread++) { - snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread+1); - snprintf(qname, sizeof(qname), "pickup%d", thread+1); + for (thread = 0; thread < (uint16_t)thread_max; thread++) { + snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread+1); + snprintf(qname, sizeof(qname), "pickup%u", thread+1); SCLogDebug("tname %s, qname %s", tname, qname); @@ -429,7 +432,7 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser, TmModule *tm_module ; const char *cur_queue = NULL; char *queues = NULL; - int thread; + uint16_t thread; /* Available cpus */ uint16_t ncpus = UtilCpuGetNumProcessorsOnline(); @@ -441,6 +444,8 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser, thread_max = ncpus * threading_detect_ratio; if (thread_max < 1) thread_max = 1; + if (thread_max > 1024) + thread_max = 1024; queues = RunmodeAutoFpCreatePickupQueuesString(thread_max); if (queues == NULL) { @@ -488,9 +493,9 @@ int RunModeSetIPSAutoFp(ConfigIPSParserFunc ConfigParser, } } - for (thread = 0; thread < thread_max; thread++) { - snprintf(tname, sizeof(tname), "%s#%02d", thread_name_workers, thread+1); - snprintf(qname, sizeof(qname), "pickup%d", thread+1); + for (thread = 0; thread < (uint16_t)thread_max; thread++) { + snprintf(tname, sizeof(tname), "%s#%02u", thread_name_workers, thread+1); + snprintf(qname, sizeof(qname), "pickup%u", thread+1); SCLogDebug("tname %s, qname %s", tname, qname);