Skip to content

Commit

Permalink
tools: logger: Use a safe variant of the string manipulation functions
Browse files Browse the repository at this point in the history
Used string manipulation functions that check the size of the available
buffer.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
(cherry picked from commit 50f936d)
  • Loading branch information
softwarecki authored and marc-hb committed Sep 20, 2023
1 parent db43d77 commit b36b94e
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions tools/logger/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ static int snapshot(const char *name)

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

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

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

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

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

Expand Down Expand Up @@ -220,17 +220,15 @@ static void *wait_open(const char *watched_dir, const char *expected_file)
const int dwatch = inotify_add_watch(iqueue, watched_dir, IN_CREATE);
struct stat expected_stat;
void *ret_stream = NULL;

char * const fpath = malloc(strlen(watched_dir) + 1 + strlen(expected_file) + 1);
const int fpath_len = strlen(watched_dir) + 1 + strlen(expected_file) + 1;
char * const fpath = malloc(fpath_len);

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

strcpy(fpath, watched_dir);
strcat(fpath, "/");
strcat(fpath, expected_file);
snprintf(fpath, fpath_len, "%s/%s", watched_dir, expected_file);

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

0 comments on commit b36b94e

Please sign in to comment.