Skip to content

Commit cc260b1

Browse files
committed
depinfo: Rewrite firmware processing
The process_firmware() function was rewritten so that there was no temporary memory allocation. Signed-off-by: Alexey Gladkov <gladkov.alexey@gmail.com>
1 parent 83e6b6b commit cc260b1

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

utils/depinfo/kmod-depinfo.c

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -329,29 +329,41 @@ static void
329329
process_firmware(const char *firmware)
330330
{
331331
char firmware_buf[MAXPATHLEN];
332-
char *s, *str, *token, *saveptr = NULL;
333-
s = str = strdup(firmware_dir);
332+
const char *begin = firmware_dir;
333+
int found = 0;
334334

335-
while ((token = strtok_r(str, ":", &saveptr)) != NULL) {
336-
for (int n = 0; suffixes[n]; n++) {
337-
int retry = 0;
338-
snprintf(firmware_buf, sizeof(firmware_buf), "%s/%s/%s%s", token, kversion, firmware, suffixes[n]);
339-
again:
340-
if (!access(firmware_buf, F_OK)) {
341-
show_with_prefix(show_tree, "firmware", firmware_buf);
342-
break;
343-
}
335+
while (*begin && !found) {
336+
const char *end = strchr(begin, ':');
337+
size_t len = end ? (size_t) (end - begin) : strlen(begin);
338+
339+
if (len > 0 && len < sizeof(firmware_buf)) {
340+
strncpy(firmware_buf, begin, len);
341+
firmware_buf[len] = '\0';
344342

345-
if (!retry) {
346-
retry = 1;
347-
snprintf(firmware_buf, sizeof(firmware_buf), "%s/%s%s", token, firmware, suffixes[n]);
348-
goto again;
343+
for (int n = 0; suffixes[n] && !found; n++) {
344+
int retry = 0;
345+
346+
snprintf(firmware_buf + len, sizeof(firmware_buf) - len,
347+
"/%s/%s%s", kversion, firmware, suffixes[n]);
348+
again:
349+
found = !access(firmware_buf, F_OK);
350+
if (found) {
351+
show_with_prefix(show_tree, "firmware", firmware_buf);
352+
break;
353+
}
354+
if (!retry) {
355+
snprintf(firmware_buf + len, sizeof(firmware_buf) - len,
356+
"/%s%s", firmware, suffixes[n]);
357+
retry = 1;
358+
goto again;
359+
}
349360
}
350361
}
351362

352-
str = NULL;
363+
if (!end)
364+
break;
365+
begin = end + 1;
353366
}
354-
free(s);
355367
}
356368

357369
static int

0 commit comments

Comments
 (0)