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

Uri Lublin uril at redhat.com
Thu Aug 29 16:35:23 UTC 2019


Upon failure, write a warning message and continue

Found-by: covscan

Signed-off-by: Uri Lublin  <uril at 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);
+            }
             fclose(pidfile);
+        } else {
+            syslog(LOG_WARNING, "Failed to create pidfile %s: %m", pidfilename);
         }
         break;
     case -1:
-- 
2.21.0



More information about the Spice-devel mailing list