commit 98a1481d100468b02f4f422a597e40dd1a1a94f0
parent e8a78ffb8ec0e64f376c94022a934a7b02630800
Author: afify <hassan@afify.dev>
Date: Fri, 9 Apr 2021 17:41:45 +0300
[fix] get_ext() max extension
problem 0: strlen() not check filename max length
solution 0: use strnlen with MAX_N
problem 1: allocating memory depends on counter
solution 1: always allocate MAX_EXT + 1 for '\0'
problem 2: ext string could be not null terminated
solution 2: make ext[MAX_EXT] = '\0'
Diffstat:
1 file changed, 4 insertions(+), 3 deletions(-)
diff --git a/sfm.c b/sfm.c
@@ -422,7 +422,7 @@ get_ext(char *str)
dot = '.';
counter = 0;
- len = strlen(str);
+ len = strnlen(str, MAX_N);
for (i = len - 1; i > 0; i--) {
if (str[i] == dot) {
@@ -432,8 +432,9 @@ get_ext(char *str)
}
}
- ext = ecalloc(counter + 1, sizeof(char));
- strncpy(ext, &str[len - counter], counter);
+ ext = ecalloc(MAX_EXT + 1, sizeof(char));
+ strncpy(ext, &str[len - counter], MAX_EXT);
+ ext[MAX_EXT] = '\0';
return ext;
}