summaryrefslogtreecommitdiffstats
path: root/TasksMeter.c
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2016-12-29 19:42:41 +0100
committerTomasz Kramkowski <tk@the-tk.com>2016-12-29 19:42:41 +0100
commit2b5c1b4b133a97a46354142aa8ab0d9e79bc70a4 (patch)
tree208946c62eff13e9e6654a146cf8ff38ea7796b1 /TasksMeter.c
parent8af4d9f453ffa2209e486418811f7652822951c6 (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 'TasksMeter.c')
-rw-r--r--TasksMeter.c8
1 files changed, 4 insertions, 4 deletions
diff --git a/TasksMeter.c b/TasksMeter.c
index 78a14683..3e695319 100644
--- a/TasksMeter.c
+++ b/TasksMeter.c
@@ -40,7 +40,7 @@ static void TasksMeter_display(Object* cast, RichString* out) {
int processes = (int) this->values[2];
- sprintf(buffer, "%d", processes);
+ snprintf(buffer, sizeof(buffer), "%d", processes);
RichString_write(out, CRT_colors[METER_VALUE], buffer);
int threadValueColor = CRT_colors[METER_VALUE];
int threadCaptionColor = CRT_colors[METER_TEXT];
@@ -50,18 +50,18 @@ static void TasksMeter_display(Object* cast, RichString* out) {
}
if (!settings->hideUserlandThreads) {
RichString_append(out, CRT_colors[METER_TEXT], ", ");
- sprintf(buffer, "%d", (int)this->values[1]);
+ snprintf(buffer, sizeof(buffer), "%d", (int)this->values[1]);
RichString_append(out, threadValueColor, buffer);
RichString_append(out, threadCaptionColor, " thr");
}
if (!settings->hideKernelThreads) {
RichString_append(out, CRT_colors[METER_TEXT], ", ");
- sprintf(buffer, "%d", (int)this->values[0]);
+ snprintf(buffer, sizeof(buffer), "%d", (int)this->values[0]);
RichString_append(out, threadValueColor, buffer);
RichString_append(out, threadCaptionColor, " kthr");
}
RichString_append(out, CRT_colors[METER_TEXT], "; ");
- sprintf(buffer, "%d", (int)this->values[3]);
+ snprintf(buffer, sizeof(buffer), "%d", (int)this->values[3]);
RichString_append(out, CRT_colors[TASKS_RUNNING], buffer);
RichString_append(out, CRT_colors[METER_TEXT], " running");
}

© 2014-2024 Faster IT GmbH | imprint | privacy policy