[spice-vdagent,v2,1/2] vdagentd: daemonize: check fopen/fprintf return value

Submitted by Uri Lublin on Aug. 29, 2019, 4:35 p.m.

Details

Message ID 20190829163524.25992-1-uril@redhat.com
State New
Headers show
Series "Series without cover letter" ( rev: 1 ) in Spice

Not browsing as part of any series.

Commit Message

Uri Lublin Aug. 29, 2019, 4:35 p.m.
Upon failure, write a warning message and continue

Found-by: covscan

Signed-off-by: Uri Lublin  <uril@redhat.com>
---

v1->v2: added a check for fopen too.

---
 src/vdagentd/vdagentd.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/vdagentd/vdagentd.c b/src/vdagentd/vdagentd.c
index 72a3e13..ba1b0c7 100644
--- a/src/vdagentd/vdagentd.c
+++ b/src/vdagentd/vdagentd.c
@@ -996,8 +996,13 @@  static void daemonize(void)
         }
         pidfile = fopen(pidfilename, "w");
         if (pidfile) {
-            fprintf(pidfile, "%d\n", (int)getpid());
+            int r = fprintf(pidfile, "%d\n", (int)getpid());
+            if (r < 0) {
+                syslog(LOG_WARNING, "Error writing to %s: %m", pidfilename);
+            }
             fclose(pidfile);
+        } else {
+            syslog(LOG_WARNING, "Failed to create pidfile %s: %m", pidfilename);
         }
         break;
     case -1:

Comments

> 
> Upon failure, write a warning message and continue
> 
> Found-by: covscan
> 
> Signed-off-by: Uri Lublin  <uril@redhat.com>
> ---
> 
> v1->v2: added a check for fopen too.
> 
> ---
>  src/vdagentd/vdagentd.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
> 
> diff --git a/src/vdagentd/vdagentd.c b/src/vdagentd/vdagentd.c
> index 72a3e13..ba1b0c7 100644
> --- a/src/vdagentd/vdagentd.c
> +++ b/src/vdagentd/vdagentd.c
> @@ -996,8 +996,13 @@ static void daemonize(void)
>          }
>          pidfile = fopen(pidfilename, "w");
>          if (pidfile) {
> -            fprintf(pidfile, "%d\n", (int)getpid());
> +            int r = fprintf(pidfile, "%d\n", (int)getpid());
> +            if (r < 0) {
> +                syslog(LOG_WARNING, "Error writing to %s: %m", pidfilename);

This won't never be executed, the condition should be

   if (fclose(pidfile) || r < 0) {

the file is just opened, the string is small and the file is not a terminal
(so full buffering, no line one, no flush) so fprintf will return r > 0.

> +            }
>              fclose(pidfile);

removing this.

> +        } else {
> +            syslog(LOG_WARNING, "Failed to create pidfile %s: %m",
> pidfilename);
>          }
>          break;
>      case -1:

Frediano