]> git.alsa-project.org Git - alsa-utils.git/commitdiff
alsactl, amidi: check write() return values to fix compile warnings
authorJaroslav Kysela <perex@perex.cz>
Fri, 4 Dec 2026 17:51:16 +0000 (18:51 +0100)
committerJaroslav Kysela <perex@perex.cz>
Fri, 4 Dec 2026 17:52:23 +0000 (18:52 +0100)
Building with _FORTIFY_SOURCE emits -Wunused-result warnings because
write() is declared with the warn_unused_result attribute and its
return value was discarded (the (void) cast does not suppress it).

Consume and check the return values instead: in initfailed() report a
failure to open or write the state file, and in amidi's receive loop
report a short/failed write to the receive file and stop.

Closes: https://github.com/alsa-project/alsa-utils/pull/221
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
alsactl/utils.c
amidi/amidi.c

index d0f8940573e895a0e691e589212a2178379af6db..1e809028be445263cea5f6729a234a28711f53be 100644 (file)
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stddef.h>
+#include <string.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <errno.h>
@@ -84,6 +85,7 @@ void initfailed(int cardnumber, const char *reason, int exitcode)
        int fp;
        char *str;
        char sexitcode[16];
+       int err = 0;
 
        if (statefile == NULL)
                return;
@@ -91,12 +93,19 @@ void initfailed(int cardnumber, const char *reason, int exitcode)
                return;
        sprintf(sexitcode, "%i", exitcode);
        fp = open(statefile, O_WRONLY|O_CREAT|O_APPEND, 0644);
-       (void)write(fp, str, strlen(str));
-       (void)write(fp, ":", 1);
-       (void)write(fp, reason, strlen(reason));
-       (void)write(fp, ":", 1);
-       (void)write(fp, sexitcode, strlen(sexitcode));
-       (void)write(fp, "\n", 1);
+       if (fp < 0) {
+               error("unable to open state file %s: %s", statefile, strerror(errno));
+               free(str);
+               return;
+       }
+       err |= write(fp, str, strlen(str)) < 0;
+       err |= write(fp, ":", 1) < 0;
+       err |= write(fp, reason, strlen(reason)) < 0;
+       err |= write(fp, ":", 1) < 0;
+       err |= write(fp, sexitcode, strlen(sexitcode)) < 0;
+       err |= write(fp, "\n", 1) < 0;
+       if (err)
+               error("unable to write to state file %s: %s", statefile, strerror(errno));
        close(fp);
        free(str);
 }
index 9a2aaf7205f0cb657d51cbc789537a56c7dc1f17..02cb619ac1f58dd8f8c1a01f475a82afd12f95f4 100644 (file)
@@ -738,8 +738,11 @@ int main(int argc, char *argv[])
                                continue;
                        read += length;
 
-                       if (receive_file != -1)
-                               write(receive_file, buf, length);
+                       if (receive_file != -1 &&
+                           write(receive_file, buf, length) != length) {
+                               error("cannot write to receive file: %s", strerror(errno));
+                               break;
+                       }
                        if (dump) {
                                for (i = 0; i < length; ++i)
                                        print_byte(buf[i], &ts);