Explorar o código

avoid invalid use of va_arg in open

reading the variadic mode argument is only valid when the O_CREAT flag
is present. this probably does not matter, but is needed for formal
correctness, and could affect LTO or other full-program analysis.
Rich Felker %!s(int64=11) %!d(string=hai) anos
pai
achega
9c2d437cb3
Modificáronse 1 ficheiros con 8 adicións e 5 borrados
  1. 8 5
      src/fcntl/open.c

+ 8 - 5
src/fcntl/open.c

@@ -5,11 +5,14 @@
 
 int open(const char *filename, int flags, ...)
 {
-	mode_t mode;
-	va_list ap;
-	va_start(ap, flags);
-	mode = va_arg(ap, mode_t);
-	va_end(ap);
+	mode_t mode = 0;
+
+	if (flags & O_CREAT) {
+		va_list ap;
+		va_start(ap, flags);
+		mode = va_arg(ap, mode_t);
+		va_end(ap);
+	}
 
 	int fd = __sys_open_cp(filename, flags, mode);
 	if (fd>=0 && (flags & O_CLOEXEC))