detect/mpm: address format truncation warnings

detect-engine-mpm.c: In function ‘BuildBasicPname’:
detect-engine-mpm.c:197:43: error: ‘snprintf’ output may be truncated before the last format character [-Werror=format-truncation=]
  197 |         snprintf(pname, sizeof(pname), "%s", name);
      |                                           ^
detect-engine-mpm.c:197:9: note: ‘snprintf’ output 1 or more bytes (assuming 2) into a destination of size 1
  197 |         snprintf(pname, sizeof(pname), "%s", name);
      |         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
detect-engine-mpm.c: In function ‘AppendTransformsToPname’:
detect-engine-mpm.c:231:61: error: ‘%s’ directive output may be truncated writing up to 1023 bytes into a region of size 7 [-Werror=format-truncation=]
  231 |             snprintf(xforms_print, sizeof(xforms_print), " (%s)", xforms);
      |                                                             ^~    ~~~~~~
detect-engine-mpm.c:231:13: note: ‘snprintf’ output between 4 and 1027 bytes into a destination of size 9
  231 |             snprintf(xforms_print, sizeof(xforms_print), " (%s)", xforms);
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ticket: #7905.
pull/13926/head
Victor Julien 2 months ago committed by Victor Julien
parent 103bad19bb
commit 5564838c22

@ -194,7 +194,7 @@ static void BuildBasicPname(char *out, const size_t out_size, const char *name,
if (strlen(name) >= name_space) {
ShortenString(name, pname, name_space, '~');
} else {
snprintf(pname, sizeof(pname), "%s", name);
strlcpy(pname, name, sizeof(pname));
}
snprintf(out, out_size, "%s#%u", pname, id);
}
@ -210,22 +210,25 @@ static void AppendTransformsToPname(
if (transforms == NULL || transforms->cnt == 0)
return;
/* create comma separated string of the names of the
* transforms and then shorten it if necessary. Finally
* use it to construct the 'profile' name for the engine */
char xforms[1024] = "";
for (int i = 0; i < transforms->cnt; i++) {
char ttstr[64];
(void)snprintf(ttstr, sizeof(ttstr), "%s,",
sigmatch_table[transforms->transforms[i].transform].name);
strlcat(xforms, ttstr, sizeof(xforms));
}
xforms[strlen(xforms) - 1] = '\0';
ssize_t left = (ssize_t)out_size - (ssize_t)strlen(out) - (ssize_t)4;
SCLogDebug("left %d '%s' %d", (int)left, xforms, (int)strlen(xforms));
/* only append xform if we can add least 5 chars */
if (left >= 5) {
/* create comma separated string of the names of the
* transforms and then shorten it if necessary. Finally
* use it to construct the 'profile' name for the engine */
char xforms[left + 1];
memset(xforms, 0, left + 1);
for (int i = 0; i < transforms->cnt; i++) {
char ttstr[64];
(void)snprintf(ttstr, sizeof(ttstr), "%s,",
sigmatch_table[transforms->transforms[i].transform].name);
strlcat(xforms, ttstr, sizeof(xforms));
}
if (strlen(xforms) == 0)
return;
xforms[strlen(xforms) - 1] = '\0';
SCLogDebug("left %d '%s' %d", (int)left, xforms, (int)strlen(xforms));
char xforms_print[out_size];
if ((size_t)left >= strlen(xforms)) {
snprintf(xforms_print, sizeof(xforms_print), " (%s)", xforms);

Loading…
Cancel
Save