ssh_client: open: fix handling of the mode vararg
The standard says that random errors might occur if va_arg is used but
no additional argument was provided. You might get garbage, or a crash,
or other fun stuff. Fix the open syscall wrapper to DTRT.
Note: I didn't observe any specific misbehavior here, but better to do
what the standard says rather than hope for the best.
Change-Id: I16ae99537440844ba15dacd410b3903adff11e37
Reviewed-on: https://chromium-review.googlesource.com/466906
Reviewed-by: Brandon Gilmore <varz@google.com>
Reviewed-by: Rob Ginda <rginda@chromium.org>
Tested-by: Mike Frysinger <vapier@chromium.org>
diff --git a/chromeapps/ssh_client/src/syscalls.cc b/chromeapps/ssh_client/src/syscalls.cc
index 954c01c..ae21bea 100644
--- a/chromeapps/ssh_client/src/syscalls.cc
+++ b/chromeapps/ssh_client/src/syscalls.cc
@@ -82,14 +82,21 @@
}
#ifdef USE_NEWLIB
+# ifndef O_TMPFILE
+# define O_TMPFILE 0
+# endif
int open(const char *file, int oflag, ...) {
int newfd;
- va_list ap;
- mode_t cmode;
+ mode_t cmode = 0;
- va_start(ap, oflag);
- cmode = va_arg(ap, mode_t);
- va_end(ap);
+ // Only peel off the mode if the call requires it. Otherwise we enter
+ // "undefined" territory and get garbage or a crash or ...
+ if (oflag & (O_CREAT | O_TMPFILE)) {
+ va_list ap;
+ va_start(ap, oflag);
+ cmode = va_arg(ap, mode_t);
+ va_end(ap);
+ }
return HANDLE_ERRNO(WRAP(open)(file, oflag, cmode, &newfd), newfd);
}
#endif