Skip to contentSkip to conversation

syscall: fix special case of write(fd, NULL, 0)

PATCH Oliver Smith 2 participants 2 messages Feb 15, 2018

lore
Share & tools
lore
#0 lore raw

Hello there,

I'm a little late to the party. But what is necessary to get this upstreamed, and how can I help?

PS: Sorry if I picked the wrong e-mail addresses, I wasn't subscribed to the ML at that point and used the addresses I could find for the people who answered to the original thread here:

https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg08073.html

Thanks,
Oliver

From: Zhuowei Zhang <address@hidden>

linux-user/syscall.c abi_long do_syscall(void *cpu_env, int num,

Quoted 26 lines

Linux returns success for the special case of calling write with a zero-length NULL buffer: compiling and running

```

int main() {
   ssize_t ret = write(STDOUT_FILENO, NULL, 0);
   fprintf(stderr, "write returned %ld\n", ret);
   return 0;
}
```

gives "write returned 0" when run directly, but "write returned -1" in QEMU.

This commit checks for this situation and returns success if found.

---
 linux-user/syscall.c | 5 +++++
 1 file changed, 5 insertions(+)
linux-user/syscall.c +0 -0
index 9b6364a..ecadf49 100644
@@ -7783,6 +7783,11 @@ abi_long do_syscall(void *cpu_env, int num,

abi_long

Quoted 14 lines
linux-user/syscall.c +5 -0
 arg1,
         }
         break;
     case TARGET_NR_write:
+        if (arg2 == 0 && arg3 == 0) {
+            /* special case: write(fd, NULL, 0) returns success. */
+            ret = 0;
+            break;
+        }
         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
             goto efault;
         if (fd_trans_target_to_host_data(arg1)) {

--
1.9.1

#1 lore raw

Le 15/02/2018 à 00:33, Oliver Smith a écrit :

Quoted 10 lines

Hello there,

I'm a little late to the party. But what is necessary to get this upstreamed, and how can I help?

PS: Sorry if I picked the wrong e-mail addresses, I wasn't subscribed to the ML at that point and used the addresses I could find for the people who answered to the original thread here:

https://lists.gnu.org/archive/html/qemu-devel/2017-09/msg08073.html

According to comments in the ML thread, you need to:

- update the patch to call write() with NULL and 0, something like:

linux-user/syscall.c +0 -0
@@ -7912,6 +7912,10 @@ abi_long do_syscall(void *cpu_env, int num,

abi_long arg1,

         }
         break;
     case TARGET_NR_write:

+ if (arg2 == 0 && arg3 == 0) {
+ ret = get_errno(safe_write(arg1, NULL, 0));
+ break;
+ }

         if (!(p = lock_user(VERIFY_READ, arg2, arg3, 1)))
             goto efault;
         if (fd_trans_target_to_host_data(arg1)) {

- change TARGET_NR_read to do the same

- check if we need to do the same for pread64/pwrite64

Thanks,
Laurent