blob: 590379cb608f36d36b2e57d081813db08d02e38c [file] [log] [blame]
Elliott Hughes91875dc2012-09-24 17:55:15 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Elliott Hughesc9244bd2014-05-14 13:31:35 -070020#include <fcntl.h>
Elliott Hughes1d13c642013-09-23 16:02:39 -070021#include <limits.h>
Colin Cross14d15072021-08-16 16:35:27 -070022#include <locale.h>
Elliott Hughes7823f322014-04-14 12:11:28 -070023#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070024#include <stdio.h>
Colin Cross4c5595c2021-08-16 15:51:59 -070025#include <sys/cdefs.h>
Elliott Hughes468efc82018-07-10 14:39:49 -070026#include <sys/socket.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070027#include <sys/stat.h>
Elliott Hughesbe78fc92022-07-28 20:58:45 +000028#include <sys/sysinfo.h>
Colin Cross14d15072021-08-16 16:35:27 -070029#include <sys/types.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070030#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070031#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010032
Elliott Hughes3a4c4542017-07-19 17:20:24 -070033#include <string>
Ryan Prichardc485cdb2019-04-30 14:47:34 -070034#include <thread>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080035#include <vector>
36
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080037#include <android-base/file.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070038#include <android-base/silent_death_test.h>
Elliott Hughes439ebbd2020-12-04 18:51:42 -080039#include <android-base/test_utils.h>
Elliott Hughes05b675e2019-04-17 13:01:06 -070040#include <android-base/unique_fd.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080041
Josh Gao2f06e102017-01-10 13:00:37 -080042#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070043
Elliott Hughes05b675e2019-04-17 13:01:06 -070044// This #include is actually a test too. We have to duplicate the
45// definitions of the RENAME_ constants because <linux/fs.h> also contains
46// pollution such as BLOCK_SIZE which conflicts with lots of user code.
47// Important to check that we have matching definitions.
48// There's no _MAX to test that we have all the constants, sadly.
49#include <linux/fs.h>
50
Christopher Ferris13f26a72016-01-13 13:47:58 -080051#if defined(NOFORTIFY)
52#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070053#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080054#else
55#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070056#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080057#endif
58
Elliott Hughes3a4c4542017-07-19 17:20:24 -070059using namespace std::string_literals;
60
Elliott Hughes141b9172021-04-09 17:13:09 -070061using stdio_DeathTest = SilentDeathTest;
62using stdio_nofortify_DeathTest = SilentDeathTest;
Elliott Hughesfb3873d2016-08-10 11:07:54 -070063
Elliott Hughes33a8cb12017-07-25 18:06:46 -070064static void SetFileTo(const char* path, const char* content) {
65 FILE* fp;
66 ASSERT_NE(nullptr, fp = fopen(path, "w"));
67 ASSERT_NE(EOF, fputs(content, fp));
68 ASSERT_EQ(0, fclose(fp));
69}
70
71static void AssertFileIs(const char* path, const char* expected) {
72 FILE* fp;
73 ASSERT_NE(nullptr, fp = fopen(path, "r"));
74 char* line = nullptr;
75 size_t length;
76 ASSERT_NE(EOF, getline(&line, &length, fp));
77 ASSERT_EQ(0, fclose(fp));
78 ASSERT_STREQ(expected, line);
79 free(line);
80}
81
Elliott Hughes70715da2016-08-01 16:35:17 -070082static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
83 rewind(fp);
84
85 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080086 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070087 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
88 ASSERT_STREQ(expected, line);
89
90 if (is_fmemopen) {
91 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
92 // extra empty line, but does on every C library I tested...
93 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
94 ASSERT_STREQ("", line);
95 }
96
97 // Make sure there isn't anything else in the file.
98 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
99}
100
Christopher Ferris13f26a72016-01-13 13:47:58 -0800101TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800102 // Check that we have a _recursive_ mutex for flockfile.
103 flockfile(stderr);
104 feof(stderr); // We don't care about the result, but this needs to take the lock.
105 funlockfile(stderr);
106}
107
Christopher Ferris13f26a72016-01-13 13:47:58 -0800108TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800109 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
110 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700111 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800112 flockfile(fp);
113 feof(fp);
114 funlockfile(fp);
115 fclose(fp);
116}
117
Christopher Ferris13f26a72016-01-13 13:47:58 -0800118TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700119 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700120 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700121
122 int fd = fileno(fp);
123 ASSERT_NE(fd, -1);
124
125 struct stat sb;
126 int rc = fstat(fd, &sb);
127 ASSERT_NE(rc, -1);
128 ASSERT_EQ(sb.st_mode & 0777, 0600U);
129
130 rc = fprintf(fp, "hello\n");
131 ASSERT_EQ(rc, 6);
132
Elliott Hughes70715da2016-08-01 16:35:17 -0700133 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700134 fclose(fp);
135}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300136
Elliott Hughesf226ee52016-02-03 11:24:28 -0800137TEST(STDIO_TEST, tmpfile64) {
138 FILE* fp = tmpfile64();
139 ASSERT_TRUE(fp != nullptr);
140 fclose(fp);
141}
142
Christopher Ferris13f26a72016-01-13 13:47:58 -0800143TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100144 TemporaryFile tf;
145
146 int rc = dprintf(tf.fd, "hello\n");
147 ASSERT_EQ(rc, 6);
148
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800149 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700150 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700151 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100152
Elliott Hughes70715da2016-08-01 16:35:17 -0700153 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700154 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100155}
156
Christopher Ferris13f26a72016-01-13 13:47:58 -0800157TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300158 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700159 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300160
161 const char* line_written = "This is a test";
162 int rc = fprintf(fp, "%s", line_written);
163 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
164
165 rewind(fp);
166
Yi Kong32bc0fc2018-08-02 17:31:13 -0700167 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300168 size_t allocated_length = 0;
169
170 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
171 for (size_t i = 0; i < 5; ++i) {
172 ASSERT_FALSE(feof(fp));
173 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
174 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800175 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300176 }
177 // The last read should have set the end-of-file indicator for the stream.
178 ASSERT_TRUE(feof(fp));
179 clearerr(fp);
180
181 // getdelim returns -1 but doesn't set errno if we're already at EOF.
182 // It should set the end-of-file indicator for the stream, though.
183 errno = 0;
184 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800185 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300186 ASSERT_TRUE(feof(fp));
187
188 free(word_read);
189 fclose(fp);
190}
191
Christopher Ferris13f26a72016-01-13 13:47:58 -0800192TEST(STDIO_TEST, getdelim_invalid) {
zijunzhao00a3dba2023-03-01 00:07:40 +0000193#pragma clang diagnostic push
194#pragma clang diagnostic ignored "-Wnonnull"
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300195 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700196 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300197
Yi Kong32bc0fc2018-08-02 17:31:13 -0700198 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300199 size_t buffer_length = 0;
200
201 // The first argument can't be NULL.
202 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700203 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800204 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300205
206 // The second argument can't be NULL.
207 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700208 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800209 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700210 fclose(fp);
zijunzhao00a3dba2023-03-01 00:07:40 +0000211#pragma clang diagnostic pop
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300212}
213
Christopher Ferris13f26a72016-01-13 13:47:58 -0800214TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700215 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700216 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700217 char* word_read;
218 size_t allocated_length;
219 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
220 fclose(fp);
221}
222
Christopher Ferris13f26a72016-01-13 13:47:58 -0800223TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300224 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700225 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300226
227 const char* line_written = "This is a test for getline\n";
228 const size_t line_count = 5;
229
230 for (size_t i = 0; i < line_count; ++i) {
231 int rc = fprintf(fp, "%s", line_written);
232 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
233 }
234
235 rewind(fp);
236
Yi Kong32bc0fc2018-08-02 17:31:13 -0700237 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300238 size_t allocated_length = 0;
239
240 size_t read_line_count = 0;
241 ssize_t read_char_count;
242 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
243 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
244 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800245 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300246 ++read_line_count;
247 }
248 ASSERT_EQ(read_line_count, line_count);
249
250 // The last read should have set the end-of-file indicator for the stream.
251 ASSERT_TRUE(feof(fp));
252 clearerr(fp);
253
254 // getline returns -1 but doesn't set errno if we're already at EOF.
255 // It should set the end-of-file indicator for the stream, though.
256 errno = 0;
257 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800258 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300259 ASSERT_TRUE(feof(fp));
260
261 free(line_read);
262 fclose(fp);
263}
264
Christopher Ferris13f26a72016-01-13 13:47:58 -0800265TEST(STDIO_TEST, getline_invalid) {
zijunzhao00a3dba2023-03-01 00:07:40 +0000266#pragma clang diagnostic push
267#pragma clang diagnostic ignored "-Wnonnull"
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300268 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700269 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300270
Yi Kong32bc0fc2018-08-02 17:31:13 -0700271 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300272 size_t buffer_length = 0;
273
274 // The first argument can't be NULL.
275 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700276 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800277 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300278
279 // The second argument can't be NULL.
280 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700281 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800282 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700283 fclose(fp);
zijunzhao00a3dba2023-03-01 00:07:40 +0000284#pragma clang diagnostic pop
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300285}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000286
Christopher Ferris13f26a72016-01-13 13:47:58 -0800287TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800288 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800289 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800290 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
291 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000292 // error: format '%zd' expects argument of type 'signed size_t',
293 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
294 ssize_t v = 1;
295 char buf[32];
296 snprintf(buf, sizeof(buf), "%zd", v);
297}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800298
Elliott Hughes05493712014-04-17 17:30:03 -0700299// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800300TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700301 char buf[BUFSIZ];
302 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
303 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
304}
305
Elliott Hughescdb4a262020-06-05 16:56:53 -0700306// http://b/152588929
307TEST(STDIO_TEST, snprintf_La) {
308#if defined(__LP64__)
309 char buf[BUFSIZ];
310 union {
311 uint64_t a[2];
312 long double v;
313 } u;
314
315 u.a[0] = UINT64_C(0x9b9b9b9b9b9b9b9b);
316 u.a[1] = UINT64_C(0xdfdfdfdfdfdfdfdf);
317 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
318 EXPECT_STREQ("<-0x1.dfdfdfdfdfdf9b9b9b9b9b9b9b9bp+8160>", buf);
319
320 u.a[0] = UINT64_C(0xffffffffffffffff);
321 u.a[1] = UINT64_C(0x7ffeffffffffffff);
322 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
323 EXPECT_STREQ("<0x1.ffffffffffffffffffffffffffffp+16383>", buf);
324
325 u.a[0] = UINT64_C(0x0000000000000000);
326 u.a[1] = UINT64_C(0x0000000000000000);
327 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%La>", u.v));
328 EXPECT_STREQ("<0x0p+0>", buf);
329#else
330 GTEST_SKIP() << "no ld128";
331#endif
332}
333
Christopher Ferris13f26a72016-01-13 13:47:58 -0800334TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700335 char buf[BUFSIZ];
336 wint_t wc = L'a';
337 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
338 EXPECT_STREQ("<a>", buf);
339}
340
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700341TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
342 char buf[BUFSIZ];
343 wchar_t wc = L'a';
344 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
345 EXPECT_STREQ("<a>", buf);
346}
347
Christopher Ferris13f26a72016-01-13 13:47:58 -0800348TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700349 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700350 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700351 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
352 EXPECT_STREQ("<(null)>", buf);
353
354 wchar_t chars[] = { L'h', L'i', 0 };
355 ws = chars;
356 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
357 EXPECT_STREQ("<hi>", buf);
358}
359
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700360TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
361 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700362 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700363 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
364 EXPECT_STREQ("<(null)>", buf);
365
366 wchar_t chars[] = { L'h', L'i', 0 };
367 ws = chars;
368 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
369 EXPECT_STREQ("<hi>", buf);
370}
371
Elliott Hughese657eb42021-02-18 17:11:56 -0800372TEST_F(STDIO_DEATHTEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700373#if defined(__BIONIC__)
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000374#pragma clang diagnostic push
375#pragma clang diagnostic ignored "-Wformat"
Elliott Hughes41398d02018-03-07 13:32:58 -0800376 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700377 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700378 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800379 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughesb813a6a2022-08-01 22:18:40 +0000380#pragma clang diagnostic pop
Elliott Hughese2341d02014-05-02 18:16:32 -0700381#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800382 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700383#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700384}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700385
Elliott Hughes7cebf832020-08-12 14:25:41 -0700386TEST(STDIO_TEST, snprintf_measure) {
387 char buf[16];
388 ASSERT_EQ(11, snprintf(buf, 0, "Hello %s", "world"));
389}
390
Christopher Ferris13f26a72016-01-13 13:47:58 -0800391TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700392 char buf[BUFSIZ];
393
394 snprintf(buf, sizeof(buf), "a");
395 EXPECT_STREQ("a", buf);
396
397 snprintf(buf, sizeof(buf), "%%");
398 EXPECT_STREQ("%", buf);
399
400 snprintf(buf, sizeof(buf), "01234");
401 EXPECT_STREQ("01234", buf);
402
403 snprintf(buf, sizeof(buf), "a%sb", "01234");
404 EXPECT_STREQ("a01234b", buf);
405
Yi Kong32bc0fc2018-08-02 17:31:13 -0700406 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700407 snprintf(buf, sizeof(buf), "a%sb", s);
408 EXPECT_STREQ("a(null)b", buf);
409
410 snprintf(buf, sizeof(buf), "aa%scc", "bb");
411 EXPECT_STREQ("aabbcc", buf);
412
413 snprintf(buf, sizeof(buf), "a%cc", 'b');
414 EXPECT_STREQ("abc", buf);
415
416 snprintf(buf, sizeof(buf), "a%db", 1234);
417 EXPECT_STREQ("a1234b", buf);
418
419 snprintf(buf, sizeof(buf), "a%db", -8123);
420 EXPECT_STREQ("a-8123b", buf);
421
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700422 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700423 EXPECT_STREQ("a16b", buf);
424
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700425 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700426 EXPECT_STREQ("a16b", buf);
427
428 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
429 EXPECT_STREQ("a68719476736b", buf);
430
431 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
432 EXPECT_STREQ("a70000b", buf);
433
434 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
435 EXPECT_STREQ("a0xb0001234b", buf);
436
437 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
438 EXPECT_STREQ("a12abz", buf);
439
440 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
441 EXPECT_STREQ("a12ABz", buf);
442
443 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
444 EXPECT_STREQ("a00123456z", buf);
445
446 snprintf(buf, sizeof(buf), "a%5dz", 1234);
447 EXPECT_STREQ("a 1234z", buf);
448
449 snprintf(buf, sizeof(buf), "a%05dz", 1234);
450 EXPECT_STREQ("a01234z", buf);
451
452 snprintf(buf, sizeof(buf), "a%8dz", 1234);
453 EXPECT_STREQ("a 1234z", buf);
454
455 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
456 EXPECT_STREQ("a1234 z", buf);
457
458 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
459 EXPECT_STREQ("Aabcdef Z", buf);
460
461 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
462 EXPECT_STREQ("Ahello:1234Z", buf);
463
464 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
465 EXPECT_STREQ("a005:5:05z", buf);
466
Yi Kong32bc0fc2018-08-02 17:31:13 -0700467 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700468 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700469#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700470 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800471#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700472 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800473#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700474
475 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
476 EXPECT_STREQ("a68719476736,6,7,8z", buf);
477
478 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
479 EXPECT_STREQ("a_1.230000_b", buf);
480
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700481 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700482 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400483
484 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
485 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700486}
487
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800488template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700489static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
490 int sscanf_fn(const T*, const T*, ...),
491 const T* fmt_string, const T* fmt, const T* fmt_plus,
492 const T* minus_inf, const T* inf_, const T* plus_inf,
493 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800494 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700495 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700496
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700497 // NaN.
498
Elliott Hughese0a9a382022-10-25 22:56:43 +0000499 snprintf_fn(buf, sizeof(buf), fmt, nan(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800500 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700501 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
502 EXPECT_TRUE(isnan(f));
503
Elliott Hughese0a9a382022-10-25 22:56:43 +0000504 snprintf_fn(buf, sizeof(buf), fmt, -nan(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800505 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700506 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
507 EXPECT_TRUE(isnan(f));
508
Elliott Hughese0a9a382022-10-25 22:56:43 +0000509 snprintf_fn(buf, sizeof(buf), fmt_plus, nan(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800510 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700511 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
512 EXPECT_TRUE(isnan(f));
513
Elliott Hughese0a9a382022-10-25 22:56:43 +0000514 snprintf_fn(buf, sizeof(buf), fmt_plus, -nan(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800515 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700516 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
517 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800518
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700519 // Inf.
520
521 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800522 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700523 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
524 EXPECT_EQ(HUGE_VALF, f);
525
526 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800527 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700528 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
529 EXPECT_EQ(-HUGE_VALF, f);
530
531 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800532 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700533 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
534 EXPECT_EQ(HUGE_VALF, f);
535
536 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800537 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700538 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
539 EXPECT_EQ(-HUGE_VALF, f);
540
541 // Check case-insensitivity.
542 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
543 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
544 EXPECT_EQ(HUGE_VALF, f);
545 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
546 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
547 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700548}
549
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700550TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
551 CheckInfNan(snprintf, sscanf, "%s",
552 "[%a]", "[%+a]",
553 "[-inf]", "[inf]", "[+inf]",
554 "[-nan]", "[nan]", "[+nan]");
555 CheckInfNan(snprintf, sscanf, "%s",
556 "[%A]", "[%+A]",
557 "[-INF]", "[INF]", "[+INF]",
558 "[-NAN]", "[NAN]", "[+NAN]");
559 CheckInfNan(snprintf, sscanf, "%s",
560 "[%e]", "[%+e]",
561 "[-inf]", "[inf]", "[+inf]",
562 "[-nan]", "[nan]", "[+nan]");
563 CheckInfNan(snprintf, sscanf, "%s",
564 "[%E]", "[%+E]",
565 "[-INF]", "[INF]", "[+INF]",
566 "[-NAN]", "[NAN]", "[+NAN]");
567 CheckInfNan(snprintf, sscanf, "%s",
568 "[%f]", "[%+f]",
569 "[-inf]", "[inf]", "[+inf]",
570 "[-nan]", "[nan]", "[+nan]");
571 CheckInfNan(snprintf, sscanf, "%s",
572 "[%F]", "[%+F]",
573 "[-INF]", "[INF]", "[+INF]",
574 "[-NAN]", "[NAN]", "[+NAN]");
575 CheckInfNan(snprintf, sscanf, "%s",
576 "[%g]", "[%+g]",
577 "[-inf]", "[inf]", "[+inf]",
578 "[-nan]", "[nan]", "[+nan]");
579 CheckInfNan(snprintf, sscanf, "%s",
580 "[%G]", "[%+G]",
581 "[-INF]", "[INF]", "[+INF]",
582 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800583}
Elliott Hughes7823f322014-04-14 12:11:28 -0700584
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700585TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
586 CheckInfNan(swprintf, swscanf, L"%s",
587 L"[%a]", L"[%+a]",
588 L"[-inf]", L"[inf]", L"[+inf]",
589 L"[-nan]", L"[nan]", L"[+nan]");
590 CheckInfNan(swprintf, swscanf, L"%s",
591 L"[%A]", L"[%+A]",
592 L"[-INF]", L"[INF]", L"[+INF]",
593 L"[-NAN]", L"[NAN]", L"[+NAN]");
594 CheckInfNan(swprintf, swscanf, L"%s",
595 L"[%e]", L"[%+e]",
596 L"[-inf]", L"[inf]", L"[+inf]",
597 L"[-nan]", L"[nan]", L"[+nan]");
598 CheckInfNan(swprintf, swscanf, L"%s",
599 L"[%E]", L"[%+E]",
600 L"[-INF]", L"[INF]", L"[+INF]",
601 L"[-NAN]", L"[NAN]", L"[+NAN]");
602 CheckInfNan(swprintf, swscanf, L"%s",
603 L"[%f]", L"[%+f]",
604 L"[-inf]", L"[inf]", L"[+inf]",
605 L"[-nan]", L"[nan]", L"[+nan]");
606 CheckInfNan(swprintf, swscanf, L"%s",
607 L"[%F]", L"[%+F]",
608 L"[-INF]", L"[INF]", L"[+INF]",
609 L"[-NAN]", L"[NAN]", L"[+NAN]");
610 CheckInfNan(swprintf, swscanf, L"%s",
611 L"[%g]", L"[%+g]",
612 L"[-inf]", L"[inf]", L"[+inf]",
613 L"[-nan]", L"[nan]", L"[+nan]");
614 CheckInfNan(swprintf, swscanf, L"%s",
615 L"[%G]", L"[%+G]",
616 L"[-INF]", L"[INF]", L"[+INF]",
617 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700618}
619
Dan Albert9601f162017-08-09 14:59:06 -0700620TEST(STDIO_TEST, swprintf) {
621 constexpr size_t nchars = 32;
622 wchar_t buf[nchars];
623
624 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
625 ASSERT_EQ(std::wstring(L"ab"), buf);
626 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
627 ASSERT_EQ(std::wstring(L"abcde"), buf);
628
629 // Unlike swprintf(), swprintf() returns -1 in case of truncation
630 // and doesn't necessarily zero-terminate the output!
631 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
632
633 const char kString[] = "Hello, World";
634 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
635 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
636 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
637 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
638}
639
640TEST(STDIO_TEST, swprintf_a) {
641 constexpr size_t nchars = 32;
642 wchar_t buf[nchars];
643
644 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
645 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
646}
647
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700648TEST(STDIO_TEST, swprintf_lc) {
649 constexpr size_t nchars = 32;
650 wchar_t buf[nchars];
651
652 wint_t wc = L'a';
653 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
654 EXPECT_EQ(std::wstring(L"<a>"), buf);
655}
656
657TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
658 constexpr size_t nchars = 32;
659 wchar_t buf[nchars];
660
661 wint_t wc = L'a';
662 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
663 EXPECT_EQ(std::wstring(L"<a>"), buf);
664}
665
Elliott Hughes618303c2017-11-02 16:58:44 -0700666TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
667 constexpr size_t nchars = 32;
668 wchar_t buf[nchars];
669
670 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
671 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
672}
673
674TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
675 constexpr size_t nchars = 32;
676 wchar_t buf[nchars];
677
678 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
679 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
680}
681
682TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
683 constexpr size_t nchars = 32;
684 wchar_t buf[nchars];
685
686 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
687 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
688}
689
690TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
691 constexpr size_t nchars = 32;
692 wchar_t buf[nchars];
693
694 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
695 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
696}
697
Dan Albert9601f162017-08-09 14:59:06 -0700698TEST(STDIO_TEST, swprintf_ls) {
699 constexpr size_t nchars = 32;
700 wchar_t buf[nchars];
701
702 static const wchar_t kWideString[] = L"Hello\uff41 World";
703 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
704 ASSERT_EQ(std::wstring(kWideString), buf);
705 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
706 ASSERT_EQ(std::wstring(kWideString), buf);
707}
708
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700709TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
710 constexpr size_t nchars = 32;
711 wchar_t buf[nchars];
712
713 static const wchar_t kWideString[] = L"Hello\uff41 World";
714 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
715 ASSERT_EQ(std::wstring(kWideString), buf);
716 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
717 ASSERT_EQ(std::wstring(kWideString), buf);
718}
719
Christopher Ferris13f26a72016-01-13 13:47:58 -0800720TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700721 char buf[BUFSIZ];
722 snprintf(buf, sizeof(buf), "%d", INT_MAX);
723 EXPECT_STREQ("2147483647", buf);
724}
725
Christopher Ferris13f26a72016-01-13 13:47:58 -0800726TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700727 char buf[BUFSIZ];
728 snprintf(buf, sizeof(buf), "%d", INT_MIN);
729 EXPECT_STREQ("-2147483648", buf);
730}
731
Elliott Hughes618303c2017-11-02 16:58:44 -0700732TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
733 char buf[BUFSIZ];
734 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
735 EXPECT_STREQ("9223372036854775807", buf);
736}
737
738TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
739 char buf[BUFSIZ];
740 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
741 EXPECT_STREQ("-9223372036854775808", buf);
742}
743
744TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
745 char buf[BUFSIZ];
746 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
747 EXPECT_STREQ("18446744073709551615", buf);
748}
749
750TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
751 char buf[BUFSIZ];
752 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
753 EXPECT_STREQ("18446744073709551615", buf);
754}
755
Christopher Ferris13f26a72016-01-13 13:47:58 -0800756TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700757 char buf[BUFSIZ];
758 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700759#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700760 EXPECT_STREQ("9223372036854775807", buf);
761#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700762 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700763#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700764}
765
Christopher Ferris13f26a72016-01-13 13:47:58 -0800766TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700767 char buf[BUFSIZ];
768 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700769#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700770 EXPECT_STREQ("-9223372036854775808", buf);
771#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700772 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700773#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700774}
775
Christopher Ferris13f26a72016-01-13 13:47:58 -0800776TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700777 char buf[BUFSIZ];
778 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
779 EXPECT_STREQ("9223372036854775807", buf);
780}
781
Christopher Ferris13f26a72016-01-13 13:47:58 -0800782TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700783 char buf[BUFSIZ];
784 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
785 EXPECT_STREQ("-9223372036854775808", buf);
786}
787
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700788TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
789 char buf[BUFSIZ];
790 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
791 EXPECT_STREQ("37777777777", buf);
792}
793
794TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
795 char buf[BUFSIZ];
796 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
797 EXPECT_STREQ("4294967295", buf);
798}
799
800TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
801 char buf[BUFSIZ];
802 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
803 EXPECT_STREQ("ffffffff", buf);
804}
805
806TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
807 char buf[BUFSIZ];
808 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
809 EXPECT_STREQ("FFFFFFFF", buf);
810}
811
Christopher Ferris13f26a72016-01-13 13:47:58 -0800812TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700813 char buf[BUFSIZ];
814
815 snprintf(buf, sizeof(buf), "%e", 1.5);
816 EXPECT_STREQ("1.500000e+00", buf);
817
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800818 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700819 EXPECT_STREQ("1.500000e+00", buf);
820}
821
Christopher Ferris13f26a72016-01-13 13:47:58 -0800822TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700823 char buf[BUFSIZ];
824
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800825 snprintf(buf, sizeof(buf), "%e", -0.0);
826 EXPECT_STREQ("-0.000000e+00", buf);
827 snprintf(buf, sizeof(buf), "%E", -0.0);
828 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700829 snprintf(buf, sizeof(buf), "%f", -0.0);
830 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800831 snprintf(buf, sizeof(buf), "%F", -0.0);
832 EXPECT_STREQ("-0.000000", buf);
833 snprintf(buf, sizeof(buf), "%g", -0.0);
834 EXPECT_STREQ("-0", buf);
835 snprintf(buf, sizeof(buf), "%G", -0.0);
836 EXPECT_STREQ("-0", buf);
837 snprintf(buf, sizeof(buf), "%a", -0.0);
838 EXPECT_STREQ("-0x0p+0", buf);
839 snprintf(buf, sizeof(buf), "%A", -0.0);
840 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700841}
842
Christopher Ferris13f26a72016-01-13 13:47:58 -0800843TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700844 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700845 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700846
Elliott Hughes69f05d22014-06-05 20:10:09 -0700847 // http://b/15439554
848 char buf[BUFSIZ];
849
850 // 1-byte character.
851 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
852 EXPECT_STREQ("1x2", buf);
853 // 2-byte character.
854 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
855 EXPECT_STREQ("1¢2", buf);
856 // 3-byte character.
857 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
858 EXPECT_STREQ("1€2", buf);
859 // 4-byte character.
860 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
861 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700862
Wally Yaua40fdbd2014-08-26 09:47:23 -0700863 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700864 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700865}
866
Elliott Hughes43f7c872016-02-05 11:18:41 -0800867static void* snprintf_small_stack_fn(void*) {
868 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
869 char buf[PATH_MAX];
870 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
871 return nullptr;
872}
873
874TEST(STDIO_TEST, snprintf_small_stack) {
875 // Is it safe to call snprintf on a thread with a small stack?
876 // (The snprintf implementation puts some pretty large buffers on the stack.)
877 pthread_attr_t a;
878 ASSERT_EQ(0, pthread_attr_init(&a));
879 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
880
881 pthread_t t;
882 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
883 ASSERT_EQ(0, pthread_join(t, nullptr));
884}
885
Elliott Hughes8200e552016-02-05 21:57:37 -0800886TEST(STDIO_TEST, snprintf_asterisk_overflow) {
887 char buf[128];
888 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
889 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
890 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
891 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
892 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
893
894 // INT_MAX-1, INT_MAX, INT_MAX+1.
895 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
896 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
897 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
898 ASSERT_EQ(ENOMEM, errno);
899}
900
Elliott Hughes5dc31302020-01-07 08:48:10 -0800901// Inspired by https://github.com/landley/toybox/issues/163.
902TEST(STDIO_TEST, printf_NULL) {
903 char buf[128];
904 char* null = nullptr;
905 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 2, null));
906 EXPECT_STREQ("<(n>", buf);
907 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 8, null));
908 EXPECT_STREQ("<(null)>", buf);
909 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 2, null));
910 EXPECT_STREQ("< (n>", buf);
911 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 8, null));
912 EXPECT_STREQ("< (null)>", buf);
913}
914
Elliott Hughes70715da2016-08-01 16:35:17 -0700915TEST(STDIO_TEST, fprintf) {
916 TemporaryFile tf;
917
918 FILE* tfile = fdopen(tf.fd, "r+");
919 ASSERT_TRUE(tfile != nullptr);
920
921 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
922 AssertFileIs(tfile, "123 abc");
923 fclose(tfile);
924}
925
Christopher Ferris13f26a72016-01-13 13:47:58 -0800926TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700927 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700928 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700929 int fd_rdonly = open("/dev/null", O_RDONLY);
930 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700931
932 // Unbuffered case where the fprintf(3) itself fails.
933 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700934 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700935 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700936 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700937 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700938 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700939
940 // Buffered case where we won't notice until the fclose(3).
941 // It's likely this is what was actually seen in http://b/7229520,
942 // and that expecting fprintf to fail is setting yourself up for
943 // disappointment. Remember to check fclose(3)'s return value, kids!
944 ASSERT_NE(nullptr, fp = tmpfile());
945 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700946 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700947 ASSERT_EQ(4, fprintf(fp, "fail"));
948 ASSERT_EQ(-1, fclose(fp));
949}
950
Elliott Hughes468efc82018-07-10 14:39:49 -0700951TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800952 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700953 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800954
955 char buf[16];
956 char* s = fgets(buf, sizeof(buf), fp);
957 buf[13] = '\0';
958 ASSERT_STREQ("Linux version", s);
959
960 ASSERT_EQ(0, pclose(fp));
961}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700962
Elliott Hughes468efc82018-07-10 14:39:49 -0700963TEST(STDIO_TEST, popen_socketpair) {
964 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700965 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700966
967 fputs("hello\nworld\n", fp);
968 fflush(fp);
969
970 char buf[16];
971 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
972 EXPECT_STREQ("hello\n", buf);
973 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
974 EXPECT_STREQ("world\n", buf);
975
976 ASSERT_EQ(0, pclose(fp));
977}
978
979TEST(STDIO_TEST, popen_socketpair_shutdown) {
980 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700981 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700982
983 fputs("a\na\na\na\nb\n", fp);
984 fflush(fp);
985 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
986
987 char buf[16];
988 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
989 EXPECT_STREQ(" 4 a\n", buf);
990 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
991 EXPECT_STREQ(" 1 b\n", buf);
992
993 ASSERT_EQ(0, pclose(fp));
994}
995
996TEST(STDIO_TEST, popen_return_value_0) {
997 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700998 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700999 int status = pclose(fp);
1000 EXPECT_TRUE(WIFEXITED(status));
1001 EXPECT_EQ(0, WEXITSTATUS(status));
1002}
1003
1004TEST(STDIO_TEST, popen_return_value_1) {
1005 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001006 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001007 int status = pclose(fp);
1008 EXPECT_TRUE(WIFEXITED(status));
1009 EXPECT_EQ(1, WEXITSTATUS(status));
1010}
1011
1012TEST(STDIO_TEST, popen_return_value_signal) {
1013 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001014 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001015 int status = pclose(fp);
1016 EXPECT_TRUE(WIFSIGNALED(status));
1017 EXPECT_EQ(7, WTERMSIG(status));
1018}
1019
Christopher Ferris13f26a72016-01-13 13:47:58 -08001020TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001021 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001022 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001023 ASSERT_EQ('L', getc(fp));
1024 ASSERT_EQ('i', getc(fp));
1025 ASSERT_EQ('n', getc(fp));
1026 ASSERT_EQ('u', getc(fp));
1027 ASSERT_EQ('x', getc(fp));
1028 fclose(fp);
1029}
1030
Christopher Ferris13f26a72016-01-13 13:47:58 -08001031TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001032 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001033 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001034 ASSERT_EQ(EOF, putc('x', fp));
1035 fclose(fp);
1036}
Elliott Hughes603332f2014-03-12 17:10:41 -07001037
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001038TEST(STDIO_TEST, sscanf_swscanf) {
1039 struct stuff {
1040 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001041 int i1, i2;
1042 char cs1[3];
1043 char s2[3];
1044 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001045 double d1;
1046 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001047 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001048
1049 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001050 EXPECT_STREQ("hello", s1);
1051 EXPECT_EQ(123, i1);
1052 EXPECT_EQ(456, i2);
1053 EXPECT_EQ('a', cs1[0]);
1054 EXPECT_EQ('b', cs1[1]);
1055 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
1056 EXPECT_STREQ("AB", s2); // Terminating NUL.
1057 EXPECT_EQ('!', c1);
1058 EXPECT_DOUBLE_EQ(1.23, d1);
1059 EXPECT_FLOAT_EQ(9.0f, f1);
1060 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001061 }
1062 } s;
1063
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001064 memset(&s, 'x', sizeof(s));
1065 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1066 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1067 s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3));
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001068 s.Check();
1069
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001070 memset(&s, 'x', sizeof(s));
1071 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1072 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1073 s.s1, &s.i1, &s.i2, s.cs1, s.s2, &s.c1, &s.d1, &s.f1, s.s3));
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001074 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001075}
Elliott Hughes53b24382014-05-02 18:29:25 -07001076
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001077template <typename T>
1078static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1079 const T* input, const T* fmt,
1080 int expected_count, const char* expected_string) {
1081 char buf[256] = {};
1082 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1083 ASSERT_STREQ(expected_string, buf) << fmt;
1084}
1085
1086TEST(STDIO_TEST, sscanf_ccl) {
1087 // `abc` is just those characters.
1088 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1089 // `a-c` is the range 'a' .. 'c'.
1090 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1091 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1092 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1093 // `a-c-e` is equivalent to `a-e`.
1094 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1095 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1096 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1097 // An initial '^' negates the set.
1098 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1099 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1100 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1101 // The first character may be ']' or '-' without being special.
1102 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1103 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1104 // The last character may be '-' without being special.
1105 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1106 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1107 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1108}
1109
1110TEST(STDIO_TEST, swscanf_ccl) {
1111 // `abc` is just those characters.
1112 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1113 // `a-c` is the range 'a' .. 'c'.
1114 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1115 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1116 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1117 // `a-c-e` is equivalent to `a-e`.
1118 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1119 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1120 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1121 // An initial '^' negates the set.
1122 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1123 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1124 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1125 // The first character may be ']' or '-' without being special.
1126 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1127 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1128 // The last character may be '-' without being special.
1129 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1130 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1131 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1132}
1133
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001134template <typename T1, typename T2>
1135static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1136 const T1* input, const T1* fmt,
1137 int expected_count, const T2* expected_string) {
1138 T2* result = nullptr;
1139 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1140 if (expected_string == nullptr) {
1141 ASSERT_EQ(nullptr, result);
1142 } else {
1143 ASSERT_STREQ(expected_string, result) << fmt;
1144 }
1145 free(result);
1146}
1147
1148TEST(STDIO_TEST, sscanf_mc) {
1149 char* p1 = nullptr;
1150 char* p2 = nullptr;
1151 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1152 ASSERT_EQ('h', *p1);
1153 ASSERT_EQ('e', *p2);
1154 free(p1);
1155 free(p2);
1156
1157 p1 = nullptr;
1158 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1159 ASSERT_EQ('h', p1[0]);
1160 ASSERT_EQ('e', p1[1]);
1161 ASSERT_EQ('l', p1[2]);
1162 ASSERT_EQ('l', p1[3]);
1163 free(p1);
1164
1165 p1 = nullptr;
1166 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1167 ASSERT_EQ('h', p1[0]);
1168 ASSERT_EQ('e', p1[1]);
1169 ASSERT_EQ('l', p1[2]);
1170 ASSERT_EQ('l', p1[3]);
1171 ASSERT_EQ('o', p1[4]);
1172 free(p1);
1173}
1174
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001175TEST(STDIO_TEST, sscanf_mlc) {
1176 // This is so useless that clang doesn't even believe it exists...
1177#pragma clang diagnostic push
1178#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1179#pragma clang diagnostic ignored "-Wformat-extra-args"
1180
1181 wchar_t* p1 = nullptr;
1182 wchar_t* p2 = nullptr;
1183 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1184 ASSERT_EQ(L'h', *p1);
1185 ASSERT_EQ(L'e', *p2);
1186 free(p1);
1187 free(p2);
1188
1189 p1 = nullptr;
1190 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1191 ASSERT_EQ(L'h', p1[0]);
1192 ASSERT_EQ(L'e', p1[1]);
1193 ASSERT_EQ(L'l', p1[2]);
1194 ASSERT_EQ(L'l', p1[3]);
1195 free(p1);
1196
1197 p1 = nullptr;
1198 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1199 ASSERT_EQ(L'h', p1[0]);
1200 ASSERT_EQ(L'e', p1[1]);
1201 ASSERT_EQ(L'l', p1[2]);
1202 ASSERT_EQ(L'l', p1[3]);
1203 ASSERT_EQ(L'o', p1[4]);
1204 free(p1);
1205#pragma clang diagnostic pop
1206}
1207
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001208TEST(STDIO_TEST, sscanf_ms) {
1209 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1210 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1211 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1212}
1213
1214TEST(STDIO_TEST, sscanf_mls) {
1215 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1216 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1217 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1218}
1219
1220TEST(STDIO_TEST, sscanf_m_ccl) {
1221 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1222 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1223 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1224}
1225
1226TEST(STDIO_TEST, sscanf_ml_ccl) {
1227 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1228 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1229 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1230}
1231
1232TEST(STDIO_TEST, sscanf_ls) {
1233 wchar_t w[32] = {};
1234 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1235 ASSERT_EQ(L"hello", std::wstring(w));
1236}
1237
1238TEST(STDIO_TEST, sscanf_ls_suppress) {
1239 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1240}
1241
1242TEST(STDIO_TEST, sscanf_ls_n) {
1243 setlocale(LC_ALL, "C.UTF-8");
1244 wchar_t w[32] = {};
1245 int pos = 0;
1246 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1247 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1248 ASSERT_EQ(2, pos);
1249}
1250
1251TEST(STDIO_TEST, sscanf_ls_realloc) {
1252 // This is so useless that clang doesn't even believe it exists...
1253#pragma clang diagnostic push
1254#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1255#pragma clang diagnostic ignored "-Wformat-extra-args"
1256 wchar_t* p1 = nullptr;
1257 wchar_t* p2 = nullptr;
1258 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1259 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1260 ASSERT_EQ(L"world", std::wstring(p2));
1261#pragma clang diagnostic pop
1262}
1263
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001264// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1265TEST(STDIO_TEST, scanf_wscanf_EOF) {
1266 EXPECT_EQ(0, sscanf("b", "ab"));
1267 EXPECT_EQ(EOF, sscanf("", "a"));
1268 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1269 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1270}
1271
1272TEST(STDIO_TEST, scanf_invalid_UTF8) {
1273#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1274 char buf[BUFSIZ];
1275 wchar_t wbuf[BUFSIZ];
1276
1277 memset(buf, 0, sizeof(buf));
1278 memset(wbuf, 0, sizeof(wbuf));
1279 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1280#endif
1281}
1282
1283TEST(STDIO_TEST, scanf_no_match_no_termination) {
1284 char buf[4] = "x";
1285 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1286 EXPECT_EQ('x', buf[0]);
1287 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1288 EXPECT_EQ('x', buf[0]);
1289
1290 wchar_t wbuf[4] = L"x";
1291 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1292 EXPECT_EQ(L'x', wbuf[0]);
1293
1294 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1295 EXPECT_EQ('x', buf[0]);
1296
1297 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1298 EXPECT_EQ(L'x', wbuf[0]);
1299}
1300
1301TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1302#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1303 wchar_t buf[BUFSIZ];
1304
1305 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1306 memset(buf, 0, sizeof(buf));
1307 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1308 EXPECT_EQ(L"x"s, std::wstring(buf));
1309 memset(buf, 0, sizeof(buf));
1310 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1311 EXPECT_EQ(L"x"s, std::wstring(buf));
1312
1313 // Even if scanf has wide characters in a class, they won't match...
1314 // TODO: is that a bug?
1315 memset(buf, 0, sizeof(buf));
1316 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1317 EXPECT_EQ(L"x"s, std::wstring(buf));
1318 // ...unless you use wscanf.
1319 memset(buf, 0, sizeof(buf));
1320 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1321 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1322
1323 // Negation only covers ASCII for scanf...
1324 memset(buf, 0, sizeof(buf));
1325 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1326 EXPECT_EQ(L"x"s, std::wstring(buf));
1327 // ...but covers wide characters for wscanf.
1328 memset(buf, 0, sizeof(buf));
1329 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1330 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1331
1332 // We already determined that non-ASCII characters are ignored in scanf classes.
1333 memset(buf, 0, sizeof(buf));
1334 EXPECT_EQ(1, sscanf("x"
1335 "\xc4\x80" // Matches a byte from each wide char in the class.
1336 "\xc6\x82" // Neither byte is in the class.
1337 "yz",
1338 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1339 EXPECT_EQ(L"x", std::wstring(buf));
1340 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1341 memset(buf, 0, sizeof(buf));
1342 EXPECT_EQ(1, swscanf(L"x"
1343 L"\xc4\x80"
1344 L"\xc6\x82"
1345 L"yz",
1346 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1347 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1348 // not put back together as a wide character.
1349 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1350#endif
1351}
1352
Christopher Ferris13f26a72016-01-13 13:47:58 -08001353TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001354 // If we open a file read-only...
1355 FILE* fp = fopen("/proc/version", "r");
1356
1357 // ...all attempts to write to that file should return failure.
1358
1359 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1360 // glibc gets the wide-character functions wrong.
1361
1362 errno = 0;
1363 EXPECT_EQ(EOF, putc('x', fp));
1364 EXPECT_EQ(EBADF, errno);
1365
1366 errno = 0;
1367 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1368 EXPECT_EQ(EBADF, errno);
1369
1370 errno = 0;
1371 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001372#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001373 EXPECT_EQ(EBADF, errno);
1374#endif
1375
1376 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001377 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1378 EXPECT_EQ(EBADF, errno);
1379
1380 errno = 0;
1381 EXPECT_EQ(EOF, fputs("hello", fp));
1382 EXPECT_EQ(EBADF, errno);
1383
1384 errno = 0;
1385 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001386#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001387 EXPECT_EQ(EBADF, errno);
1388#endif
1389}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001390
1391// Tests that we can only have a consistent and correct fpos_t when using
1392// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001393TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001394 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1395 uselocale(LC_GLOBAL_LOCALE);
1396
1397 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001398 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001399
1400 wchar_t mb_one_bytes = L'h';
1401 wchar_t mb_two_bytes = 0x00a2;
1402 wchar_t mb_three_bytes = 0x20ac;
1403 wchar_t mb_four_bytes = 0x24b62;
1404
1405 // Write to file.
1406 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1407 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1408 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1409 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1410
1411 rewind(fp);
1412
1413 // Record each character position.
1414 fpos_t pos1;
1415 fpos_t pos2;
1416 fpos_t pos3;
1417 fpos_t pos4;
1418 fpos_t pos5;
1419 EXPECT_EQ(0, fgetpos(fp, &pos1));
1420 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1421 EXPECT_EQ(0, fgetpos(fp, &pos2));
1422 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1423 EXPECT_EQ(0, fgetpos(fp, &pos3));
1424 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1425 EXPECT_EQ(0, fgetpos(fp, &pos4));
1426 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1427 EXPECT_EQ(0, fgetpos(fp, &pos5));
1428
Elliott Hughes063525c2014-05-13 11:19:57 -07001429#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001430 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1431 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1432 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1433 // structure.
1434 ASSERT_EQ(0, static_cast<off_t>(pos1));
1435 ASSERT_EQ(1, static_cast<off_t>(pos2));
1436 ASSERT_EQ(3, static_cast<off_t>(pos3));
1437 ASSERT_EQ(6, static_cast<off_t>(pos4));
1438 ASSERT_EQ(10, static_cast<off_t>(pos5));
1439#endif
1440
1441 // Exercise back and forth movements of the position.
1442 ASSERT_EQ(0, fsetpos(fp, &pos2));
1443 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1444 ASSERT_EQ(0, fsetpos(fp, &pos1));
1445 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1446 ASSERT_EQ(0, fsetpos(fp, &pos4));
1447 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1448 ASSERT_EQ(0, fsetpos(fp, &pos3));
1449 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1450 ASSERT_EQ(0, fsetpos(fp, &pos5));
1451 ASSERT_EQ(WEOF, fgetwc(fp));
1452
1453 fclose(fp);
1454}
1455
1456// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001457TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001458 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1459 uselocale(LC_GLOBAL_LOCALE);
1460
Calin Juravle9b95ea92014-05-14 17:07:10 +01001461 // In glibc-2.16 fseek doesn't work properly in wide mode
1462 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1463 // to close and re-open the file. We do it in order to make the test pass
1464 // with all glibcs.
1465
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001466 TemporaryFile tf;
1467 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001468 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001469
1470 wchar_t mb_two_bytes = 0x00a2;
1471 wchar_t mb_three_bytes = 0x20ac;
1472 wchar_t mb_four_bytes = 0x24b62;
1473
1474 // Write to file.
1475 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1476 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1477 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1478
1479 fflush(fp);
1480 fclose(fp);
1481
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001482 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001483 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001484
1485 // Store a valid position.
1486 fpos_t mb_two_bytes_pos;
1487 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1488
1489 // Move inside mb_four_bytes with fseek.
1490 long offset_inside_mb = 6;
1491 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1492
1493 // Store the "inside multi byte" position.
1494 fpos_t pos_inside_mb;
1495 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001496#if defined(__BIONIC__)
1497 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1498#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001499
1500 // Reading from within a byte should produce an error.
1501 ASSERT_EQ(WEOF, fgetwc(fp));
1502 ASSERT_EQ(EILSEQ, errno);
1503
1504 // Reverting to a valid position should work.
1505 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1506 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1507
1508 // Moving withing a multi byte with fsetpos should work but reading should
1509 // produce an error.
1510 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1511 ASSERT_EQ(WEOF, fgetwc(fp));
1512 ASSERT_EQ(EILSEQ, errno);
1513
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001514 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001515}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001516
Christopher Ferris13f26a72016-01-13 13:47:58 -08001517TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001518 char buf[16];
1519 memset(buf, 0, sizeof(buf));
1520 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1521 ASSERT_EQ('<', fputc('<', fp));
1522 ASSERT_NE(EOF, fputs("abc>\n", fp));
1523 fflush(fp);
1524
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001525 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001526 ASSERT_STREQ("<abc>\n", buf);
1527
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001528 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001529 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001530 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001531}
1532
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001533TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001534 FILE* fp = fmemopen(nullptr, 128, "r+");
1535 ASSERT_NE(EOF, fputs("xyz\n", fp));
1536
Elliott Hughes70715da2016-08-01 16:35:17 -07001537 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001538 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001539}
1540
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001541TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1542 FILE* fp;
1543 char buf[8];
1544
1545 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1546 // shall be written at the current position or at the end of the buffer,
1547 // depending on the size of the contents."
1548 memset(buf, 'x', sizeof(buf));
1549 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1550 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1551 ASSERT_EQ(0, fflush(fp));
1552 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1553 // Now write and check that the NUL moves along with our writes...
1554 ASSERT_NE(EOF, fputs("hello", fp));
1555 ASSERT_EQ(0, fflush(fp));
1556 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1557 ASSERT_NE(EOF, fputs("wo", fp));
1558 ASSERT_EQ(0, fflush(fp));
1559 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1560 ASSERT_EQ(0, fclose(fp));
1561
1562 // "If a stream open for update is flushed or closed and the last write has
1563 // advanced the current buffer size, a null byte shall be written at the end
1564 // of the buffer if it fits."
1565 memset(buf, 'x', sizeof(buf));
1566 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1567 // Nothing written yet, so no advance...
1568 ASSERT_EQ(0, fflush(fp));
1569 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1570 ASSERT_NE(EOF, fputs("hello", fp));
1571 ASSERT_EQ(0, fclose(fp));
1572}
1573
1574TEST(STDIO_TEST, fmemopen_size) {
1575 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001576 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001577 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001578
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001579 // POSIX: "The stream shall also maintain the size of the current buffer
1580 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1581 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001582
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001583 // "For modes r and r+ the size shall be set to the value given by the size
1584 // argument."
1585 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1586 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1587 EXPECT_EQ(16, ftell(fp));
1588 EXPECT_EQ(16, ftello(fp));
1589 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1590 EXPECT_EQ(16, ftell(fp));
1591 EXPECT_EQ(16, ftello(fp));
1592 ASSERT_EQ(0, fclose(fp));
1593 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1594 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1595 EXPECT_EQ(16, ftell(fp));
1596 EXPECT_EQ(16, ftello(fp));
1597 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1598 EXPECT_EQ(16, ftell(fp));
1599 EXPECT_EQ(16, ftello(fp));
1600 ASSERT_EQ(0, fclose(fp));
1601
1602 // "For modes w and w+ the initial size shall be zero..."
1603 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1604 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1605 EXPECT_EQ(0, ftell(fp));
1606 EXPECT_EQ(0, ftello(fp));
1607 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1608 EXPECT_EQ(0, ftell(fp));
1609 EXPECT_EQ(0, ftello(fp));
1610 ASSERT_EQ(0, fclose(fp));
1611 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1612 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1613 EXPECT_EQ(0, ftell(fp));
1614 EXPECT_EQ(0, ftello(fp));
1615 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1616 EXPECT_EQ(0, ftell(fp));
1617 EXPECT_EQ(0, ftello(fp));
1618 ASSERT_EQ(0, fclose(fp));
1619
1620 // "...and for modes a and a+ the initial size shall be:
1621 // 1. Zero, if buf is a null pointer
1622 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1623 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1624 EXPECT_EQ(0, ftell(fp));
1625 EXPECT_EQ(0, ftello(fp));
1626 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1627 EXPECT_EQ(0, ftell(fp));
1628 EXPECT_EQ(0, ftello(fp));
1629 ASSERT_EQ(0, fclose(fp));
1630 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1631 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1632 EXPECT_EQ(0, ftell(fp));
1633 EXPECT_EQ(0, ftello(fp));
1634 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1635 EXPECT_EQ(0, ftell(fp));
1636 EXPECT_EQ(0, ftello(fp));
1637 ASSERT_EQ(0, fclose(fp));
1638
1639 // 2. The position of the first null byte in the buffer, if one is found
1640 memset(buf, 'x', sizeof(buf));
1641 buf[3] = '\0';
1642 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1643 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1644 EXPECT_EQ(3, ftell(fp));
1645 EXPECT_EQ(3, ftello(fp));
1646 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1647 EXPECT_EQ(3, ftell(fp));
1648 EXPECT_EQ(3, ftello(fp));
1649 ASSERT_EQ(0, fclose(fp));
1650 memset(buf, 'x', sizeof(buf));
1651 buf[3] = '\0';
1652 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1653 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1654 EXPECT_EQ(3, ftell(fp));
1655 EXPECT_EQ(3, ftello(fp));
1656 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1657 EXPECT_EQ(3, ftell(fp));
1658 EXPECT_EQ(3, ftello(fp));
1659 ASSERT_EQ(0, fclose(fp));
1660
1661 // 3. The value of the size argument, if buf is not a null pointer and no
1662 // null byte is found.
1663 memset(buf, 'x', sizeof(buf));
1664 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1665 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1666 EXPECT_EQ(16, ftell(fp));
1667 EXPECT_EQ(16, ftello(fp));
1668 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1669 EXPECT_EQ(16, ftell(fp));
1670 EXPECT_EQ(16, ftello(fp));
1671 ASSERT_EQ(0, fclose(fp));
1672 memset(buf, 'x', sizeof(buf));
1673 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1674 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1675 EXPECT_EQ(16, ftell(fp));
1676 EXPECT_EQ(16, ftello(fp));
1677 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1678 EXPECT_EQ(16, ftell(fp));
1679 EXPECT_EQ(16, ftello(fp));
1680 ASSERT_EQ(0, fclose(fp));
1681}
1682
1683TEST(STDIO_TEST, fmemopen_SEEK_END) {
1684 // fseek SEEK_END is relative to the current string length, not the buffer size.
1685 FILE* fp;
1686 char buf[8];
1687 memset(buf, 'x', sizeof(buf));
1688 strcpy(buf, "str");
1689 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1690 ASSERT_NE(EOF, fputs("string", fp));
1691 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1692 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1693 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1694 EXPECT_EQ(0, fclose(fp));
1695
1696 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1697 // than adding).
1698 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1699 ASSERT_NE(EOF, fputs("54321", fp));
1700 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1701 EXPECT_EQ('2', fgetc(fp));
1702 EXPECT_EQ(0, fclose(fp));
1703}
1704
1705TEST(STDIO_TEST, fmemopen_seek_invalid) {
1706 char buf[8];
1707 memset(buf, 'x', sizeof(buf));
1708 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1709 ASSERT_TRUE(fp != nullptr);
1710
1711 // POSIX: "An attempt to seek ... to a negative position or to a position
1712 // larger than the buffer size given in the size argument shall fail."
1713 // (There's no mention of what errno should be set to, and glibc doesn't
1714 // set errno in any of these cases.)
1715 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1716 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1717 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1718 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1719}
1720
1721TEST(STDIO_TEST, fmemopen_read_EOF) {
1722 // POSIX: "A read operation on the stream shall not advance the current
1723 // buffer position beyond the current buffer size."
1724 char buf[8];
1725 memset(buf, 'x', sizeof(buf));
1726 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1727 ASSERT_TRUE(fp != nullptr);
1728 char buf2[BUFSIZ];
1729 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1730 // POSIX: "Reaching the buffer size in a read operation shall count as
1731 // end-of-file.
1732 ASSERT_TRUE(feof(fp));
1733 ASSERT_EQ(EOF, fgetc(fp));
1734 ASSERT_EQ(0, fclose(fp));
1735}
1736
1737TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1738 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1739 char buf[] = "h\0e\0l\0l\0o";
1740 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1741 ASSERT_TRUE(fp != nullptr);
1742 ASSERT_EQ('h', fgetc(fp));
1743 ASSERT_EQ(0, fgetc(fp));
1744 ASSERT_EQ('e', fgetc(fp));
1745 ASSERT_EQ(0, fgetc(fp));
1746 ASSERT_EQ('l', fgetc(fp));
1747 ASSERT_EQ(0, fgetc(fp));
1748 // POSIX: "The read operation shall start at the current buffer position of
1749 // the stream."
1750 char buf2[8];
1751 memset(buf2, 'x', sizeof(buf2));
1752 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1753 ASSERT_EQ('l', buf2[0]);
1754 ASSERT_EQ(0, buf2[1]);
1755 ASSERT_EQ('o', buf2[2]);
1756 ASSERT_EQ(0, buf2[3]);
1757 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1758 ASSERT_TRUE(feof(fp));
1759 ASSERT_EQ(0, fclose(fp));
1760}
1761
1762TEST(STDIO_TEST, fmemopen_write) {
1763 FILE* fp;
1764 char buf[8];
1765
1766 // POSIX: "A write operation shall start either at the current position of
1767 // the stream (if mode has not specified 'a' as the first character)..."
1768 memset(buf, 'x', sizeof(buf));
1769 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1770 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1771 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1772 ASSERT_EQ(' ', fputc(' ', fp));
1773 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1774 ASSERT_EQ(0, fclose(fp));
1775
1776 // "...or at the current size of the stream (if mode had 'a' as the first
1777 // character)." (See the fmemopen_size test for what "size" means, but for
1778 // mode "a", it's the first NUL byte.)
1779 memset(buf, 'x', sizeof(buf));
1780 buf[3] = '\0';
1781 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1782 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1783 ASSERT_EQ(' ', fputc(' ', fp));
1784 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1785 ASSERT_EQ(0, fclose(fp));
1786
1787 // "If the current position at the end of the write is larger than the
1788 // current buffer size, the current buffer size shall be set to the current
1789 // position." (See the fmemopen_size test for what "size" means, but to
1790 // query it we SEEK_END with offset 0, and then ftell.)
1791 memset(buf, 'x', sizeof(buf));
1792 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1793 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1794 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1795 EXPECT_EQ(0, ftell(fp));
1796 ASSERT_EQ(' ', fputc(' ', fp));
1797 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1798 EXPECT_EQ(1, ftell(fp));
1799 ASSERT_NE(EOF, fputs("123", fp));
1800 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1801 EXPECT_EQ(4, ftell(fp));
1802 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1803 ASSERT_EQ(0, fclose(fp));
1804}
1805
1806TEST(STDIO_TEST, fmemopen_write_EOF) {
1807 // POSIX: "A write operation on the stream shall not advance the current
1808 // buffer size beyond the size given in the size argument."
1809 FILE* fp;
1810
1811 // Scalar writes...
1812 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1813 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1814 ASSERT_EQ('x', fputc('x', fp));
1815 ASSERT_EQ('x', fputc('x', fp));
1816 ASSERT_EQ('x', fputc('x', fp));
1817 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1818 ASSERT_EQ(0, fclose(fp));
1819
1820 // Vector writes...
1821 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1822 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1823 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1824 ASSERT_EQ(0, fclose(fp));
1825}
1826
1827TEST(STDIO_TEST, fmemopen_initial_position) {
1828 // POSIX: "The ... current position in the buffer ... shall be initially
1829 // set to either the beginning of the buffer (for r and w modes) ..."
1830 char buf[] = "hello\0world";
1831 FILE* fp;
1832 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1833 EXPECT_EQ(0L, ftell(fp));
1834 EXPECT_EQ(0, fclose(fp));
1835 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1836 EXPECT_EQ(0L, ftell(fp));
1837 EXPECT_EQ(0, fclose(fp));
1838 buf[0] = 'h'; // (Undo the effects of the above.)
1839
1840 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1841 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1842 EXPECT_EQ(5L, ftell(fp));
1843 EXPECT_EQ(0, fclose(fp));
1844
1845 // POSIX: "If no null byte is found in append mode, the initial position
1846 // shall be set to one byte after the end of the buffer."
1847 memset(buf, 'x', sizeof(buf));
1848 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1849 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1850 EXPECT_EQ(0, fclose(fp));
1851}
1852
1853TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1854 // POSIX: "If buf is a null pointer, the initial position shall always be
1855 // set to the beginning of the buffer."
1856 FILE* fp = fmemopen(nullptr, 128, "a+");
1857 ASSERT_TRUE(fp != nullptr);
1858 EXPECT_EQ(0L, ftell(fp));
1859 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1860 EXPECT_EQ(0, fclose(fp));
1861}
1862
1863TEST(STDIO_TEST, fmemopen_zero_length) {
1864 // POSIX says it's up to the implementation whether or not you can have a
1865 // zero-length buffer (but "A future version of this standard may require
1866 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1867 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1868 FILE* fp;
1869 char buf[16];
1870 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1871 ASSERT_EQ(EOF, fgetc(fp));
1872 ASSERT_TRUE(feof(fp));
1873 ASSERT_EQ(0, fclose(fp));
1874 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1875 ASSERT_EQ(EOF, fgetc(fp));
1876 ASSERT_TRUE(feof(fp));
1877 ASSERT_EQ(0, fclose(fp));
1878
1879 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1880 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1881 ASSERT_EQ(EOF, fputc('x', fp));
1882 ASSERT_EQ(0, fclose(fp));
1883 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1884 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1885 ASSERT_EQ(EOF, fputc('x', fp));
1886 ASSERT_EQ(0, fclose(fp));
1887}
1888
Elliott Hughes288465d2019-02-05 15:00:13 -08001889TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1890 char buf[2] = "x";
1891 ASSERT_EQ('x', buf[0]);
1892 FILE* fp = fmemopen(buf, 0, "w");
1893 ASSERT_EQ('x', buf[0]);
1894 ASSERT_EQ(0, fclose(fp));
1895}
1896
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001897TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1898 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1899 // BSD fails, glibc doesn't. We side with the more lenient.
1900 FILE* fp;
1901 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1902 ASSERT_EQ(0, fclose(fp));
1903 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1904 ASSERT_EQ(0, fclose(fp));
1905}
1906
1907TEST(STDIO_TEST, fmemopen_fileno) {
1908 // There's no fd backing an fmemopen FILE*.
1909 FILE* fp = fmemopen(nullptr, 16, "r");
1910 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001911 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001912 ASSERT_EQ(-1, fileno(fp));
1913 ASSERT_EQ(EBADF, errno);
1914 ASSERT_EQ(0, fclose(fp));
1915}
1916
1917TEST(STDIO_TEST, fmemopen_append_after_seek) {
1918 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1919 // there had been an intervening seek.
1920
1921 FILE* fp;
1922 char buf[] = "hello\0world";
1923 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1924 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1925 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1926 ASSERT_NE(EOF, fputc('!', fp));
1927 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1928 ASSERT_EQ(0, fclose(fp));
1929
1930 memcpy(buf, "hello\0world", sizeof(buf));
1931 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1932 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1933 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1934 ASSERT_NE(EOF, fputc('!', fp));
1935 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1936 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001937}
1938
Christopher Ferris13f26a72016-01-13 13:47:58 -08001939TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001940 char* p = nullptr;
1941 size_t size = 0;
1942 FILE* fp = open_memstream(&p, &size);
1943 ASSERT_NE(EOF, fputs("hello, world!", fp));
1944 fclose(fp);
1945
1946 ASSERT_STREQ("hello, world!", p);
1947 ASSERT_EQ(strlen("hello, world!"), size);
1948 free(p);
1949}
1950
Christopher Ferris13f26a72016-01-13 13:47:58 -08001951TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001952#if defined(__BIONIC__)
zijunzhao00a3dba2023-03-01 00:07:40 +00001953#pragma clang diagnostic push
1954#pragma clang diagnostic ignored "-Wnonnull"
Elliott Hughes6b841db2014-08-20 16:10:49 -07001955 char* p;
1956 size_t size;
1957
1958 // Invalid buffer.
1959 errno = 0;
1960 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1961 ASSERT_EQ(EINVAL, errno);
1962
1963 // Invalid size.
1964 errno = 0;
1965 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1966 ASSERT_EQ(EINVAL, errno);
zijunzhao00a3dba2023-03-01 00:07:40 +00001967#pragma clang diagnostic pop
Elliott Hughes6b841db2014-08-20 16:10:49 -07001968#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001969 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001970#endif
1971}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001972
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001973TEST(STDIO_TEST, fdopen_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001974 // This fd doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001975 int fd = open("/proc/version", O_RDONLY);
1976 ASSERT_FALSE(CloseOnExec(fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001977 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001978 FILE* fp = fdopen(fd, "re");
1979 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1980 fclose(fp);
1981}
1982
1983TEST(STDIO_TEST, fdopen_remove_CLOEXEC) {
1984 // This fd has O_CLOEXEC...
1985 int fd = open("/proc/version", O_RDONLY | O_CLOEXEC);
1986 ASSERT_TRUE(CloseOnExec(fd));
1987 // ...but the new one doesn't.
1988 FILE* fp = fdopen(fd, "r");
1989 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1990 fclose(fp);
1991}
1992
1993TEST(STDIO_TEST, freopen_add_CLOEXEC) {
1994 // This FILE* doesn't have O_CLOEXEC...
1995 FILE* fp = fopen("/proc/version", "r");
1996 ASSERT_FALSE(CloseOnExec(fileno(fp)));
1997 // ...but the new one does.
1998 fp = freopen("/proc/version", "re", fp);
1999 ASSERT_TRUE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002000
2001 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07002002}
2003
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002004TEST(STDIO_TEST, freopen_remove_CLOEXEC) {
2005 // This FILE* has O_CLOEXEC...
2006 FILE* fp = fopen("/proc/version", "re");
2007 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2008 // ...but the new one doesn't.
2009 fp = freopen("/proc/version", "r", fp);
2010 ASSERT_FALSE(CloseOnExec(fileno(fp)));
2011 fclose(fp);
2012}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002013
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002014TEST(STDIO_TEST, freopen_null_filename_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07002015 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002016 FILE* fp = fopen("/proc/version", "r");
2017 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002018 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002019 fp = freopen(nullptr, "re", fp);
2020 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2021 fclose(fp);
2022}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002023
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002024TEST(STDIO_TEST, freopen_null_filename_remove_CLOEXEC) {
2025 // This FILE* has O_CLOEXEC...
2026 FILE* fp = fopen("/proc/version", "re");
2027 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2028 // ...but the new one doesn't.
2029 fp = freopen(nullptr, "r", fp);
2030 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002031 fclose(fp);
2032}
Elliott Hughes20841a12014-12-01 16:13:30 -08002033
Elliott Hughesf226ee52016-02-03 11:24:28 -08002034TEST(STDIO_TEST, fopen64_freopen64) {
2035 FILE* fp = fopen64("/proc/version", "r");
2036 ASSERT_TRUE(fp != nullptr);
2037 fp = freopen64("/proc/version", "re", fp);
2038 ASSERT_TRUE(fp != nullptr);
2039 fclose(fp);
2040}
2041
Elliott Hughes20841a12014-12-01 16:13:30 -08002042// https://code.google.com/p/android/issues/detail?id=81155
2043// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08002044TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08002045 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07002046 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002047
2048 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07002049 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08002050
2051 char buf[65*1024];
2052 memset(buf, 0xff, sizeof(buf));
2053
Yi Kong32bc0fc2018-08-02 17:31:13 -07002054 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002055 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002056 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08002057 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07002058 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002059
2060 fclose(fp);
2061
2062 // 1024 64KiB reads should have been very quick.
2063 ASSERT_LE(t1 - t0, 1);
2064
2065 for (size_t i = 0; i < 64*1024; ++i) {
2066 ASSERT_EQ('\0', buf[i]);
2067 }
2068 for (size_t i = 64*1024; i < 65*1024; ++i) {
2069 ASSERT_EQ('\xff', buf[i]);
2070 }
2071}
Elliott Hughes75b99382015-01-20 11:23:50 -08002072
Christopher Ferris13f26a72016-01-13 13:47:58 -08002073TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002074 std::string digits("0123456789");
2075 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08002076
2077 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
2078 char buf1[4 * 4];
2079 memset(buf1, 0, sizeof(buf1));
2080 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002081 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08002082 ASSERT_TRUE(feof(fp));
2083
2084 rewind(fp);
2085
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002086 // Try to read way too much so stdio tries to read more direct from the stream.
2087 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08002088 memset(buf2, 0, sizeof(buf2));
2089 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002090 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002091 ASSERT_TRUE(feof(fp));
2092
2093 fclose(fp);
2094}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002095
2096static void test_fread_from_write_only_stream(size_t n) {
2097 FILE* fp = fopen("/dev/null", "w");
2098 std::vector<char> buf(n, 0);
2099 errno = 0;
2100 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2101 ASSERT_EQ(EBADF, errno);
2102 ASSERT_TRUE(ferror(fp));
2103 ASSERT_FALSE(feof(fp));
2104 fclose(fp);
2105}
2106
Christopher Ferris13f26a72016-01-13 13:47:58 -08002107TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002108 test_fread_from_write_only_stream(1);
2109}
2110
Christopher Ferris13f26a72016-01-13 13:47:58 -08002111TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002112 test_fread_from_write_only_stream(64*1024);
2113}
2114
2115static void test_fwrite_after_fread(size_t n) {
2116 TemporaryFile tf;
2117
2118 FILE* fp = fdopen(tf.fd, "w+");
2119 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2120 fflush(fp);
2121
2122 // We've flushed but not rewound, so there's nothing to read.
2123 std::vector<char> buf(n, 0);
2124 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2125 ASSERT_TRUE(feof(fp));
2126
2127 // But hitting EOF doesn't prevent us from writing...
2128 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002129 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002130
2131 // And if we rewind, everything's there.
2132 rewind(fp);
2133 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2134 ASSERT_EQ('1', buf[0]);
2135 ASSERT_EQ('2', buf[1]);
2136
2137 fclose(fp);
2138}
2139
Christopher Ferris13f26a72016-01-13 13:47:58 -08002140TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002141 test_fwrite_after_fread(16);
2142}
2143
Christopher Ferris13f26a72016-01-13 13:47:58 -08002144TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002145 test_fwrite_after_fread(64*1024);
2146}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002147
2148// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002149TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002150 TemporaryFile tf;
2151
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002152 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002153 ASSERT_TRUE(fp != nullptr);
2154
2155 char file_data[12288];
2156 for (size_t i = 0; i < 12288; i++) {
2157 file_data[i] = i;
2158 }
2159 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2160 fclose(fp);
2161
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002162 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002163 ASSERT_TRUE(fp != nullptr);
2164
2165 char buffer[8192];
2166 size_t cur_location = 0;
2167 // Small read to populate internal buffer.
2168 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2169 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2170
2171 cur_location = static_cast<size_t>(ftell(fp));
2172 // Large read to force reading into the user supplied buffer and bypassing
2173 // the internal buffer.
2174 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2175 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2176
2177 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002178 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002179 cur_location = static_cast<size_t>(ftell(fp));
2180 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2181 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2182
2183 fclose(fp);
2184}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002185
2186// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002187TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002188 TemporaryFile tf;
2189 char buf[6] = {0};
2190
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002191 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002192 ASSERT_TRUE(fw != nullptr);
2193
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002194 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002195 ASSERT_TRUE(fr != nullptr);
2196
2197 fwrite("a", 1, 1, fw);
2198 fflush(fw);
2199 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2200 ASSERT_STREQ("a", buf);
2201
2202 // 'fr' is now at EOF.
2203 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2204 ASSERT_TRUE(feof(fr));
2205
2206 // Write some more...
2207 fwrite("z", 1, 1, fw);
2208 fflush(fw);
2209
2210 // ...and check that we can read it back.
2211 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2212 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2213 ASSERT_STREQ("z", buf);
2214
2215 // But now we're done.
2216 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2217
2218 fclose(fr);
2219 fclose(fw);
2220}
Elliott Hughes923f1652016-01-19 15:46:05 -08002221
2222TEST(STDIO_TEST, fclose_invalidates_fd) {
2223 // The typical error we're trying to help people catch involves accessing
2224 // memory after it's been freed. But we know that stdin/stdout/stderr are
2225 // special and don't get deallocated, so this test uses stdin.
2226 ASSERT_EQ(0, fclose(stdin));
2227
2228 // Even though using a FILE* after close is undefined behavior, I've closed
2229 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2230 // especially because they might actually correspond to a real stream.
2231 errno = 0;
2232 ASSERT_EQ(-1, fileno(stdin));
2233 ASSERT_EQ(EBADF, errno);
2234}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002235
2236TEST(STDIO_TEST, fseek_ftell_unseekable) {
2237#if defined(__BIONIC__) // glibc has fopencookie instead.
2238 auto read_fn = [](void*, char*, int) { return -1; };
2239 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2240 ASSERT_TRUE(fp != nullptr);
2241
2242 // Check that ftell balks on an unseekable FILE*.
2243 errno = 0;
2244 ASSERT_EQ(-1, ftell(fp));
2245 ASSERT_EQ(ESPIPE, errno);
2246
2247 // SEEK_CUR is rewritten as SEEK_SET internally...
2248 errno = 0;
2249 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2250 ASSERT_EQ(ESPIPE, errno);
2251
2252 // ...so it's worth testing the direct seek path too.
2253 errno = 0;
2254 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2255 ASSERT_EQ(ESPIPE, errno);
2256
2257 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002258#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002259 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002260#endif
2261}
2262
2263TEST(STDIO_TEST, funopen_EINVAL) {
2264#if defined(__BIONIC__)
2265 errno = 0;
2266 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2267 ASSERT_EQ(EINVAL, errno);
2268#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002269 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002270#endif
2271}
2272
2273TEST(STDIO_TEST, funopen_seek) {
2274#if defined(__BIONIC__)
2275 auto read_fn = [](void*, char*, int) { return -1; };
2276
2277 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2278 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2279
2280 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2281 ASSERT_TRUE(fp != nullptr);
2282 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002283#if defined(__LP64__)
2284 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2285 EXPECT_EQ(0xfedcba12LL, pos);
2286#else
2287 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2288 EXPECT_EQ(EOVERFLOW, errno);
2289#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002290
2291 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2292 ASSERT_TRUE(fp64 != nullptr);
2293 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002294 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2295 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002296#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002297 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002298#endif
2299}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002300
2301TEST(STDIO_TEST, lots_of_concurrent_files) {
2302 std::vector<TemporaryFile*> tfs;
2303 std::vector<FILE*> fps;
2304
2305 for (size_t i = 0; i < 256; ++i) {
2306 TemporaryFile* tf = new TemporaryFile;
2307 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002308 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002309 fps.push_back(fp);
2310 fprintf(fp, "hello %zu!\n", i);
2311 fflush(fp);
2312 }
2313
2314 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002315 char expected[BUFSIZ];
2316 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002317
Elliott Hughes70715da2016-08-01 16:35:17 -07002318 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002319 fclose(fps[i]);
2320 delete tfs[i];
2321 }
2322}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002323
2324static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2325 EXPECT_EQ(offset, ftell(fp));
2326 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002327 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002328 fpos_t pos;
2329 fpos64_t pos64;
2330 EXPECT_EQ(0, fgetpos(fp, &pos));
2331 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2332#if defined(__BIONIC__)
2333 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2334 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2335#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002336 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002337#endif
2338}
2339
2340TEST(STDIO_TEST, seek_tell_family_smoke) {
2341 TemporaryFile tf;
2342 FILE* fp = fdopen(tf.fd, "w+");
2343
2344 // Initially we should be at 0.
2345 AssertFileOffsetAt(fp, 0);
2346
2347 // Seek to offset 8192.
2348 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2349 AssertFileOffsetAt(fp, 8192);
2350 fpos_t eight_k_pos;
2351 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2352
2353 // Seek forward another 8192...
2354 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2355 AssertFileOffsetAt(fp, 8192 + 8192);
2356 fpos64_t sixteen_k_pos64;
2357 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2358
2359 // Seek back 8192...
2360 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2361 AssertFileOffsetAt(fp, 8192);
2362
2363 // Since we haven't written anything, the end is also at 0.
2364 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2365 AssertFileOffsetAt(fp, 0);
2366
2367 // Check that our fpos64_t from 16KiB works...
2368 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2369 AssertFileOffsetAt(fp, 8192 + 8192);
2370 // ...as does our fpos_t from 8192.
2371 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2372 AssertFileOffsetAt(fp, 8192);
2373
2374 // Do fseeko and fseeko64 work too?
2375 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2376 AssertFileOffsetAt(fp, 1234);
2377 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2378 AssertFileOffsetAt(fp, 5678);
2379
2380 fclose(fp);
2381}
2382
2383TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2384 TemporaryFile tf;
2385 FILE* fp = fdopen(tf.fd, "w+");
2386
2387 // Bad whence.
2388 errno = 0;
2389 ASSERT_EQ(-1, fseek(fp, 0, 123));
2390 ASSERT_EQ(EINVAL, errno);
2391 errno = 0;
2392 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2393 ASSERT_EQ(EINVAL, errno);
2394 errno = 0;
2395 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2396 ASSERT_EQ(EINVAL, errno);
2397
2398 // Bad offset.
2399 errno = 0;
2400 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2401 ASSERT_EQ(EINVAL, errno);
2402 errno = 0;
2403 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2404 ASSERT_EQ(EINVAL, errno);
2405 errno = 0;
2406 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2407 ASSERT_EQ(EINVAL, errno);
2408
2409 fclose(fp);
2410}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002411
2412TEST(STDIO_TEST, ctermid) {
2413 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2414
2415 char buf[L_ctermid] = {};
2416 ASSERT_EQ(buf, ctermid(buf));
2417 ASSERT_STREQ("/dev/tty", buf);
2418}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002419
2420TEST(STDIO_TEST, remove) {
2421 struct stat sb;
2422
2423 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002424 ASSERT_EQ(0, remove(tf.path));
2425 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002426 ASSERT_EQ(ENOENT, errno);
2427
2428 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002429 ASSERT_EQ(0, remove(td.path));
2430 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002431 ASSERT_EQ(ENOENT, errno);
2432
2433 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002434 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002435 ASSERT_EQ(ENOENT, errno);
2436
2437 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002438 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002439 ASSERT_EQ(ENOENT, errno);
2440}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002441
Elliott Hughese657eb42021-02-18 17:11:56 -08002442TEST_F(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002443 char buf[16];
2444 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2445 testing::KilledBySignal(SIGABRT),
2446#if defined(NOFORTIFY)
2447 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2448#else
2449 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2450#endif
2451 );
2452}
2453
Elliott Hughese657eb42021-02-18 17:11:56 -08002454TEST_F(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002455 std::string buf = "world";
2456 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2457 testing::KilledBySignal(SIGABRT),
2458 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2459}
2460
2461TEST(STDIO_TEST, sprintf_30445072) {
2462 std::string buf = "world";
2463 sprintf(&buf[0], "hello");
2464 ASSERT_EQ(buf, "hello");
2465}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002466
Elliott Hughes654cd832018-08-30 16:00:42 -07002467TEST(STDIO_TEST, printf_m) {
2468 char buf[BUFSIZ];
2469 errno = 0;
2470 snprintf(buf, sizeof(buf), "<%m>");
2471 ASSERT_STREQ("<Success>", buf);
2472 errno = -1;
2473 snprintf(buf, sizeof(buf), "<%m>");
2474 ASSERT_STREQ("<Unknown error -1>", buf);
2475 errno = EINVAL;
2476 snprintf(buf, sizeof(buf), "<%m>");
Steven Moreland4ef83d62021-10-07 00:19:18 +00002477 ASSERT_STREQ("<Invalid argument>", buf);
Elliott Hughes654cd832018-08-30 16:00:42 -07002478}
2479
Elliott Hughesf340a562018-09-06 10:42:40 -07002480TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2481 char buf[BUFSIZ];
2482 const char* m = strerror(-1);
2483 ASSERT_STREQ("Unknown error -1", m);
2484 errno = -2;
2485 snprintf(buf, sizeof(buf), "<%m>");
2486 ASSERT_STREQ("<Unknown error -2>", buf);
2487 ASSERT_STREQ("Unknown error -1", m);
2488}
2489
Elliott Hughes654cd832018-08-30 16:00:42 -07002490TEST(STDIO_TEST, wprintf_m) {
2491 wchar_t buf[BUFSIZ];
2492 errno = 0;
2493 swprintf(buf, sizeof(buf), L"<%m>");
2494 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2495 errno = -1;
2496 swprintf(buf, sizeof(buf), L"<%m>");
2497 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2498 errno = EINVAL;
2499 swprintf(buf, sizeof(buf), L"<%m>");
Steven Moreland4ef83d62021-10-07 00:19:18 +00002500 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
Elliott Hughes654cd832018-08-30 16:00:42 -07002501}
2502
Elliott Hughesf340a562018-09-06 10:42:40 -07002503TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2504 wchar_t buf[BUFSIZ];
2505 const char* m = strerror(-1);
2506 ASSERT_STREQ("Unknown error -1", m);
2507 errno = -2;
2508 swprintf(buf, sizeof(buf), L"<%m>");
2509 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2510 ASSERT_STREQ("Unknown error -1", m);
2511}
2512
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002513TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2514 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002515 SetFileTo(tf.path, "0123456789");
2516 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002517 EXPECT_EQ(10, ftell(fp));
2518 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2519 EXPECT_EQ(2, ftell(fp));
2520 ASSERT_NE(EOF, fputs("xxx", fp));
2521 ASSERT_EQ(0, fflush(fp));
2522 EXPECT_EQ(13, ftell(fp));
2523 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2524 EXPECT_EQ(13, ftell(fp));
2525 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002526 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002527}
2528
2529TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2530 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002531 SetFileTo(tf.path, "0123456789");
2532 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002533 ASSERT_NE(-1, fd);
2534 // POSIX: "The file position indicator associated with the new stream is set to the position
2535 // indicated by the file offset associated with the file descriptor."
2536 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2537 FILE* fp = fdopen(fd, "a");
2538 EXPECT_EQ(4, ftell(fp));
2539 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2540 EXPECT_EQ(2, ftell(fp));
2541 ASSERT_NE(EOF, fputs("xxx", fp));
2542 ASSERT_EQ(0, fflush(fp));
2543 EXPECT_EQ(13, ftell(fp));
2544 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2545 EXPECT_EQ(13, ftell(fp));
2546 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002547 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002548}
2549
2550TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2551 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002552 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002553 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002554 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002555 EXPECT_EQ(10, ftell(fp));
2556 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2557 EXPECT_EQ(2, ftell(fp));
2558 ASSERT_NE(EOF, fputs("xxx", fp));
2559 ASSERT_EQ(0, fflush(fp));
2560 EXPECT_EQ(13, ftell(fp));
2561 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2562 EXPECT_EQ(13, ftell(fp));
2563 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002564 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002565}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002566
2567TEST(STDIO_TEST, constants) {
2568 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2569 ASSERT_EQ(L_tmpnam, PATH_MAX);
2570}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002571
2572TEST(STDIO_TEST, perror) {
2573 ExecTestHelper eth;
Steven Moreland4ef83d62021-10-07 00:19:18 +00002574 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2575 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2576 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002577}
2578
2579TEST(STDIO_TEST, puts) {
2580 ExecTestHelper eth;
2581 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2582}
2583
Elliott Hughes7cebf832020-08-12 14:25:41 -07002584TEST(STDIO_TEST, putchar) {
2585 ExecTestHelper eth;
2586 eth.Run([&]() { exit(putchar('A')); }, 65, "A");
2587}
2588
2589TEST(STDIO_TEST, putchar_unlocked) {
2590 ExecTestHelper eth;
2591 eth.Run([&]() { exit(putchar('B')); }, 66, "B");
2592}
2593
Elliott Hughes37ad9592017-10-30 17:47:12 -07002594TEST(STDIO_TEST, unlocked) {
2595 TemporaryFile tf;
2596
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002597 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002598 ASSERT_TRUE(fp != nullptr);
2599
2600 clearerr_unlocked(fp);
2601 ASSERT_FALSE(feof_unlocked(fp));
2602 ASSERT_FALSE(ferror_unlocked(fp));
2603
2604 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2605
2606 ASSERT_NE(EOF, putc_unlocked('a', fp));
2607 ASSERT_NE(EOF, putc('b', fp));
2608 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2609 ASSERT_NE(EOF, fputc('d', fp));
2610
2611 rewind(fp);
2612 ASSERT_EQ('a', getc_unlocked(fp));
2613 ASSERT_EQ('b', getc(fp));
2614 ASSERT_EQ('c', fgetc_unlocked(fp));
2615 ASSERT_EQ('d', fgetc(fp));
2616
2617 rewind(fp);
2618 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2619 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2620 ASSERT_EQ(0, fflush_unlocked(fp));
2621
2622 rewind(fp);
2623 char buf[BUFSIZ] = {};
2624 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2625 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2626 ASSERT_STREQ("ABCD", buf);
2627
2628 rewind(fp);
2629 ASSERT_NE(EOF, fputs("hello ", fp));
2630 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2631 ASSERT_NE(EOF, fputc('\n', fp));
2632
2633 rewind(fp);
2634 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2635 ASSERT_STREQ("hello world\n", buf);
2636
2637 ASSERT_EQ(0, fclose(fp));
2638}
Ryan Prichardbf549862017-11-07 15:30:32 -08002639
2640TEST(STDIO_TEST, fseek_64bit) {
2641 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002642 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002643 ASSERT_TRUE(fp != nullptr);
2644 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2645 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2646 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2647 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2648 ASSERT_EQ(0, fclose(fp));
2649}
2650
2651// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2652// isn't representable in long/off_t.
2653TEST(STDIO_TEST, fseek_overflow_32bit) {
2654 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002655 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002656 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2657
2658 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2659#if defined(__BIONIC__) && !defined(__LP64__)
2660 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2661 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2662 ASSERT_EQ(EOVERFLOW, errno);
2663#endif
2664
2665 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2666 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2667 // and SEEK_END -- many C libraries check neither.)
2668 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2669 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2670
2671 fclose(fp);
2672}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002673
2674TEST(STDIO_TEST, dev_std_files) {
2675 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2676 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002677 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2678 ASSERT_LT(0, length);
2679 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2680
2681 length = readlink("/dev/stdout", path, sizeof(path));
2682 ASSERT_LT(0, length);
2683 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2684
2685 length = readlink("/dev/stderr", path, sizeof(path));
2686 ASSERT_LT(0, length);
2687 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002688}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002689
2690TEST(STDIO_TEST, fread_with_locked_file) {
2691 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2692 // files locked on other threads, even if it flushes some line-buffered files.
2693 FILE* fp1 = fopen("/dev/zero", "r");
2694 ASSERT_TRUE(fp1 != nullptr);
2695 flockfile(fp1);
2696
2697 std::thread([] {
2698 for (int mode : { _IONBF, _IOLBF }) {
2699 FILE* fp2 = fopen("/dev/zero", "r");
2700 ASSERT_TRUE(fp2 != nullptr);
2701 setvbuf(fp2, nullptr, mode, 0);
2702 ASSERT_EQ('\0', fgetc(fp2));
2703 fclose(fp2);
2704 }
2705 }).join();
2706
2707 funlockfile(fp1);
2708 fclose(fp1);
2709}
Elliott Hughes31c73092019-05-07 10:03:02 -07002710
2711TEST(STDIO_TEST, SEEK_macros) {
2712 ASSERT_EQ(0, SEEK_SET);
2713 ASSERT_EQ(1, SEEK_CUR);
2714 ASSERT_EQ(2, SEEK_END);
2715 ASSERT_EQ(3, SEEK_DATA);
2716 ASSERT_EQ(4, SEEK_HOLE);
2717 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2718 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2719}
Elliott Hughes05b675e2019-04-17 13:01:06 -07002720
2721TEST(STDIO_TEST, rename) {
2722 TemporaryDir td;
2723 std::string old_path = td.path + "/old"s;
2724 std::string new_path = td.path + "/new"s;
2725
2726 // Create the file, check it exists.
2727 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2728 struct stat sb;
2729 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2730 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2731
2732 // Rename and check it moved.
2733 ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str()));
2734 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2735 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2736}
2737
2738TEST(STDIO_TEST, renameat) {
2739 TemporaryDir td;
2740 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2741 std::string old_path = td.path + "/old"s;
2742 std::string new_path = td.path + "/new"s;
2743
2744 // Create the file, check it exists.
2745 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2746 struct stat sb;
2747 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2748 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2749
2750 // Rename and check it moved.
2751 ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new"));
2752 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2753 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2754}
2755
2756TEST(STDIO_TEST, renameat2) {
Colin Cross4c5595c2021-08-16 15:51:59 -07002757#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -07002758 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28 and musl doesn't have renameat2";
Elliott Hughes05b675e2019-04-17 13:01:06 -07002759#else
2760 TemporaryDir td;
2761 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2762 std::string old_path = td.path + "/old"s;
2763 std::string new_path = td.path + "/new"s;
2764
2765 // Create the file, check it exists.
2766 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2767 struct stat sb;
2768 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2769 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2770
2771 // Rename and check it moved.
2772 ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0));
2773 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2774 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2775
2776 // After this, both "old" and "new" exist.
2777 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2778
2779 // Rename and check it moved.
2780 ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE));
2781 ASSERT_EQ(EEXIST, errno);
2782#endif
2783}
2784
2785TEST(STDIO_TEST, renameat2_flags) {
2786#if defined(__GLIBC__)
2787 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2788#else
2789 ASSERT_NE(0, RENAME_EXCHANGE);
2790 ASSERT_NE(0, RENAME_NOREPLACE);
2791 ASSERT_NE(0, RENAME_WHITEOUT);
2792#endif
2793}
Elliott Hughes7cebf832020-08-12 14:25:41 -07002794
2795TEST(STDIO_TEST, fdopen_failures) {
2796 FILE* fp;
2797 int fd = open("/proc/version", O_RDONLY);
2798 ASSERT_TRUE(fd != -1);
2799
2800 // Nonsense mode.
2801 errno = 0;
2802 fp = fdopen(fd, "nonsense");
2803 ASSERT_TRUE(fp == nullptr);
2804 ASSERT_EQ(EINVAL, errno);
2805
2806 // Mode that isn't a subset of the fd's actual mode.
2807 errno = 0;
2808 fp = fdopen(fd, "w");
2809 ASSERT_TRUE(fp == nullptr);
2810 ASSERT_EQ(EINVAL, errno);
2811
2812 // Can't set append on the underlying fd.
2813 errno = 0;
2814 fp = fdopen(fd, "a");
2815 ASSERT_TRUE(fp == nullptr);
2816 ASSERT_EQ(EINVAL, errno);
2817
2818 // Bad fd.
2819 errno = 0;
2820 fp = fdopen(-1, "re");
2821 ASSERT_TRUE(fp == nullptr);
2822 ASSERT_EQ(EBADF, errno);
2823
2824 close(fd);
2825}
2826
2827TEST(STDIO_TEST, fmemopen_invalid_mode) {
2828 errno = 0;
2829 FILE* fp = fmemopen(nullptr, 16, "nonsense");
2830 ASSERT_TRUE(fp == nullptr);
2831 ASSERT_EQ(EINVAL, errno);
2832}
2833
2834TEST(STDIO_TEST, fopen_invalid_mode) {
2835 errno = 0;
2836 FILE* fp = fopen("/proc/version", "nonsense");
2837 ASSERT_TRUE(fp == nullptr);
2838 ASSERT_EQ(EINVAL, errno);
2839}
2840
2841TEST(STDIO_TEST, freopen_invalid_mode) {
2842 FILE* fp = fopen("/proc/version", "re");
2843 ASSERT_TRUE(fp != nullptr);
2844
2845 errno = 0;
2846 fp = freopen("/proc/version", "nonsense", fp);
2847 ASSERT_TRUE(fp == nullptr);
2848 ASSERT_EQ(EINVAL, errno);
2849}
2850
2851TEST(STDIO_TEST, asprintf_smoke) {
2852 char* p = nullptr;
2853 ASSERT_EQ(11, asprintf(&p, "hello %s", "world"));
2854 ASSERT_STREQ("hello world", p);
2855 free(p);
2856}
2857
2858TEST(STDIO_TEST, fopen_ENOENT) {
2859 errno = 0;
2860 FILE* fp = fopen("/proc/does-not-exist", "re");
2861 ASSERT_TRUE(fp == nullptr);
2862 ASSERT_EQ(ENOENT, errno);
2863}
Elliott Hughes439ebbd2020-12-04 18:51:42 -08002864
2865static void tempnam_test(bool has_TMPDIR, const char* dir, const char* prefix, const char* re) {
2866 if (has_TMPDIR) {
2867 setenv("TMPDIR", "/my/tmp/dir", 1);
2868 } else {
2869 unsetenv("TMPDIR");
2870 }
2871 char* s1 = tempnam(dir, prefix);
2872 char* s2 = tempnam(dir, prefix);
2873 ASSERT_MATCH(s1, re);
2874 ASSERT_MATCH(s2, re);
2875 ASSERT_STRNE(s1, s2);
2876 free(s1);
2877 free(s2);
2878}
2879
2880TEST(STDIO_TEST, tempnam__system_directory_system_prefix_with_TMPDIR) {
2881 tempnam_test(true, nullptr, nullptr, "^/my/tmp/dir/.*");
2882}
2883
2884TEST(STDIO_TEST, tempnam__system_directory_system_prefix_without_TMPDIR) {
2885 tempnam_test(false, nullptr, nullptr, "^/data/local/tmp/.*");
2886}
2887
2888TEST(STDIO_TEST, tempnam__system_directory_user_prefix_with_TMPDIR) {
2889 tempnam_test(true, nullptr, "prefix", "^/my/tmp/dir/prefix.*");
2890}
2891
2892TEST(STDIO_TEST, tempnam__system_directory_user_prefix_without_TMPDIR) {
2893 tempnam_test(false, nullptr, "prefix", "^/data/local/tmp/prefix.*");
2894}
2895
2896TEST(STDIO_TEST, tempnam__user_directory_system_prefix_with_TMPDIR) {
2897 tempnam_test(true, "/a/b/c", nullptr, "^/my/tmp/dir/.*");
2898}
2899
2900TEST(STDIO_TEST, tempnam__user_directory_system_prefix_without_TMPDIR) {
2901 tempnam_test(false, "/a/b/c", nullptr, "^/a/b/c/.*");
2902}
2903
2904TEST(STDIO_TEST, tempnam__user_directory_user_prefix_with_TMPDIR) {
2905 tempnam_test(true, "/a/b/c", "prefix", "^/my/tmp/dir/prefix.*");
2906}
2907
2908TEST(STDIO_TEST, tempnam__user_directory_user_prefix_without_TMPDIR) {
2909 tempnam_test(false, "/a/b/c", "prefix", "^/a/b/c/prefix.*");
2910}
2911
2912static void tmpnam_test(char* s) {
2913 char s1[L_tmpnam], s2[L_tmpnam];
2914
2915 strcpy(s1, tmpnam(s));
2916 strcpy(s2, tmpnam(s));
2917 ASSERT_MATCH(s1, "/tmp/.*");
2918 ASSERT_MATCH(s2, "/tmp/.*");
2919 ASSERT_STRNE(s1, s2);
2920}
2921
2922TEST(STDIO_TEST, tmpnam) {
2923 tmpnam_test(nullptr);
2924}
2925
2926TEST(STDIO_TEST, tmpnam_buf) {
2927 char buf[L_tmpnam];
2928 tmpnam_test(buf);
2929}
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002930
2931TEST(STDIO_TEST, freopen_null_filename_mode) {
2932 TemporaryFile tf;
2933 FILE* fp = fopen(tf.path, "r");
2934 ASSERT_TRUE(fp != nullptr);
2935
2936 // "r" = O_RDONLY
2937 char buf[1];
2938 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2939 ASSERT_EQ(-1, write(fileno(fp), "hello", 1));
2940 // "r+" = O_RDWR
2941 fp = freopen(nullptr, "r+", fp);
2942 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2943 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2944 // "w" = O_WRONLY
2945 fp = freopen(nullptr, "w", fp);
2946 ASSERT_EQ(-1, read(fileno(fp), buf, 1));
2947 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2948
2949 fclose(fp);
2950}
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002951
Elliott Hughes0cac2912022-08-02 18:25:22 +00002952#if defined(__LP64__)
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002953static int64_t GetTotalRamGiB() {
2954 struct sysinfo si;
2955 sysinfo(&si);
2956 return (static_cast<int64_t>(si.totalram) * si.mem_unit) / 1024 / 1024 / 1024;
2957}
Elliott Hughes0cac2912022-08-02 18:25:22 +00002958#endif
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002959
2960TEST(STDIO_TEST, fread_int_overflow) {
Elliott Hughes0cac2912022-08-02 18:25:22 +00002961#if defined(__LP64__)
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002962 if (GetTotalRamGiB() <= 4) GTEST_SKIP() << "not enough memory";
2963
2964 const size_t too_big_for_an_int = 0x80000000ULL;
2965 std::vector<char> buf(too_big_for_an_int);
2966 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/dev/zero", "re"), fclose};
2967 ASSERT_EQ(too_big_for_an_int, fread(&buf[0], 1, too_big_for_an_int, fp.get()));
Elliott Hughes0cac2912022-08-02 18:25:22 +00002968#else
2969 GTEST_SKIP() << "32-bit can't allocate 2GiB";
2970#endif
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002971}
2972
2973TEST(STDIO_TEST, fwrite_int_overflow) {
Elliott Hughes0cac2912022-08-02 18:25:22 +00002974#if defined(__LP64__)
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002975 if (GetTotalRamGiB() <= 4) GTEST_SKIP() << "not enough memory";
2976
2977 const size_t too_big_for_an_int = 0x80000000ULL;
2978 std::vector<char> buf(too_big_for_an_int);
2979 std::unique_ptr<FILE, decltype(&fclose)> fp{fopen("/dev/null", "we"), fclose};
2980 ASSERT_EQ(too_big_for_an_int, fwrite(&buf[0], 1, too_big_for_an_int, fp.get()));
Elliott Hughes0cac2912022-08-02 18:25:22 +00002981#else
2982 GTEST_SKIP() << "32-bit can't allocate 2GiB";
2983#endif
Elliott Hughesbe78fc92022-07-28 20:58:45 +00002984}
Elliott Hughesb813a6a2022-08-01 22:18:40 +00002985
2986TEST(STDIO_TEST, snprintf_b) {
Elliott Hughese50d9d22023-04-19 16:35:40 -07002987#if defined(__BIONIC__)
Elliott Hughesb813a6a2022-08-01 22:18:40 +00002988 char buf[BUFSIZ];
Elliott Hughese50d9d22023-04-19 16:35:40 -07002989
2990 uint8_t b = 5;
2991 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "<%" PRIb8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00002992 EXPECT_STREQ("<101>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07002993 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%08" PRIb8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00002994 EXPECT_STREQ("<00000101>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07002995
2996 uint16_t s = 0xaaaa;
2997 EXPECT_EQ(18, snprintf(buf, sizeof(buf), "<%" PRIb16 ">", s));
2998 EXPECT_STREQ("<1010101010101010>", buf);
2999 EXPECT_EQ(20, snprintf(buf, sizeof(buf), "<%#" PRIb16 ">", s));
3000 EXPECT_STREQ("<0b1010101010101010>", buf);
3001
3002 EXPECT_EQ(34, snprintf(buf, sizeof(buf), "<%" PRIb32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003003 EXPECT_STREQ("<10101010101010101010101010101010>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003004 EXPECT_EQ(36, snprintf(buf, sizeof(buf), "<%#" PRIb32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003005 EXPECT_STREQ("<0b10101010101010101010101010101010>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003006
3007 // clang doesn't like "%lb" (https://github.com/llvm/llvm-project/issues/62247)
3008#pragma clang diagnostic push
3009#pragma clang diagnostic ignored "-Wformat"
3010 EXPECT_EQ(66, snprintf(buf, sizeof(buf), "<%" PRIb64 ">", 0xaaaaaaaa'aaaaaaaa));
3011 EXPECT_STREQ("<1010101010101010101010101010101010101010101010101010101010101010>", buf);
3012 EXPECT_EQ(68, snprintf(buf, sizeof(buf), "<%#" PRIb64 ">", 0xaaaaaaaa'aaaaaaaa));
3013 EXPECT_STREQ("<0b1010101010101010101010101010101010101010101010101010101010101010>", buf);
3014#pragma clang diagnostic pop
3015
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003016 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%#b>", 0));
3017 EXPECT_STREQ("<0>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003018#else
3019 GTEST_SKIP() << "no %b in glibc";
3020#endif
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003021}
3022
3023TEST(STDIO_TEST, snprintf_B) {
Elliott Hughese50d9d22023-04-19 16:35:40 -07003024#if defined(__BIONIC__)
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003025 char buf[BUFSIZ];
Elliott Hughese50d9d22023-04-19 16:35:40 -07003026
3027 uint8_t b = 5;
3028 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "<%" PRIB8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003029 EXPECT_STREQ("<101>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003030 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%08" PRIB8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003031 EXPECT_STREQ("<00000101>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003032
3033 uint16_t s = 0xaaaa;
3034 EXPECT_EQ(18, snprintf(buf, sizeof(buf), "<%" PRIB16 ">", s));
3035 EXPECT_STREQ("<1010101010101010>", buf);
3036 EXPECT_EQ(20, snprintf(buf, sizeof(buf), "<%#" PRIB16 ">", s));
3037 EXPECT_STREQ("<0B1010101010101010>", buf);
3038
3039 EXPECT_EQ(34, snprintf(buf, sizeof(buf), "<%" PRIB32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003040 EXPECT_STREQ("<10101010101010101010101010101010>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003041 EXPECT_EQ(36, snprintf(buf, sizeof(buf), "<%#" PRIB32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003042 EXPECT_STREQ("<0B10101010101010101010101010101010>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003043
3044 // clang doesn't like "%lB" (https://github.com/llvm/llvm-project/issues/62247)
3045#pragma clang diagnostic push
3046#pragma clang diagnostic ignored "-Wformat"
3047 EXPECT_EQ(66, snprintf(buf, sizeof(buf), "<%" PRIB64 ">", 0xaaaaaaaa'aaaaaaaa));
3048 EXPECT_STREQ("<1010101010101010101010101010101010101010101010101010101010101010>", buf);
3049 EXPECT_EQ(68, snprintf(buf, sizeof(buf), "<%#" PRIB64 ">", 0xaaaaaaaa'aaaaaaaa));
3050 EXPECT_STREQ("<0B1010101010101010101010101010101010101010101010101010101010101010>", buf);
3051#pragma clang diagnostic pop
3052
3053 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%#b>", 0));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003054 EXPECT_STREQ("<0>", buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003055#else
3056 GTEST_SKIP() << "no %B in glibc";
3057#endif
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003058}
3059
3060TEST(STDIO_TEST, swprintf_b) {
Elliott Hughese50d9d22023-04-19 16:35:40 -07003061#if defined(__BIONIC__)
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003062 wchar_t buf[BUFSIZ];
Elliott Hughese50d9d22023-04-19 16:35:40 -07003063
3064 uint8_t b = 5;
3065 EXPECT_EQ(5, swprintf(buf, sizeof(buf), L"<%" PRIb8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003066 EXPECT_EQ(std::wstring(L"<101>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003067 EXPECT_EQ(10, swprintf(buf, sizeof(buf), L"<%08" PRIb8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003068 EXPECT_EQ(std::wstring(L"<00000101>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003069
3070 uint16_t s = 0xaaaa;
3071 EXPECT_EQ(18, swprintf(buf, sizeof(buf), L"<%" PRIb16 ">", s));
3072 EXPECT_EQ(std::wstring(L"<1010101010101010>"), buf);
3073 EXPECT_EQ(20, swprintf(buf, sizeof(buf), L"<%#" PRIb16 ">", s));
3074 EXPECT_EQ(std::wstring(L"<0b1010101010101010>"), buf);
3075
3076 EXPECT_EQ(34, swprintf(buf, sizeof(buf), L"<%" PRIb32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003077 EXPECT_EQ(std::wstring(L"<10101010101010101010101010101010>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003078 EXPECT_EQ(36, swprintf(buf, sizeof(buf), L"<%#" PRIb32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003079 EXPECT_EQ(std::wstring(L"<0b10101010101010101010101010101010>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003080
3081#pragma clang diagnostic push
3082#pragma clang diagnostic ignored "-Wformat" // clang doesn't like "%lb"
3083 EXPECT_EQ(66, swprintf(buf, sizeof(buf), L"<%" PRIb64 ">", 0xaaaaaaaa'aaaaaaaa));
3084 EXPECT_EQ(std::wstring(L"<1010101010101010101010101010101010101010101010101010101010101010>"),
3085 buf);
3086 EXPECT_EQ(68, swprintf(buf, sizeof(buf), L"<%#" PRIb64 ">", 0xaaaaaaaa'aaaaaaaa));
3087 EXPECT_EQ(std::wstring(L"<0b1010101010101010101010101010101010101010101010101010101010101010>"),
3088 buf);
3089#pragma clang diagnostic pop
3090
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003091 EXPECT_EQ(3, swprintf(buf, sizeof(buf), L"<%#b>", 0));
3092 EXPECT_EQ(std::wstring(L"<0>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003093#else
3094 GTEST_SKIP() << "no %b in glibc";
3095#endif
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003096}
3097
3098TEST(STDIO_TEST, swprintf_B) {
Elliott Hughese50d9d22023-04-19 16:35:40 -07003099#if defined(__BIONIC__)
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003100 wchar_t buf[BUFSIZ];
Elliott Hughese50d9d22023-04-19 16:35:40 -07003101
3102 uint8_t b = 5;
3103 EXPECT_EQ(5, swprintf(buf, sizeof(buf), L"<%" PRIB8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003104 EXPECT_EQ(std::wstring(L"<101>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003105 EXPECT_EQ(10, swprintf(buf, sizeof(buf), L"<%08" PRIB8 ">", b));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003106 EXPECT_EQ(std::wstring(L"<00000101>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003107
3108 uint16_t s = 0xaaaa;
3109 EXPECT_EQ(18, swprintf(buf, sizeof(buf), L"<%" PRIB16 ">", s));
3110 EXPECT_EQ(std::wstring(L"<1010101010101010>"), buf);
3111 EXPECT_EQ(20, swprintf(buf, sizeof(buf), L"<%#" PRIB16 ">", s));
3112 EXPECT_EQ(std::wstring(L"<0B1010101010101010>"), buf);
3113
3114 EXPECT_EQ(34, swprintf(buf, sizeof(buf), L"<%" PRIB32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003115 EXPECT_EQ(std::wstring(L"<10101010101010101010101010101010>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003116 EXPECT_EQ(36, swprintf(buf, sizeof(buf), L"<%#" PRIB32 ">", 0xaaaaaaaa));
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003117 EXPECT_EQ(std::wstring(L"<0B10101010101010101010101010101010>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003118
3119#pragma clang diagnostic push
3120#pragma clang diagnostic ignored "-Wformat" // clang doesn't like "%lb"
3121 EXPECT_EQ(66, swprintf(buf, sizeof(buf), L"<%" PRIB64 ">", 0xaaaaaaaa'aaaaaaaa));
3122 EXPECT_EQ(std::wstring(L"<1010101010101010101010101010101010101010101010101010101010101010>"),
3123 buf);
3124 EXPECT_EQ(68, swprintf(buf, sizeof(buf), L"<%#" PRIB64 ">", 0xaaaaaaaa'aaaaaaaa));
3125 EXPECT_EQ(std::wstring(L"<0B1010101010101010101010101010101010101010101010101010101010101010>"),
3126 buf);
3127#pragma clang diagnostic pop
3128
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003129 EXPECT_EQ(3, swprintf(buf, sizeof(buf), L"<%#B>", 0));
3130 EXPECT_EQ(std::wstring(L"<0>"), buf);
Elliott Hughese50d9d22023-04-19 16:35:40 -07003131#else
3132 GTEST_SKIP() << "no %B in glibc";
3133#endif
Elliott Hughesb813a6a2022-08-01 22:18:40 +00003134}
Elliott Hughes1f462de2022-08-05 22:51:05 +00003135
3136TEST(STDIO_TEST, scanf_i_decimal) {
3137 int i;
3138 EXPECT_EQ(1, sscanf("<123789>", "<%i>", &i));
3139 EXPECT_EQ(123789, i);
3140
3141 long long int lli;
3142 char ch;
3143 EXPECT_EQ(2, sscanf("1234567890abcdefg", "%lli%c", &lli, &ch));
3144 EXPECT_EQ(1234567890, lli);
3145 EXPECT_EQ('a', ch);
3146}
3147
3148TEST(STDIO_TEST, scanf_i_hex) {
3149 int i;
3150 EXPECT_EQ(1, sscanf("<0x123abf>", "<%i>", &i));
3151 EXPECT_EQ(0x123abf, i);
3152
3153 long long int lli;
3154 char ch;
3155 EXPECT_EQ(2, sscanf("0x1234567890abcdefg", "%lli%c", &lli, &ch));
3156 EXPECT_EQ(0x1234567890abcdefLL, lli);
3157 EXPECT_EQ('g', ch);
3158}
3159
3160TEST(STDIO_TEST, scanf_i_octal) {
3161 int i;
3162 EXPECT_EQ(1, sscanf("<01234567>", "<%i>", &i));
3163 EXPECT_EQ(01234567, i);
3164
3165 long long int lli;
3166 char ch;
3167 EXPECT_EQ(2, sscanf("010234567890abcdefg", "%lli%c", &lli, &ch));
3168 EXPECT_EQ(010234567, lli);
3169 EXPECT_EQ('8', ch);
3170}
3171
3172TEST(STDIO_TEST, scanf_i_binary) {
3173 int i;
3174 EXPECT_EQ(1, sscanf("<0b101>", "<%i>", &i));
3175 EXPECT_EQ(0b101, i);
3176
3177 long long int lli;
3178 char ch;
3179 EXPECT_EQ(2, sscanf("0b10234567890abcdefg", "%lli%c", &lli, &ch));
3180 EXPECT_EQ(0b10, lli);
3181 EXPECT_EQ('2', ch);
3182}
3183
3184TEST(STDIO_TEST, wscanf_i_decimal) {
3185 int i;
3186 EXPECT_EQ(1, swscanf(L"<123789>", L"<%i>", &i));
3187 EXPECT_EQ(123789, i);
3188
3189 long long int lli;
3190 char ch;
3191 EXPECT_EQ(2, swscanf(L"1234567890abcdefg", L"%lli%c", &lli, &ch));
3192 EXPECT_EQ(1234567890, lli);
3193 EXPECT_EQ('a', ch);
3194}
3195
3196TEST(STDIO_TEST, wscanf_i_hex) {
3197 int i;
3198 EXPECT_EQ(1, swscanf(L"<0x123abf>", L"<%i>", &i));
3199 EXPECT_EQ(0x123abf, i);
3200
3201 long long int lli;
3202 char ch;
3203 EXPECT_EQ(2, swscanf(L"0x1234567890abcdefg", L"%lli%c", &lli, &ch));
3204 EXPECT_EQ(0x1234567890abcdefLL, lli);
3205 EXPECT_EQ('g', ch);
3206}
3207
3208TEST(STDIO_TEST, wscanf_i_octal) {
3209 int i;
3210 EXPECT_EQ(1, swscanf(L"<01234567>", L"<%i>", &i));
3211 EXPECT_EQ(01234567, i);
3212
3213 long long int lli;
3214 char ch;
3215 EXPECT_EQ(2, swscanf(L"010234567890abcdefg", L"%lli%c", &lli, &ch));
3216 EXPECT_EQ(010234567, lli);
3217 EXPECT_EQ('8', ch);
3218}
3219
3220TEST(STDIO_TEST, wscanf_i_binary) {
3221 int i;
3222 EXPECT_EQ(1, swscanf(L"<0b101>", L"<%i>", &i));
3223 EXPECT_EQ(0b101, i);
3224
3225 long long int lli;
3226 char ch;
3227 EXPECT_EQ(2, swscanf(L"0b10234567890abcdefg", L"%lli%c", &lli, &ch));
3228 EXPECT_EQ(0b10, lli);
3229 EXPECT_EQ('2', ch);
3230}
3231
3232TEST(STDIO_TEST, scanf_b) {
Elliott Hughes1f462de2022-08-05 22:51:05 +00003233 int i;
3234 char ch;
3235 EXPECT_EQ(2, sscanf("<1012>", "<%b%c>", &i, &ch));
3236 EXPECT_EQ(0b101, i);
3237 EXPECT_EQ('2', ch);
3238 EXPECT_EQ(1, sscanf("<00000101>", "<%08b>", &i));
3239 EXPECT_EQ(0b00000101, i);
3240 EXPECT_EQ(1, sscanf("<0b1010>", "<%b>", &i));
3241 EXPECT_EQ(0b1010, i);
3242 EXPECT_EQ(2, sscanf("-0b", "%i%c", &i, &ch));
3243 EXPECT_EQ(0, i);
3244 EXPECT_EQ('b', ch);
Elliott Hughes1f462de2022-08-05 22:51:05 +00003245}
3246
3247TEST(STDIO_TEST, swscanf_b) {
Elliott Hughes1f462de2022-08-05 22:51:05 +00003248 int i;
3249 char ch;
3250 EXPECT_EQ(2, swscanf(L"<1012>", L"<%b%c>", &i, &ch));
3251 EXPECT_EQ(0b101, i);
3252 EXPECT_EQ('2', ch);
3253 EXPECT_EQ(1, swscanf(L"<00000101>", L"<%08b>", &i));
3254 EXPECT_EQ(0b00000101, i);
3255 EXPECT_EQ(1, swscanf(L"<0b1010>", L"<%b>", &i));
3256 EXPECT_EQ(0b1010, i);
3257 EXPECT_EQ(2, swscanf(L"-0b", L"%i%c", &i, &ch));
3258 EXPECT_EQ(0, i);
3259 EXPECT_EQ('b', ch);
Elliott Hughes1f462de2022-08-05 22:51:05 +00003260}