From 961931e73a223c6fa1fa03fd83b245efc90f9ccb Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Wed, 31 Oct 2018 16:41:07 -0600 Subject: [PATCH] filestore: fix compiler truncation warnings And error out if the constructed filename is truncated. --- src/output-filestore.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/output-filestore.c b/src/output-filestore.c index 2ce3d9dd65..2c5c908d95 100644 --- a/src/output-filestore.c +++ b/src/output-filestore.c @@ -354,7 +354,13 @@ static bool InitFilestoreDirectory(const char *dir) for (int i = 0; i <= dir_count; i++) { char leaf[PATH_MAX]; - snprintf(leaf, sizeof(leaf) - 1, "%s/%02x", dir, i); + int n = snprintf(leaf, sizeof(leaf), "%s/%02x", dir, i); + if (n < 0 || n >= PATH_MAX) { + SCLogError(SC_ERR_CREATE_DIRECTORY, + "Filestore (v2) failed to create leaf directory: " + "path too long"); + return false; + } if (!SCPathExists(leaf)) { SCLogInfo("Filestore (v2) creating directory %s", leaf); if (SCDefaultMkDir(leaf) != 0) { @@ -368,7 +374,12 @@ static bool InitFilestoreDirectory(const char *dir) /* Make sure the tmp directory exists. */ char tmpdir[PATH_MAX]; - snprintf(tmpdir, sizeof(tmpdir) - 1, "%s/tmp", dir); + int n = snprintf(tmpdir, sizeof(tmpdir), "%s/tmp", dir); + if (n < 0 || n >= PATH_MAX) { + SCLogError(SC_ERR_CREATE_DIRECTORY, + "Filestore (v2) failed to create tmp directory: path too long"); + return false; + } if (!SCPathExists(tmpdir)) { SCLogInfo("Filestore (v2) creating directory %s", tmpdir); if (SCDefaultMkDir(tmpdir) != 0) {