From 0eb59fc8489cd5337542829ecbf7db2bd6cc368f Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Thu, 21 Mar 2019 13:45:25 +0100 Subject: [PATCH] Use minimal number of characters for strncmp MAX_GOVERNOR_LENGTH is defined as PATH_MAX + 1, so when comparing two variable, each of once size, use the smaller one, i.e. PATH_MAX. Assert statically that MAX_GOVERNOR_LENGTH is larger then PATH_MAX so copying a string to a MAX_GOVERNOR_LENGTH sized variable from a PATH_MAX size variable will not truncate the string. --- daemon/governors-query.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/daemon/governors-query.c b/daemon/governors-query.c index d357678..9e0af0f 100644 --- a/daemon/governors-query.c +++ b/daemon/governors-query.c @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "governors-query.h" #include "logging.h" +#include #include #include #include @@ -80,12 +81,13 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH]) /* Only add this governor if it is unique */ for (int j = 0; j < num_governors; j++) { - if (strncmp(fullpath, governors[i], MAX_GOVERNOR_LENGTH) == 0) { + if (strncmp(fullpath, governors[i], PATH_MAX) == 0) { continue; } } /* Copy this governor into the output set */ + static_assert(MAX_GOVERNOR_LENGTH > PATH_MAX, "possible string truncation"); strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH); num_governors++; }