-
Notifications
You must be signed in to change notification settings - Fork 104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The unit tests for PR #273 #274
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,11 @@ | |
* "LICENSE" for information on usage and redistribution of this file. | ||
*/ | ||
|
||
#include <assert.h> | ||
Check warning Code scanning / Cppcheck (reported by Codacy) Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning
Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
|
||
#include <stdint.h> | ||
#include <stdio.h> | ||
Check warning Code scanning / Cppcheck (reported by Codacy) Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning
Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 21.6 rule Note
MISRA 21.6 rule
|
||
#include <stdlib.h> | ||
Check warning Code scanning / Cppcheck (reported by Codacy) Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning
Include file: <stdlib.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
|
||
#include <string.h> | ||
Check warning Code scanning / Cppcheck (reported by Codacy) Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning
Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
|
||
#include <sys/time.h> | ||
#include <time.h> | ||
|
||
|
@@ -67,3 +71,163 @@ | |
tp->tv_sec = tv_sec; | ||
tp->tv_nsec = tv_usec / 1000; /* Transfer to microseconds */ | ||
} | ||
|
||
char *sanitize_path(const char *orig_path) | ||
{ | ||
size_t n = strlen(orig_path); | ||
Check warning Code scanning / Semgrep (reported by Codacy) The strlen family of functions does not handle strings that are not null terminated. This can lead to buffer over reads and cause the application to crash by accessing unintended memory locations. It is recommended that strnlen be used instead as a maxlen value can be provided. For more information please see: https://linux.die.net/man/3/strnlen If developing for C Runtime Library (CRT), more secure versions of these functions should be used, see: https://learn.microsoft.com/en-us/cpp/c- Warning
The strlen family of functions does not handle strings that are not null
terminated. This can lead to buffer over reads and cause the application to crash by accessing unintended memory locations. It is recommended that strnlen be used instead as a maxlen value can be provided. For more information please see: https://linux.die.net/man/3/strnlen If developing for C Runtime Library (CRT), more secure versions of these functions should be used, see: https://learn.microsoft.com/en-us/cpp/c- |
||
|
||
char *ret = (char *) malloc(n + 1); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 21.3 rule Note
MISRA 21.3 rule
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
|
||
memset(ret, '\0', n + 1); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 17.7 rule Note
MISRA 17.7 rule
|
||
Check warning Code scanning / Semgrep (reported by Codacy) When handling sensitive information in a buffer, it's important to ensure that the data is securely erased before the buffer is deleted or reused. Warning
When handling sensitive information in a buffer, it's important to ensure that the data is securely erased before the buffer is deleted or reused.
|
||
|
||
/* After sanitization, the new path will only be shorter than the original | ||
* one. Thus, we can reuse the space */ | ||
if (n == 0) { | ||
ret[0] = '.'; | ||
return ret; | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 15.5 rule Note
MISRA 15.5 rule
|
||
} | ||
|
||
int rooted = (orig_path[0] == '/'); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 1003 with no text in the supplied rule-texts-file Warning
misra violation 1003 with no text in the supplied rule-texts-file
|
||
|
||
/* | ||
* Invariants: | ||
* reading from path; r is index of next byte to process -> path[r] | ||
* writing to buf; w is index of next byte to write -> ret[strlen(ret)] | ||
* dotdot is index in buf where .. must stop, either because | ||
* a) it is the leading slash | ||
* b) it is a leading ../../.. prefix. | ||
*/ | ||
size_t w = 0; | ||
size_t r = 0; | ||
size_t dotdot = 0; | ||
if (rooted) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 14.4 rule Note
MISRA 14.4 rule
|
||
ret[w] = '/'; | ||
w++; | ||
r = 1; | ||
dotdot = 1; | ||
} | ||
|
||
while (r < n) { | ||
if (orig_path[r] == '/') { | ||
/* empty path element */ | ||
r++; | ||
} else if (orig_path[r] == '.' && | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
|
||
(r + 1 == n || orig_path[r + 1] == '/')) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
|
||
/* . element */ | ||
r++; | ||
} else if (orig_path[r] == '.' && orig_path[r + 1] == '.' && | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
|
||
(r + 2 == n || orig_path[r + 2] == '/')) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
|
||
/* .. element: remove to last / */ | ||
r += 2; | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
|
||
|
||
if (w > dotdot) { | ||
/* can backtrack */ | ||
w--; | ||
while (w > dotdot && ret[w] != '/') { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
|
||
w--; | ||
} | ||
} else if (!rooted) { | ||
/* cannot backtrack, but not rooted, so append .. element. */ | ||
if (w > 0) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
|
||
ret[w] = '/'; | ||
w++; | ||
} | ||
ret[w] = '.'; | ||
w++; | ||
ret[w] = '.'; | ||
w++; | ||
dotdot = w; | ||
} | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 15.7 rule Note
MISRA 15.7 rule
|
||
} else { | ||
/* real path element. | ||
add slash if needed */ | ||
if ((rooted && w != 1) || (!rooted && w != 0)) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
|
||
ret[w] = '/'; | ||
w++; | ||
} | ||
|
||
/* copy element */ | ||
for (; r < n && orig_path[r] != '/'; r++) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 12.1 rule Note
MISRA 12.1 rule
|
||
ret[w] = orig_path[r]; | ||
w++; | ||
} | ||
} | ||
// printf("w = %ld, r = %ld, dotdot = %ld\nret = %s\n", w, r, dotdot, | ||
// ret); | ||
} | ||
|
||
/* Turn empty string into "." */ | ||
if (w == 0) { | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 10.4 rule Note
MISRA 10.4 rule
|
||
ret[w] = '.'; | ||
w++; | ||
} | ||
|
||
for (size_t i = w; i < n; i++) { | ||
ret[i] = '\0'; | ||
} | ||
return ret; | ||
} | ||
|
||
#ifdef UNITTEST | ||
void compare(char *input, char *expected_output) | ||
Check warning Code scanning / Cppcheck (reported by Codacy) Parameter 'input' can be declared as pointer to const Warning
Parameter 'input' can be declared as pointer to const
Check warning Code scanning / Cppcheck (reported by Codacy) Parameter 'expected_output' can be declared as pointer to const Warning
Parameter 'expected_output' can be declared as pointer to const
|
||
{ | ||
char *input_sanitized = sanitize_path(input); | ||
// printf("\n\nInput =\t\t\t%s\nOutput =\t\t%s\nExpected output =\t%s\n", | ||
// input, input_sanitized, expected_output); | ||
assert(strcmp(input_sanitized, expected_output) == 0); | ||
free(input_sanitized); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 21.3 rule Note
MISRA 21.3 rule
|
||
} | ||
|
||
void sanitize_path_test() | ||
{ | ||
printf("sanitize_path_test - start\n"); | ||
|
||
// Already clean | ||
compare("", "."); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("abc", "abc"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("abc/def", "abc/def"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare(".", "."); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("..", ".."); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("../..", "../.."); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("../../abc", "../../abc"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("/abc", "/abc"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("/", "/"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
|
||
// Remove trailing slash | ||
compare("abc/", "abc"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("abc/def/", "abc/def"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("a/b/c/", "a/b/c"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("./", "."); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("../", ".."); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("../../", "../.."); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("/abc/", "/abc"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
|
||
// Remove doubled slash | ||
compare("abc//def//ghi", "abc/def/ghi"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("//abc", "/abc"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("///abc", "/abc"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("//abc//", "/abc"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("abc//", "abc"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
|
||
// Remove . elements | ||
compare("abc/./def", "abc/def"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("/./abc/def", "/abc/def"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("abc/.", "abc"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
|
||
// Remove .. elements | ||
compare("abc/def/ghi/../jkl", "abc/def/jkl"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("abc/def/../ghi/../jkl", "abc/jkl"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("abc/def/..", "abc"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("abc/def/../..", "."); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("/abc/def/../..", "/"); | ||
compare("abc/def/../../..", ".."); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("/abc/def/../../..", "/"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("abc/def/../../../ghi/jkl/../../../mno", "../../mno"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
|
||
// Combinations | ||
compare("abc/./../def", "def"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
compare("abc//./../def", "def"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
|
||
compare("abc/../../././../def", "../../def"); | ||
Check warning Code scanning / Cppcheck (reported by Codacy) misra violation 704 with no text in the supplied rule-texts-file Warning
misra violation 704 with no text in the supplied rule-texts-file
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 11.8 rule Note
MISRA 11.8 rule
|
||
|
||
printf("sanitize_path_test - end\n"); | ||
Check notice Code scanning / Cppcheck (reported by Codacy) MISRA 17.7 rule Note
MISRA 17.7 rule
|
||
} | ||
#endif |
Check notice
Code scanning / Flawfinder (reported by Codacy)
Does not handle strings that are not \0-terminated; if given one it may perform an over-read (it could cause a crash if unprotected) (CWE-126). Note