Просмотр исходного кода

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.
Christian Kellner 6 лет назад
Родитель
Сommit
0eb59fc848
1 измененных файлов с 3 добавлено и 1 удалено
  1. 3 1
      daemon/governors-query.c

+ 3 - 1
daemon/governors-query.c

@@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE.
 #include "governors-query.h"
 #include "logging.h"
 
+#include <assert.h>
 #include <glob.h>
 #include <stdio.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 */
 		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++;
 	}