FORTIFY_SOURCE: Add openat, fix bug
Add fortify_source support for openat(). This change requires that
an argument be supplied when using O_CREAT.
Fix unnecessary call to __open_2. If, at compile time, we know that
"flags" is constant and DOESN'T contain O_CREAT, the call to __open_2
is useless.
Change-Id: Ifcd29c4fb25e25656961d7552d672e161f0cfdbd
diff --git a/libc/include/fcntl.h b/libc/include/fcntl.h
index 47151f4..77c92be 100644
--- a/libc/include/fcntl.h
+++ b/libc/include/fcntl.h
@@ -59,10 +59,10 @@
* http://clang.llvm.org/docs/UsersManual.html#c_unimpl_gcc
*/
-extern void __open_creat_error()
- __attribute__((__error__ ("open called with O_CREAT, but missing mode")));
-extern void __open_toomanyargs_error()
- __attribute__((__error__ ("open called with too many arguments")));
+extern void __creat_error()
+ __attribute__((__error__ ("called with O_CREAT, but missing mode")));
+extern void __too_many_args_error()
+ __attribute__((__error__ ("too many arguments")));
extern int __open_real(const char *pathname, int flags, ...)
__asm__(__USER_LABEL_PREFIX__ "open");
extern int __open_2(const char *, int);
@@ -71,21 +71,44 @@
int open(const char *pathname, int flags, ...) {
if (__builtin_constant_p(flags)) {
if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
- __open_creat_error(); // compile time error
+ __creat_error(); // compile time error
}
}
if (__builtin_va_arg_pack_len() > 1) {
- __open_toomanyargs_error(); // compile time error
+ __too_many_args_error(); // compile time error
}
- if (__builtin_va_arg_pack_len() == 0) {
+ if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
return __open_2(pathname, flags);
}
return __open_real(pathname, flags, __builtin_va_arg_pack());
}
+extern int __openat_2(int, const char *, int);
+extern int __openat_real(int dirfd, const char *pathname, int flags, ...)
+ __asm__(__USER_LABEL_PREFIX__ "openat");
+
+__BIONIC_FORTIFY_INLINE
+int openat(int dirfd, const char *pathname, int flags, ...) {
+ if (__builtin_constant_p(flags)) {
+ if ((flags & O_CREAT) && __builtin_va_arg_pack_len() == 0) {
+ __creat_error(); // compile time error
+ }
+ }
+
+ if (__builtin_va_arg_pack_len() > 1) {
+ __too_many_args_error(); // compile time error
+ }
+
+ if ((__builtin_va_arg_pack_len() == 0) && !__builtin_constant_p(flags)) {
+ return __openat_2(dirfd, pathname, flags);
+ }
+
+ return __openat_real(dirfd, pathname, flags, __builtin_va_arg_pack());
+}
+
#endif /* !defined(__clang__) */
#endif /* defined(__BIONIC_FORTIFY_INLINE) */