log/pcap: improve path handling

Use proper path merging.

Address format truncation warnings.

log-pcap.c: In function 'PcapLogInitRingBuffer':
log-pcap.c:970:13: warning: '%s' directive output may be truncated writing up to 255 bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
  970 |         if (snprintf(path, PATH_MAX, "%s/%s", pattern, entry->d_name) == PATH_MAX)
      |             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log-pcap.c:970:13: note: 'snprintf' output between 2 and 4352 bytes into a destination of size 4096
log-pcap.c: In function 'PcapLogOpenFileCtx':
log-pcap.c:1796:56: warning: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
 1796 |                 ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
      |                                                        ^~
log-pcap.c:1796:52: note: assuming directive output of 1 byte
 1796 |                 ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
      |                                                    ^~~~~~~~~~~~
log-pcap.c:1796:52: note: using the range [0, 4294967295] for directive argument
log-pcap.c:1796:52: note: directive argument in the range [0, 1048575]
log-pcap.c:1796:52: note: assuming directive output of 1 byte
log-pcap.c:1796:23: note: 'snprintf' output 8 or more bytes (assuming 4111) into a destination of size 4096
 1796 |                 ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1797 |                         pl->prefix, pl->thread_number, (uint32_t)SCTIME_SECS(ts),
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1798 |                         (uint32_t)SCTIME_USECS(ts), pl->suffix);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log-pcap.c:1793:56: warning: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
 1793 |                 ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 "%s", pl->dir, pl->prefix,
      |                                                        ^~
log-pcap.c:1793:52: note: assuming directive output of 1 byte
 1793 |                 ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 "%s", pl->dir, pl->prefix,
      |                                                    ^~~~~~~~~~~~
log-pcap.c:1793:52: note: using the range [0, 4294967295] for directive argument
log-pcap.c:1793:52: note: assuming directive output of 1 byte
log-pcap.c:1793:23: note: 'snprintf' output 6 or more bytes (assuming 4103) into a destination of size 4096
 1793 |                 ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 "%s", pl->dir, pl->prefix,
      |                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1794 |                         pl->thread_number, (uint32_t)SCTIME_SECS(ts), pl->suffix);
      |                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log-pcap.c:1738:52: warning: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
 1738 |             ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
      |                                                    ^~
log-pcap.c:1738:48: note: assuming directive output of 1 byte
 1738 |             ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
      |                                                ^~~~~~~~~
log-pcap.c:1738:48: note: using the range [0, 4294967295] for directive argument
log-pcap.c:1738:48: note: directive argument in the range [0, 1048575]
log-pcap.c:1738:48: note: assuming directive output of 1 byte
log-pcap.c:1738:19: note: 'snprintf' output 6 or more bytes (assuming 4109) into a destination of size 4096
 1738 |             ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1739 |                     pl->prefix, (uint32_t)SCTIME_SECS(ts), (uint32_t)SCTIME_USECS(ts), pl->suffix);
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
log-pcap.c:1735:52: warning: '%s' directive output may be truncated writing likely 1 or more bytes into a region of size between 0 and 4095 [-Wformat-truncation=]
 1735 |             ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 "%s", pl->dir, pl->prefix,
      |                                                    ^~
log-pcap.c:1735:48: note: assuming directive output of 1 byte
 1735 |             ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 "%s", pl->dir, pl->prefix,
      |                                                ^~~~~~~~~
log-pcap.c:1735:48: note: using the range [0, 4294967295] for directive argument
log-pcap.c:1735:48: note: assuming directive output of 1 byte
log-pcap.c:1735:19: note: 'snprintf' output 4 or more bytes (assuming 4101) into a destination of size 4096
 1735 |             ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 "%s", pl->dir, pl->prefix,
      |                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 1736 |                     (uint32_t)SCTIME_SECS(ts), pl->suffix);
      |                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ticket: #7905.
pull/13926/head
Victor Julien 2 months ago committed by Victor Julien
parent 3d0d7b740e
commit 98730c2c43

