]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/basic/io-util.c
loop-write: do strlen() implicitly if size is specified as SIZE_MAX
[thirdparty/systemd.git] / src / basic / io-util.c
index bf1aab9e3884a8757bb9f9b5aa9483186ce98eeb..25e551604a7ad52bac97e1d17c20c92852922481 100644 (file)
@@ -106,12 +106,24 @@ int loop_read_exact(int fd, void *buf, size_t nbytes, bool do_poll) {
 }
 
 int loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
-        const uint8_t *p = ASSERT_PTR(buf);
+        const uint8_t *p;
 
         assert(fd >= 0);
 
-        if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
-                return -EINVAL;
+        if (nbytes == 0) {
+                static const dummy_t dummy[0];
+                assert_cc(sizeof(dummy) == 0);
+                p = (const void*) dummy; /* Some valid pointer, in case NULL was specified */
+        } else {
+                assert(buf);
+
+                if (nbytes == SIZE_MAX)
+                        nbytes = strlen(buf);
+                else if (_unlikely_(nbytes > (size_t) SSIZE_MAX))
+                        return -EINVAL;
+
+                p = buf;
+        }
 
         do {
                 ssize_t k;