Merge "<time.h>: change the new C23 TIME_ constants."
diff --git a/libc/bionic/syslog.cpp b/libc/bionic/syslog.cpp
index 6b17d26..a459c6b 100644
--- a/libc/bionic/syslog.cpp
+++ b/libc/bionic/syslog.cpp
@@ -18,18 +18,22 @@
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
+#include <unistd.h>
#include <async_safe/log.h>
static const char* syslog_log_tag = nullptr;
static int syslog_priority_mask = 0xff;
+static int syslog_options = 0;
void closelog() {
syslog_log_tag = nullptr;
+ syslog_options = 0;
}
-void openlog(const char* log_tag, int /*options*/, int /*facility*/) {
+void openlog(const char* log_tag, int options, int /*facility*/) {
syslog_log_tag = log_tag;
+ syslog_options = options;
}
int setlogmask(int new_mask) {
@@ -73,10 +77,16 @@
android_log_priority = ANDROID_LOG_DEBUG;
}
- // We can't let async_safe_format_log do the formatting because it doesn't support
- // all the printf functionality.
+ // We can't let async_safe_format_log do the formatting because it doesn't
+ // support all the printf functionality.
char log_line[1024];
- vsnprintf(log_line, sizeof(log_line), fmt, args);
+ int n = vsnprintf(log_line, sizeof(log_line), fmt, args);
+ if (n < 0) return;
async_safe_format_log(android_log_priority, log_tag, "%s", log_line);
+ if ((syslog_options & LOG_PERROR) != 0) {
+ bool have_newline =
+ (n > 0 && n < static_cast<int>(sizeof(log_line)) && log_line[n - 1] == '\n');
+ dprintf(STDERR_FILENO, "%s: %s%s", log_tag, log_line, have_newline ? "" : "\n");
+ }
}
diff --git a/libc/include/bits/get_device_api_level_inlines.h b/libc/include/bits/get_device_api_level_inlines.h
index d14eb2c..dc5871b 100644
--- a/libc/include/bits/get_device_api_level_inlines.h
+++ b/libc/include/bits/get_device_api_level_inlines.h
@@ -35,8 +35,8 @@
__BEGIN_DECLS
// Avoid circular dependencies since this is exposed from <sys/cdefs.h>.
-int __system_property_get(const char* __name, char* __value);
-int atoi(const char* __s) __attribute_pure__;
+int __system_property_get(const char* _Nonnull __name, char* _Nonnull __value);
+int atoi(const char* _Nonnull __s) __attribute_pure__;
__BIONIC_GET_DEVICE_API_LEVEL_INLINE int android_get_device_api_level() {
char value[92] = { 0 };
diff --git a/libc/include/sys/ucontext.h b/libc/include/sys/ucontext.h
index bb6443b..4f4d5ce 100644
--- a/libc/include/sys/ucontext.h
+++ b/libc/include/sys/ucontext.h
@@ -318,12 +318,20 @@
#if defined(__USE_GNU)
-#define REG_PC 0
-#define REG_RA 1
-#define REG_SP 2
-#define REG_TP 4
-#define REG_S0 8
-#define REG_A0 10
+enum {
+ REG_PC = 0,
+#define REG_PC REG_PC
+ REG_RA = 1,
+#define REG_RA REG_RA
+ REG_SP = 2,
+#define REG_SP REG_SP
+ REG_TP = 4,
+#define REG_TP REG_TP
+ REG_S0 = 8,
+#define REG_S0 REG_S0
+ REG_A0 = 10,
+#define REG_A0 REG_A0
+};
#endif // defined(__USE_GNU)
diff --git a/libc/include/syslog.h b/libc/include/syslog.h
index d89d769..90ea76e 100644
--- a/libc/include/syslog.h
+++ b/libc/include/syslog.h
@@ -112,17 +112,21 @@
*/
#define LOG_UPTO(pri) ((1 << ((pri)+1)) - 1)
-/** openlog() options are currently ignored on Android. */
+/** openlog() option ignored on Android. */
#define LOG_PID 0x01
-/** openlog() options are currently ignored on Android. */
+/** openlog() option ignored on Android. */
#define LOG_CONS 0x02
-/** openlog() options are currently ignored on Android. */
+/** openlog() option ignored on Android. */
#define LOG_ODELAY 0x04
-/** openlog() options are currently ignored on Android. */
+/** openlog() option ignored on Android. */
#define LOG_NDELAY 0x08
-/** openlog() options are currently ignored on Android. */
+/** openlog() option ignored on Android. */
#define LOG_NOWAIT 0x10
-/** openlog() options are currently ignored on Android. */
+/**
+ * openlog() option to log to stderr as well as the system log.
+ *
+ * Available since API level 34 (ignored before then).
+ */
#define LOG_PERROR 0x20
/**
diff --git a/libc/stdio/printf_common.h b/libc/stdio/printf_common.h
index b0055f0..365728b 100644
--- a/libc/stdio/printf_common.h
+++ b/libc/stdio/printf_common.h
@@ -528,17 +528,29 @@
case 'b':
ADDUARG();
break;
- case 'w':
+ case 'w': {
n = 0;
+ bool fast = false;
ch = *fmt++;
+ if (ch == 'f') {
+ fast = true;
+ ch = *fmt++;
+ }
while (is_digit(ch)) {
APPEND_DIGIT(n, ch);
ch = *fmt++;
}
if (n == 64) {
flags |= LLONGINT;
+ } else {
+ if (n != 8 && fast) {
+#if defined(__LP64__)
+ flags |= LLONGINT;
+#endif
+ }
}
goto reswitch;
+ }
default: /* "%?" prints ?, unless ? is NUL */
if (ch == '\0') goto done;
break;
@@ -824,4 +836,15 @@
return convbuf;
}
+ // Trasnlate a fixed size integer argument for the %w/%wf format to a
+ // flag representation. Supported sizes are 8, 16, 32, and 64 so far.
+ // See details in bionic/libc/include/stdint.h
+ static int w_to_flag(int size, bool fast) {
+ static constexpr int fast_size = sizeof(void*) == 8 ? LLONGINT : 0;
+ if (size == 8) return CHARINT;
+ if (size == 16) return fast ? fast_size : SHORTINT;
+ if (size == 32) return fast ? fast_size : 0;
+ if (size == 64) return LLONGINT;
+ __fortify_fatal("%%w%s%d is unsupported", fast ? "f" : "", size);
+ }
};
diff --git a/libc/stdio/vfprintf.cpp b/libc/stdio/vfprintf.cpp
index b7c68dd..994269b 100644
--- a/libc/stdio/vfprintf.cpp
+++ b/libc/stdio/vfprintf.cpp
@@ -521,34 +521,21 @@
_umax = UARG();
base = DEC;
goto nosign;
- case 'w':
+ case 'w': {
n = 0;
+ bool fast = false;
ch = *fmt++;
+ if (ch == 'f') {
+ fast = true;
+ ch = *fmt++;
+ }
while (is_digit(ch)) {
APPEND_DIGIT(n, ch);
ch = *fmt++;
}
- switch (n) {
- case 8: {
- flags |= CHARINT;
- goto reswitch;
- }
- case 16: {
- flags |= SHORTINT;
- goto reswitch;
- }
- case 32: {
- goto reswitch;
- }
- case 64: {
- flags |= LLONGINT;
- goto reswitch;
- }
- default: {
- __fortify_fatal("%%w%d is unsupported", n);
- break;
- }
- }
+ flags |= helpers::w_to_flag(n, fast);
+ goto reswitch;
+ }
case 'X':
xdigs = xdigs_upper;
goto hex;
diff --git a/libc/stdio/vfwprintf.cpp b/libc/stdio/vfwprintf.cpp
index 52ae64b..0caeb2d 100644
--- a/libc/stdio/vfwprintf.cpp
+++ b/libc/stdio/vfwprintf.cpp
@@ -510,34 +510,21 @@
_umax = UARG();
base = DEC;
goto nosign;
- case 'w':
+ case 'w': {
n = 0;
+ bool fast = false;
ch = *fmt++;
+ if (ch == 'f') {
+ fast = true;
+ ch = *fmt++;
+ }
while (is_digit(ch)) {
APPEND_DIGIT(n, ch);
ch = *fmt++;
}
- switch (n) {
- case 8: {
- flags |= CHARINT;
- goto reswitch;
- }
- case 16: {
- flags |= SHORTINT;
- goto reswitch;
- }
- case 32: {
- goto reswitch;
- }
- case 64: {
- flags |= LLONGINT;
- goto reswitch;
- }
- default: {
- __fortify_fatal("%%w%d is unsupported", n);
- break;
- }
- }
+ flags |= helpers::w_to_flag(n, fast);
+ goto reswitch;
+ }
case 'X':
xdigs = xdigs_upper;
goto hex;
diff --git a/tests/Android.bp b/tests/Android.bp
index 1be1ec3..281e29d 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -503,6 +503,7 @@
"sys_vfs_test.cpp",
"sys_wait_test.cpp",
"sys_xattr_test.cpp",
+ "syslog_test.cpp",
"system_properties_test.cpp",
"system_properties_test2.cpp",
"termios_test.cpp",
diff --git a/tests/stdio_test.cpp b/tests/stdio_test.cpp
index 0e267c5..800732f 100644
--- a/tests/stdio_test.cpp
+++ b/tests/stdio_test.cpp
@@ -3415,4 +3415,172 @@
#else
GTEST_SKIP() << "no %w in glibc";
#endif
+}
+
+TEST(STDIO_TEST, snprintf_wf_base) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconstant-conversion"
+#pragma clang diagnostic ignored "-Wformat"
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+ char buf[BUFSIZ];
+ int_fast8_t a = 0b101;
+ snprintf(buf, sizeof(buf), "<%wf8b>", a);
+ EXPECT_STREQ("<101>", buf);
+ int_fast8_t b = 0x12341234'12341234;
+ snprintf(buf, sizeof(buf), "<%wf8x>", b);
+ EXPECT_STREQ("<34>", buf);
+ uint_fast16_t c = 0x11111111'22222222;
+#if defined(__LP64__)
+ snprintf(buf, sizeof(buf), "<%wf16x>", c);
+ EXPECT_STREQ("<1111111122222222>", buf);
+#else
+ snprintf(buf, sizeof(buf), "<%wf16x>", c);
+ EXPECT_STREQ("<22222222>", buf);
+#endif
+ int_fast32_t d = 0x33333333'44444444;
+#if defined(__LP64__)
+ snprintf(buf, sizeof(buf), "<%wf32x>", d);
+ EXPECT_STREQ("<3333333344444444>", buf);
+#else
+ snprintf(buf, sizeof(buf), "<%wf32x>", d);
+ EXPECT_STREQ("<44444444>", buf);
+#endif
+ int_fast64_t e = 0xaaaaaaaa'aaaaaaaa;
+ snprintf(buf, sizeof(buf), "<%wf64x>", e);
+ EXPECT_STREQ("<aaaaaaaaaaaaaaaa>", buf);
+ snprintf(buf, sizeof(buf), "<%wf64X>", e);
+ EXPECT_STREQ("<AAAAAAAAAAAAAAAA>", buf);
+#pragma clang diagnostic pop
+#else
+ GTEST_SKIP() << "no %wf in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, snprintf_wf_arguments_reordering) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wconstant-conversion"
+#pragma clang diagnostic ignored "-Wformat"
+#pragma clang diagnostic ignored "-Wformat-extra-args"
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+ char buf[BUFSIZ];
+ int_fast16_t a = 0x11111111'22222222;
+ int_fast32_t b = 0x33333333'44444444;
+ int_fast32_t c = 0xaaaaaaaa'aaaaaaaa;
+#if defined(__LP64__)
+ snprintf(buf, sizeof(buf), "<%2$wf32x --- %1$wf32b>", c, b);
+ EXPECT_STREQ(
+ "<3333333344444444 --- 1010101010101010101010101010101010101010101010101010101010101010>",
+ buf);
+ snprintf(buf, sizeof(buf), "<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+ EXPECT_STREQ(
+ "<1010101010101010101010101010101010101010101010101010101010101010 --- 1111111122222222 --- "
+ "3333333344444444>",
+ buf);
+#else
+ snprintf(buf, sizeof(buf), "<%2$wf32x --- %1$wf32b>", c, b);
+ EXPECT_STREQ("<44444444 --- 10101010101010101010101010101010>", buf);
+ snprintf(buf, sizeof(buf), "<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+ EXPECT_STREQ("<10101010101010101010101010101010 --- 22222222 --- 44444444>", buf);
+#endif
+#pragma clang diagnostic pop
+#else
+ GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, snprintf_invalid_wf_width) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat"
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+ char buf[BUFSIZ];
+ int_fast32_t a = 100;
+ EXPECT_DEATH(snprintf(buf, sizeof(buf), "%wf20d", &a), "%wf20 is unsupported");
+#pragma clang diagnostic pop
+#else
+ GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, swprintf_wf_base) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+#pragma clang diagnostic ignored "-Wconstant-conversion"
+ wchar_t buf[BUFSIZ];
+ int_fast8_t a = 0b101;
+ swprintf(buf, sizeof(buf), L"<%wf8b>", a);
+ EXPECT_EQ(std::wstring(L"<101>"), buf);
+ int_fast8_t b = 0x12341234'12341234;
+ swprintf(buf, sizeof(buf), L"<%wf8x>", b);
+ EXPECT_EQ(std::wstring(L"<34>"), buf);
+ uint_fast16_t c = 0x11111111'22222222;
+#if defined(__LP64__)
+ swprintf(buf, sizeof(buf), L"<%wf16x>", c);
+ EXPECT_EQ(std::wstring(L"<1111111122222222>"), buf);
+#else
+ swprintf(buf, sizeof(buf), L"<%wf16x>", c);
+ EXPECT_EQ(std::wstring(L"<22222222>"), buf);
+#endif
+ int_fast32_t d = 0x33333333'44444444;
+#if defined(__LP64__)
+ swprintf(buf, sizeof(buf), L"<%wf32x>", d);
+ EXPECT_EQ(std::wstring(L"<3333333344444444>"), buf);
+#else
+ swprintf(buf, sizeof(buf), L"<%wf32x>", d);
+ EXPECT_EQ(std::wstring(L"<44444444>"), buf);
+#endif
+ int_fast64_t e = 0xaaaaaaaa'aaaaaaaa;
+ swprintf(buf, sizeof(buf), L"<%wf64x>", e);
+ EXPECT_EQ(std::wstring(L"<aaaaaaaaaaaaaaaa>"), buf);
+ swprintf(buf, sizeof(buf), L"<%wf64X>", e);
+ EXPECT_EQ(std::wstring(L"<AAAAAAAAAAAAAAAA>"), buf);
+#else
+ GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, swprintf_wf_arguments_reordering) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+#pragma clang diagnostic ignored "-Wformat-extra-args"
+ wchar_t buf[BUFSIZ];
+ int_fast16_t a = 0x11111111'22222222;
+ int_fast32_t b = 0x33333333'44444444;
+ int_fast32_t c = 0xaaaaaaaa'aaaaaaaa;
+#if defined(__LP64__)
+ swprintf(buf, sizeof(buf), L"<%2$wf32x --- %1$wf32b>", c, b);
+ EXPECT_EQ(std::wstring(L"<3333333344444444 --- "
+ L"1010101010101010101010101010101010101010101010101010101010101010>"),
+ buf);
+ swprintf(buf, sizeof(buf), L"<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+ EXPECT_EQ(std::wstring(L"<1010101010101010101010101010101010101010101010101010101010101010 --- "
+ L"1111111122222222 --- 3333333344444444>"),
+ buf);
+#else
+ swprintf(buf, sizeof(buf), L"<%2$wf32x --- %1$wf32b>", c, b);
+ EXPECT_EQ(std::wstring(L"<44444444 --- 10101010101010101010101010101010>"), buf);
+ swprintf(buf, sizeof(buf), L"<%3$wf32b --- %1$wf16x --- %2$wf32x>", a, b, c);
+ EXPECT_EQ(std::wstring(L"<10101010101010101010101010101010 --- 22222222 --- 44444444>"), buf);
+#endif
+#pragma clang diagnostic pop
+#else
+ GTEST_SKIP() << "no %w in glibc";
+#endif
+}
+
+TEST(STDIO_TEST, swprintf_invalid_wf_width) {
+#if defined(__BIONIC__)
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
+ wchar_t buf[BUFSIZ];
+ int_fast32_t a = 100;
+ EXPECT_DEATH(swprintf(buf, sizeof(buf), L"%wf20d", &a), "%wf20 is unsupported");
+#pragma clang diagnostic pop
+#else
+ GTEST_SKIP() << "no %w in glibc";
+#endif
}
\ No newline at end of file
diff --git a/tests/syslog_test.cpp b/tests/syslog_test.cpp
new file mode 100644
index 0000000..3ec3337
--- /dev/null
+++ b/tests/syslog_test.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <syslog.h>
+
+#include <errno.h>
+#include <gtest/gtest.h>
+
+#include "utils.h"
+
+TEST(syslog, syslog_percent_m) {
+ ExecTestHelper eth;
+ eth.Run(
+ [&]() {
+ openlog("foo", LOG_PERROR, LOG_AUTH);
+ errno = EINVAL;
+ syslog(LOG_ERR, "a b c: %m");
+ closelog();
+ exit(0);
+ },
+ 0, "foo: a b c: Invalid argument\n");
+}
+
+TEST(syslog, syslog_empty) {
+ ExecTestHelper eth;
+ eth.Run(
+ [&]() {
+ openlog("foo", LOG_PERROR, LOG_AUTH);
+ errno = EINVAL;
+ syslog(LOG_ERR, "");
+ closelog();
+ exit(0);
+ },
+ 0, "foo: \n");
+}
+
+TEST(syslog, syslog_truncation) {
+ ExecTestHelper eth;
+ eth.Run(
+ [&]() {
+ openlog("bar", LOG_PERROR, LOG_AUTH);
+ char too_long[2048] = {};
+ memset(too_long, 'x', sizeof(too_long) - 1);
+ syslog(LOG_ERR, "%s", too_long);
+ closelog();
+ exit(0);
+ },
+ 0, "bar: x{1023}\n");
+}