diff options
author | Lassi Marttala <Lassi.LM.Marttala@partner.bmw.de> | 2013-01-30 11:48:35 +0100 |
---|---|---|
committer | Alexander Wenzel <Alexander.AW.Wenzel@bmw.de> | 2013-07-19 16:54:27 +0200 |
commit | e4ef602f86c7725fbe52f9cd129851ef24be61e0 (patch) | |
tree | 2b65fe1e69f3c3669a27c90afddcb095ba2680ec | |
parent | 0c2ac36389567e46804d48f5b3a410016c7fb07e (diff) | |
download | DLT-daemon-e4ef602f86c7725fbe52f9cd129851ef24be61e0.tar.gz |
Regression fix: check ferror/feof when fgets return NULL
Signed-off-by: Alexander Wenzel <Alexander.AW.Wenzel@bmw.de>
-rw-r--r-- | src/daemon/dlt_daemon_common.c | 42 |
1 files changed, 38 insertions, 4 deletions
diff --git a/src/daemon/dlt_daemon_common.c b/src/daemon/dlt_daemon_common.c index 69aecdc..12af44e 100644 --- a/src/daemon/dlt_daemon_common.c +++ b/src/daemon/dlt_daemon_common.c @@ -494,10 +494,27 @@ int dlt_daemon_applications_load(DltDaemon *daemon,const char *filename, int ver ret=fgets(buf,sizeof(buf),fd); if (NULL == ret) { - snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "dlt_daemon_applications_load fgets(buf,sizeof(buf),fd) returned NULL"); + /* fgets always null pointer if the last byte of the file is a new line + * We need to check here if there was an error or was it feof.*/ + if(ferror(fd)) + { + snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "dlt_daemon_applications_load fgets(buf,sizeof(buf),fd) returned NULL. %s\n", + strerror(errno)); + dlt_log(LOG_ERR, str); + fclose(fd); + return -1; + } + else if(feof(fd)) + { + fclose(fd); + return 0; + } + else { + snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "dlt_daemon_applications_load fgets(buf,sizeof(buf),fd) returned NULL. Unknown error.\n"); dlt_log(LOG_ERR, str); fclose(fd); - return -1;//seems to be appropriate, but not sure. !feof is already a precondition, so another problem should the reason! + return -1; + } } if (strcmp(buf,"")!=0) @@ -840,10 +857,27 @@ int dlt_daemon_contexts_load(DltDaemon *daemon,const char *filename, int verbose ret=fgets(buf,sizeof(buf),fd); if (NULL == ret) { - sprintf(str,"dlt_daemon_contexts_load: fgets(buf,sizeof(buf),fd);"); + /* fgets always returns null pointer if the last byte of the file is a new line. + * We need to check here if there was an error or was it feof.*/ + if(ferror(fd)) + { + snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "dlt_daemon_contexts_load fgets(buf,sizeof(buf),fd) returned NULL. %s\n", + strerror(errno)); + dlt_log(LOG_ERR, str); + fclose(fd); + return -1; + } + else if(feof(fd)) + { + fclose(fd); + return 0; + } + else { + snprintf(str,DLT_DAEMON_COMMON_TEXTBUFSIZE, "dlt_daemon_contexts_load fgets(buf,sizeof(buf),fd) returned NULL. Unknown error.\n"); dlt_log(LOG_ERR, str); fclose(fd); - return -1;//as we are not eof, there seems to be another problem. + return -1; + } } if (strcmp(buf,"")!=0) |