Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18fabd3

Browse files
ktrzcinxjajanusz
authored andcommitted
logger: Refactor fread() error check in logger_read()
fread() returns number of readed blocks, 0 when nothing read. Comparison fread return value with "!ret" is quite misleading - may suggests that negative value is returrned after fail. Swapping if content makes flow easier, then first is error check, and eventyally return statement, next try to reopen file. It allows to check error condition only in one place, so there won't be possibility to use different error checks in subsequent stages (like ferror() and errno). in_file alignment with trace entry size check has been added, to warn about corrupted file. Signed-off-by: Karol Trzcinski <[email protected]>
1 parent 67957cd commit 18fabd3

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

tools/logger/convert.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,20 @@ static int logger_read(void)
638638
while (!ferror(global_config->in_fd)) {
639639
/* getting entry parameters from dma dump */
640640
ret = fread(&dma_log, sizeof(dma_log), 1, global_config->in_fd);
641-
if (!ret) {
642-
if (global_config->trace && !ferror(global_config->in_fd)) {
641+
if (ret != 1) {
642+
/*
643+
* use ferror (not errno) to check fread fail -
644+
* see https://www.gnu.org/software/gnulib/manual/html_node/fread.html
645+
*/
646+
ret = -ferror(global_config->in_fd);
647+
if (ret) {
648+
log_err("in %s(), fread(..., %s) failed: %s(%d)\n",
649+
__func__, global_config->in_file,
650+
strerror(-ret), ret);
651+
return ret;
652+
}
653+
/* for trace mode, try to reopen */
654+
if (global_config->trace) {
643655
if (freopen(NULL, "rb", global_config->in_fd)) {
644656
continue;
645657
} else {
@@ -648,13 +660,13 @@ static int logger_read(void)
648660
strerror(errno), errno);
649661
return -errno;
650662
}
663+
} else {
664+
/* EOF */
665+
if (!feof(global_config->in_fd))
666+
log_err("file '%s' is unaligned with trace entry size (%ld)\n",
667+
global_config->in_file, sizeof(dma_log));
668+
break;
651669
}
652-
ret = -ferror(global_config->in_fd);
653-
if (ret)
654-
log_err("in %s(), fread(..., %s) failed: %s(%d)\n",
655-
__func__, global_config->in_file,
656-
strerror(-ret), ret);
657-
return ret;
658670
}
659671

660672
/* checking if received trace address is located in

0 commit comments

Comments
 (0)