]> granicus.if.org Git - nethack/commitdiff
apple pasteboard
authorPatR <rankin@nethack.org>
Fri, 11 Feb 2022 22:43:01 +0000 (14:43 -0800)
committerPatR <rankin@nethack.org>
Fri, 11 Feb 2022 22:43:01 +0000 (14:43 -0800)
Turning on -Wformat-noliteral for Mac triggered a new warning.
Blindly suppressing the warning would have silenced it but would
also have left a real bug in place.  The former format was passing
a string argument to %d format.

This converts the format to a literal with an additional argument
for the non-literal part.  It compiles cleanly but I don't know how
to test it, let alone force an error for it to report.

sys/unix/unixmain.c

index 9f99bb949620c4d1d24e1ea3dd290a3330240113..59f41a7379bd9c4ccf2ec1a8edbd3031918c168c 100644 (file)
@@ -729,33 +729,33 @@ void
 port_insert_pastebuf(char *buf)
 {
     /* This should be replaced when there is a Cocoa port. */
-    const char *errfmt;
+    const char *errarg;
     size_t len;
     FILE *PB = popen("/usr/bin/pbcopy", "w");
 
     if (!PB) {
-        errfmt = "Unable to start pbcopy (%d)\n";
+        errarg = "Unable to start pbcopy";
         goto error;
     }
 
     len = strlen(buf);
     /* Remove the trailing \n, carefully. */
-    if (buf[len - 1] == '\n')
+    if (len > 0 && buf[len - 1] == '\n')
         len--;
 
     /* XXX Sorry, I'm too lazy to write a loop for output this short. */
     if (len != fwrite(buf, 1, len, PB)) {
-        errfmt = "Error sending data to pbcopy (%d)\n";
+        errarg = "Error sending data to pbcopy";
         goto error;
     }
 
     if (pclose(PB) != -1) {
         return;
     }
-    errfmt = "Error finishing pbcopy (%d)\n";
+    errarg = "Error finishing pbcopy";
 
  error:
-    raw_printf(errfmt, strerror(errno));
+    raw_printf("%s: %s (%d)\n", errarg, strerror(errno), errno);
 }
 #endif /* __APPLE__ */