Skip to content

Commit fced92e

Browse files
committed
tools: logger: Fix resources release
Improved release of resources when an error is detected. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 50f936d commit fced92e

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

tools/logger/logger.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ static int snapshot(const char *name)
134134

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

137-
count = fwrite(buffer, 1, strlen(buffer), out_fd);
137+
i = strlen(buffer);
138+
count = fwrite(buffer, 1, i, out_fd);
139+
if (count != i) {
140+
fprintf(stderr, "error: an error occurred during write to %s: %s\n",
141+
poutname, strerror(errno));
142+
}
138143

139144
addr += 4;
140145
}
@@ -164,7 +169,12 @@ static int configure_uart(const char *file, unsigned int baud)
164169
tio.c_cc[VMIN] = 1;
165170

166171
ret = tcsetattr(fd, TCSANOW, &tio);
167-
return ret < 0 ? -errno : fd;
172+
if (ret < 0) {
173+
close(fd);
174+
return -errno;
175+
}
176+
177+
return fd;
168178
}
169179

170180
/* Concantenate `config->filter_config` with `input` + `\n` */
@@ -266,7 +276,9 @@ static void *wait_open(const char *watched_dir, const char *expected_file)
266276
}
267277

268278
fopenit:
269-
stat(fpath, &expected_stat);
279+
if (stat(fpath, &expected_stat))
280+
goto cleanup;
281+
270282
if ((expected_stat.st_mode & S_IFMT) == S_IFDIR)
271283
ret_stream = opendir(fpath);
272284
else
@@ -362,7 +374,8 @@ int main(int argc, char *argv[])
362374
if (i < 0 || 1 < i) {
363375
fprintf(stderr, "%s: invalid option: -e %s\n",
364376
APP_NAME, optarg);
365-
return -EINVAL;
377+
ret = -EINVAL;
378+
goto out;
366379
}
367380
config.relative_timestamps = i;
368381
break;
@@ -371,7 +384,8 @@ int main(int argc, char *argv[])
371384
config.time_precision = atoi(optarg);
372385
if (config.time_precision < 0) {
373386
usage();
374-
return -EINVAL;
387+
ret = -EINVAL;
388+
goto out;
375389
}
376390
break;
377391
case 'g':
@@ -401,8 +415,10 @@ int main(int argc, char *argv[])
401415
usage();
402416
}
403417

404-
if (snapshot_file)
405-
return baud ? EINVAL : -snapshot(snapshot_file);
418+
if (snapshot_file) {
419+
ret = baud ? EINVAL : -snapshot(snapshot_file);
420+
goto out;
421+
}
406422

407423
if (!config.ldc_file) {
408424
fprintf(stderr, "error: Missing ldc file\n");

0 commit comments

Comments
 (0)