@ -967,7 +967,7 @@ static TmEcode PcapLogInitRingBuffer(PcapLogData *pl)
goto fail;
}
char path[PATH_MAX];
if (snprintf(path, PATH_MAX, "%s/%s", pattern, entry->d_name) == PATH_MAX)
if (PathMerge(path, sizeof(path), pattern, entry->d_name) < 0)
goto fail;
if ((pf->filename = SCStrdup(path)) == NULL) {
@ -1705,18 +1705,18 @@ static void PcapLogFileDeInitCtx(OutputCtx *output_ctx)
*/
static int PcapLogOpenFileCtx(PcapLogData *pl)
{
char *filename = NULL;
char *path = NULL;
PCAPLOG_PROFILE_START;
if (pl->filename != NULL)
filename = pl->filename;
path = pl->filename;
else {
filename = SCMalloc(PATH_MAX);
if (unlikely(filename == NULL)) {
path = SCMalloc(PATH_MAX);
if (unlikely(path == NULL)) {
return -1;
}
pl->filename = filename;
pl->filename = path;
}
/** get the time so we can have a filename with seconds since epoch */
@ -1728,15 +1728,16 @@ static int PcapLogOpenFileCtx(PcapLogData *pl)
return -1;
}
char file[PATH_MAX] = "";
if (pl->mode == LOGMODE_NORMAL) {
int ret;
/* create the filename to use */
if (pl->timestamp_format == TS_FORMAT_SEC) {
ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 "%s", pl->dir, pl->prefix,
ret = snprintf(file, sizeof(file), "%s.%" PRIu32 "%s", pl->prefix,
(uint32_t)SCTIME_SECS(ts), pl->suffix);
} else {
ret = snprintf(filename, PATH_MAX, "%s/%s.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
pl->prefix, (uint32_t)SCTIME_SECS(ts), (uint32_t)SCTIME_USECS(ts), pl->suffix);
ret = snprintf(file, sizeof(file), "%s.%" PRIu32 ".%" PRIu32 "%s", pl->prefix,
(uint32_t)SCTIME_SECS(ts), (uint32_t)SCTIME_USECS(ts), pl->suffix);
}
if (ret < 0 || (size_t)ret >= PATH_MAX) {
SCLogError("failed to construct path");
@ -1746,9 +1747,6 @@ static int PcapLogOpenFileCtx(PcapLogData *pl)
if (pl->filename_part_cnt > 0) {
/* assemble filename from stored tokens */
strlcpy(filename, pl->dir, PATH_MAX);
strlcat(filename, "/", PATH_MAX);
for (int i = 0; i < pl->filename_part_cnt; i++) {
if (pl->filename_parts[i] == NULL ||strlen(pl->filename_parts[i]) == 0)
continue;
@ -1778,31 +1776,35 @@ static int PcapLogOpenFileCtx(PcapLogData *pl)
(uint32_t)SCTIME_SECS(ts), (uint32_t)SCTIME_USECS(ts));
}
}
strlcat(filename, str, PATH_MAX);
strlcat(file, str, sizeof(file));
/* copy the rest over */
/* copy the rest over */
} else {
strlcat(filename, pl->filename_parts[i], PATH_MAX);
strlcat(file, pl->filename_parts[i], sizeof(file));
}
}
strlcat(filename, pl->suffix, PATH_MAX);
strlcat(file, pl->suffix, sizeof(file));
} else {
int ret;
/* create the filename to use */
if (pl->timestamp_format == TS_FORMAT_SEC) {
ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 "%s", pl->dir, pl->prefix,
ret = snprintf(file, sizeof(file), "%s.%u.%" PRIu32 "%s", pl->prefix,
pl->thread_number, (uint32_t)SCTIME_SECS(ts), pl->suffix);
} else {
ret = snprintf(filename, PATH_MAX, "%s/%s.%u.%" PRIu32 ".%" PRIu32 "%s", pl->dir,
pl->prefix, pl->thread_number, (uint32_t)SCTIME_SECS(ts),
(uint32_t)SCTIME_USECS(ts), pl->suffix);
ret = snprintf(file, sizeof(file), "%s.%u.%" PRIu32 ".%" PRIu32 "%s", pl->prefix,
pl->thread_number, (uint32_t)SCTIME_SECS(ts), (uint32_t)SCTIME_USECS(ts),
pl->suffix);
}
if (ret < 0 || (size_t)ret >= PATH_MAX) {
SCLogError("failed to construct path");
goto error;
}
}
SCLogDebug("multi-mode: filename %s", filename);
SCLogDebug("multi-mode: filename %s", file);
}
if (PathMerge(path, PATH_MAX, pl->dir, file) < 0) {
SCLogError("failed to construct path");
goto error;
}
if ((pf->filename = SCStrdup(pl->filename)) == NULL) {

Loading…
Cancel
Save