mirror of
https://github.com/xzeldon/htop.git
synced 2025-07-14 04:54:37 +03:00
Replace all uses of sprintf with snprintf
In all the cases where sprintf was being used within htop, snprintf could have been used. This patch replaces all uses of sprintf with snprintf which makes sure that if a buffer is too small to hold the resulting string, the string is simply cut short instead of causing a buffer overflow which leads to undefined behaviour. `sizeof(variable)` was used in these cases, as opposed to `sizeof variable` which is my personal preference because `sizeof(variable)` was already used in one way or another in other parts of the code.
This commit is contained in:
@ -33,11 +33,11 @@ static void UptimeMeter_updateValues(Meter* this, char* buffer, int len) {
|
||||
}
|
||||
char daysbuf[15];
|
||||
if (days > 100) {
|
||||
sprintf(daysbuf, "%d days(!), ", days);
|
||||
snprintf(daysbuf, sizeof(daysbuf), "%d days(!), ", days);
|
||||
} else if (days > 1) {
|
||||
sprintf(daysbuf, "%d days, ", days);
|
||||
snprintf(daysbuf, sizeof(daysbuf), "%d days, ", days);
|
||||
} else if (days == 1) {
|
||||
sprintf(daysbuf, "1 day, ");
|
||||
snprintf(daysbuf, sizeof(daysbuf), "1 day, ");
|
||||
} else {
|
||||
daysbuf[0] = '\0';
|
||||
}
|
||||
|
Reference in New Issue
Block a user