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.
This commit is contained in:
Christian Kellner 2019-03-21 13:45:25 +01:00 committed by Alex Smith
parent bbde1d0357
commit 0eb59fc848

View File

@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "governors-query.h" #include "governors-query.h"
#include "logging.h" #include "logging.h"
#include <assert.h>
#include <glob.h> #include <glob.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@ -80,12 +81,13 @@ int fetch_governors(char governors[MAX_GOVERNORS][MAX_GOVERNOR_LENGTH])
/* Only add this governor if it is unique */ /* Only add this governor if it is unique */
for (int j = 0; j < num_governors; j++) { 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; continue;
} }
} }
/* Copy this governor into the output set */ /* 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); strncpy(governors[num_governors], fullpath, MAX_GOVERNOR_LENGTH);
num_governors++; num_governors++;
} }