diff options
author | Tomasz Kramkowski <tk@the-tk.com> | 2016-12-29 19:42:41 +0100 |
---|---|---|
committer | Tomasz Kramkowski <tk@the-tk.com> | 2016-12-29 19:42:41 +0100 |
commit | 2b5c1b4b133a97a46354142aa8ab0d9e79bc70a4 (patch) | |
tree | 208946c62eff13e9e6654a146cf8ff38ea7796b1 /LoadAverageMeter.c | |
parent | 8af4d9f453ffa2209e486418811f7652822951c6 (diff) |
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.
Diffstat (limited to 'LoadAverageMeter.c')
-rw-r--r-- | LoadAverageMeter.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/LoadAverageMeter.c b/LoadAverageMeter.c index 54e517a8..517031dd 100644 --- a/LoadAverageMeter.c +++ b/LoadAverageMeter.c @@ -28,11 +28,11 @@ static void LoadAverageMeter_updateValues(Meter* this, char* buffer, int size) { static void LoadAverageMeter_display(Object* cast, RichString* out) { Meter* this = (Meter*)cast; char buffer[20]; - sprintf(buffer, "%.2f ", this->values[0]); + snprintf(buffer, sizeof(buffer), "%.2f ", this->values[0]); RichString_write(out, CRT_colors[LOAD_AVERAGE_ONE], buffer); - sprintf(buffer, "%.2f ", this->values[1]); + snprintf(buffer, sizeof(buffer), "%.2f ", this->values[1]); RichString_append(out, CRT_colors[LOAD_AVERAGE_FIVE], buffer); - sprintf(buffer, "%.2f ", this->values[2]); + snprintf(buffer, sizeof(buffer), "%.2f ", this->values[2]); RichString_append(out, CRT_colors[LOAD_AVERAGE_FIFTEEN], buffer); } @@ -48,7 +48,7 @@ static void LoadMeter_updateValues(Meter* this, char* buffer, int size) { static void LoadMeter_display(Object* cast, RichString* out) { Meter* this = (Meter*)cast; char buffer[20]; - sprintf(buffer, "%.2f ", ((Meter*)this)->values[0]); + snprintf(buffer, sizeof(buffer), "%.2f ", ((Meter*)this)->values[0]); RichString_write(out, CRT_colors[LOAD], buffer); } |