Skip to content

Commit 650471a

Browse files
committed
Clean up string creation via smprintf and strdup
This fixes some bugs when using the incorrect strlen in strncat.
1 parent f1bdb61 commit 650471a

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

src/mbpfan.c

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
*/
2828

2929

30+
#include <stdarg.h>
3031
#include <stdio.h>
3132
#include <stdlib.h>
3233
#include <unistd.h>
@@ -61,6 +62,29 @@ t_sensors* sensors = NULL;
6162
t_fans* fans = NULL;
6263

6364

65+
static char *smprintf(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
66+
static char *smprintf(const char *fmt, ...)
67+
{
68+
char *buf;
69+
int cnt;
70+
va_list ap;
71+
72+
// find buffer length
73+
va_start(ap, fmt);
74+
cnt = vsnprintf(NULL, 0, fmt, ap);
75+
va_end(ap);
76+
if (cnt < 0) {
77+
return NULL;
78+
}
79+
80+
// create and write to buffer
81+
buf = malloc(cnt + 1);
82+
va_start(ap, fmt);
83+
vsnprintf(buf, cnt + 1, fmt, ap);
84+
va_end(ap);
85+
return buf;
86+
}
87+
6488
bool is_legacy_sensors_path()
6589
{
6690
struct utsname kernel;
@@ -147,9 +171,7 @@ t_sensors *retrieve_sensors()
147171

148172
if (errno == EISDIR) {
149173

150-
path_begin = (char*) malloc(sizeof( char ) * (strlen(hwmon_path) + strlen("/temp") + 1));
151-
strcpy(path_begin, hwmon_path);
152-
strcat(path_begin, "/temp");
174+
path_begin = smprintf("%s/temp", hwmon_path);
153175

154176
if(verbose) {
155177
printf("Found hwmon path at %s\n", path_begin);
@@ -167,28 +189,17 @@ t_sensors *retrieve_sensors()
167189

168190
const char *path_end = "_input";
169191

170-
int path_size = strlen(path_begin) + strlen(path_end) + 2;
171-
char number[2];
172-
sprintf(number,"%d",0);
173-
174192
int sensors_found = 0;
175193

176194
int counter = 0;
177195
for(counter = 0; counter<10; counter++) {
178-
path = (char*) malloc(sizeof( char ) * path_size);
179-
180-
sprintf(number,"%d",counter);
181-
path[0] = '\0';
182-
strncat( path, path_begin, strlen(path_begin) );
183-
strncat( path, number, strlen(number) );
184-
strncat( path, path_end, strlen(path_begin) );
196+
path = smprintf("%s%d%s", path_begin, counter, path_end);
185197

186198
FILE *file = fopen(path, "r");
187199

188200
if(file != NULL) {
189201
s = (t_sensors *) malloc( sizeof( t_sensors ) );
190-
s->path = (char *) malloc(sizeof( char ) * path_size);
191-
strcpy(s->path, path);
202+
s->path = strdup(path);
192203
fscanf(file, "%d", &s->temperature);
193204

194205
if (sensors_head == NULL) {
@@ -248,39 +259,20 @@ t_fans *retrieve_fans()
248259
const char *path_output_end = "_output";
249260
const char *path_man_end = "_manual";
250261

251-
int path_min_size = strlen(path_begin) + strlen(path_output_end) + 2;
252-
int path_man_size = strlen(path_begin) + strlen(path_man_end) + 2;
253-
char number[2];
254-
sprintf(number,"%d",0);
255-
256262
int counter = 0;
257263
int fans_found = 0;
258264

259265
for(counter = 0; counter<10; counter++) {
260266

261-
path_output = (char*) malloc(sizeof( char ) * path_min_size);
262-
path_output[0] = '\0';
263-
path_manual = (char*) malloc(sizeof( char ) * path_man_size);
264-
path_manual[0] = '\0';
265-
sprintf(number,"%d",counter);
266-
267-
strncat( path_output, path_begin, strlen(path_begin) );
268-
strncat( path_output, number, strlen(number) );
269-
strncat( path_output, path_output_end, strlen(path_begin) );
270-
271-
strncat( path_manual, path_begin, strlen(path_begin) );
272-
strncat( path_manual, number, strlen(number) );
273-
strncat( path_manual, path_man_end, strlen(path_begin) );
274-
267+
path_output = smprintf("%s%d%s", path_begin, counter, path_output_end);
268+
path_manual = smprintf("%s%d%s", path_begin, counter, path_man_end);
275269

276270
FILE *file = fopen(path_output, "w");
277271

278272
if(file != NULL) {
279273
fan = (t_fans *) malloc( sizeof( t_fans ) );
280-
fan->fan_output_path = (char *) malloc(sizeof( char ) * path_min_size);
281-
fan->fan_manual_path = (char *) malloc(sizeof( char ) * path_man_size);
282-
strcpy(fan->fan_output_path, path_output);
283-
strcpy(fan->fan_manual_path, path_manual);
274+
fan->fan_output_path = strdup(path_output);
275+
fan->fan_manual_path = strdup(path_manual);
284276

285277
if (fans_head == NULL) {
286278
fans_head = fan;

0 commit comments

Comments
 (0)