Skip to content

Commit edd77fb

Browse files
committed
tools: logger: Use a safe variant of the string manipulation functions
Used string manipulation functions that check the size of the available buffer. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 9c9cbde commit edd77fb

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

tools/logger/logger.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ static int snapshot(const char *name)
104104

105105
for (i = 0; i < ARRAY_SIZE(debugfs); i++) {
106106

107-
sprintf(pinname, "%s/%s", path, debugfs[i]);
108-
sprintf(poutname, "%s.%s.txt", name, debugfs[i]);
107+
snprintf(pinname, sizeof(pinname), "%s/%s", path, debugfs[i]);
108+
snprintf(poutname, sizeof(poutname), "%s.%s.txt", name, debugfs[i]);
109109

110110
/* open debugfs for reading */
111111
in_fd = fopen(pinname, "rb");
@@ -132,7 +132,7 @@ static int snapshot(const char *name)
132132
if (count != 4)
133133
break;
134134

135-
sprintf(buffer, "0x%6.6x: 0x%8.8x\n", addr, val);
135+
snprintf(buffer, sizeof(buffer), "0x%6.6x: 0x%8.8x\n", addr, val);
136136

137137
count = fwrite(buffer, 1, strlen(buffer), out_fd);
138138

@@ -220,17 +220,15 @@ static void *wait_open(const char *watched_dir, const char *expected_file)
220220
const int dwatch = inotify_add_watch(iqueue, watched_dir, IN_CREATE);
221221
struct stat expected_stat;
222222
void *ret_stream = NULL;
223-
224-
char * const fpath = malloc(strlen(watched_dir) + 1 + strlen(expected_file) + 1);
223+
const int fpath_len = strlen(watched_dir) + 1 + strlen(expected_file) + 1;
224+
char * const fpath = malloc(fpath_len);
225225

226226
if (!fpath) {
227227
fprintf(stderr, "error: can't allocate memory\n");
228228
exit(EXIT_FAILURE);
229229
}
230230

231-
strcpy(fpath, watched_dir);
232-
strcat(fpath, "/");
233-
strcat(fpath, expected_file);
231+
snprintf(fpath, fpath_len, "%s/%s", watched_dir, expected_file);
234232

235233
/* Not racy because the inotify watch was set first. */
236234
if (!access(fpath, F_OK))

0 commit comments

Comments
 (0)