blob: e38dd60774c2e0daa3835648aa94cabb92c3bb5d [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>
Colin Cross14d15072021-08-16 16:35:27 -070028#include <sys/types.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070029#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070030#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010031
Elliott Hughes3a4c4542017-07-19 17:20:24 -070032#include <string>
Ryan Prichardc485cdb2019-04-30 14:47:34 -070033#include <thread>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080034#include <vector>
35
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080036#include <android-base/file.h>
Elliott Hughes141b9172021-04-09 17:13:09 -070037#include <android-base/silent_death_test.h>
Elliott Hughes439ebbd2020-12-04 18:51:42 -080038#include <android-base/test_utils.h>
Elliott Hughes05b675e2019-04-17 13:01:06 -070039#include <android-base/unique_fd.h>
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080040
Josh Gao2f06e102017-01-10 13:00:37 -080041#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070042
Elliott Hughes05b675e2019-04-17 13:01:06 -070043// This #include is actually a test too. We have to duplicate the
44// definitions of the RENAME_ constants because <linux/fs.h> also contains
45// pollution such as BLOCK_SIZE which conflicts with lots of user code.
46// Important to check that we have matching definitions.
47// There's no _MAX to test that we have all the constants, sadly.
48#include <linux/fs.h>
49
Christopher Ferris13f26a72016-01-13 13:47:58 -080050#if defined(NOFORTIFY)
51#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070052#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080053#else
54#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070055#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080056#endif
57
Elliott Hughes3a4c4542017-07-19 17:20:24 -070058using namespace std::string_literals;
59
Elliott Hughes141b9172021-04-09 17:13:09 -070060using stdio_DeathTest = SilentDeathTest;
61using stdio_nofortify_DeathTest = SilentDeathTest;
Elliott Hughesfb3873d2016-08-10 11:07:54 -070062
Elliott Hughes33a8cb12017-07-25 18:06:46 -070063static void SetFileTo(const char* path, const char* content) {
64 FILE* fp;
65 ASSERT_NE(nullptr, fp = fopen(path, "w"));
66 ASSERT_NE(EOF, fputs(content, fp));
67 ASSERT_EQ(0, fclose(fp));
68}
69
70static void AssertFileIs(const char* path, const char* expected) {
71 FILE* fp;
72 ASSERT_NE(nullptr, fp = fopen(path, "r"));
73 char* line = nullptr;
74 size_t length;
75 ASSERT_NE(EOF, getline(&line, &length, fp));
76 ASSERT_EQ(0, fclose(fp));
77 ASSERT_STREQ(expected, line);
78 free(line);
79}
80
Elliott Hughes70715da2016-08-01 16:35:17 -070081static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
82 rewind(fp);
83
84 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080085 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070086 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
87 ASSERT_STREQ(expected, line);
88
89 if (is_fmemopen) {
90 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
91 // extra empty line, but does on every C library I tested...
92 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
93 ASSERT_STREQ("", line);
94 }
95
96 // Make sure there isn't anything else in the file.
97 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
98}
99
Christopher Ferris13f26a72016-01-13 13:47:58 -0800100TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800101 // Check that we have a _recursive_ mutex for flockfile.
102 flockfile(stderr);
103 feof(stderr); // We don't care about the result, but this needs to take the lock.
104 funlockfile(stderr);
105}
106
Christopher Ferris13f26a72016-01-13 13:47:58 -0800107TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800108 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
109 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700110 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800111 flockfile(fp);
112 feof(fp);
113 funlockfile(fp);
114 fclose(fp);
115}
116
Christopher Ferris13f26a72016-01-13 13:47:58 -0800117TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700118 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700119 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700120
121 int fd = fileno(fp);
122 ASSERT_NE(fd, -1);
123
124 struct stat sb;
125 int rc = fstat(fd, &sb);
126 ASSERT_NE(rc, -1);
127 ASSERT_EQ(sb.st_mode & 0777, 0600U);
128
129 rc = fprintf(fp, "hello\n");
130 ASSERT_EQ(rc, 6);
131
Elliott Hughes70715da2016-08-01 16:35:17 -0700132 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700133 fclose(fp);
134}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300135
Elliott Hughesf226ee52016-02-03 11:24:28 -0800136TEST(STDIO_TEST, tmpfile64) {
137 FILE* fp = tmpfile64();
138 ASSERT_TRUE(fp != nullptr);
139 fclose(fp);
140}
141
Christopher Ferris13f26a72016-01-13 13:47:58 -0800142TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100143 TemporaryFile tf;
144
145 int rc = dprintf(tf.fd, "hello\n");
146 ASSERT_EQ(rc, 6);
147
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800148 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700149 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700150 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100151
Elliott Hughes70715da2016-08-01 16:35:17 -0700152 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700153 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100154}
155
Christopher Ferris13f26a72016-01-13 13:47:58 -0800156TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300157 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700158 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300159
160 const char* line_written = "This is a test";
161 int rc = fprintf(fp, "%s", line_written);
162 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
163
164 rewind(fp);
165
Yi Kong32bc0fc2018-08-02 17:31:13 -0700166 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300167 size_t allocated_length = 0;
168
169 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
170 for (size_t i = 0; i < 5; ++i) {
171 ASSERT_FALSE(feof(fp));
172 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
173 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800174 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300175 }
176 // The last read should have set the end-of-file indicator for the stream.
177 ASSERT_TRUE(feof(fp));
178 clearerr(fp);
179
180 // getdelim returns -1 but doesn't set errno if we're already at EOF.
181 // It should set the end-of-file indicator for the stream, though.
182 errno = 0;
183 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800184 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300185 ASSERT_TRUE(feof(fp));
186
187 free(word_read);
188 fclose(fp);
189}
190
Christopher Ferris13f26a72016-01-13 13:47:58 -0800191TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300192 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700193 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300194
Yi Kong32bc0fc2018-08-02 17:31:13 -0700195 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300196 size_t buffer_length = 0;
197
198 // The first argument can't be NULL.
199 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700200 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800201 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300202
203 // The second argument can't be NULL.
204 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700205 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800206 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700207 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300208}
209
Christopher Ferris13f26a72016-01-13 13:47:58 -0800210TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700211 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700212 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700213 char* word_read;
214 size_t allocated_length;
215 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
216 fclose(fp);
217}
218
Christopher Ferris13f26a72016-01-13 13:47:58 -0800219TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300220 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700221 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300222
223 const char* line_written = "This is a test for getline\n";
224 const size_t line_count = 5;
225
226 for (size_t i = 0; i < line_count; ++i) {
227 int rc = fprintf(fp, "%s", line_written);
228 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
229 }
230
231 rewind(fp);
232
Yi Kong32bc0fc2018-08-02 17:31:13 -0700233 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300234 size_t allocated_length = 0;
235
236 size_t read_line_count = 0;
237 ssize_t read_char_count;
238 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
239 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
240 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800241 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300242 ++read_line_count;
243 }
244 ASSERT_EQ(read_line_count, line_count);
245
246 // The last read should have set the end-of-file indicator for the stream.
247 ASSERT_TRUE(feof(fp));
248 clearerr(fp);
249
250 // getline returns -1 but doesn't set errno if we're already at EOF.
251 // It should set the end-of-file indicator for the stream, though.
252 errno = 0;
253 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800254 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300255 ASSERT_TRUE(feof(fp));
256
257 free(line_read);
258 fclose(fp);
259}
260
Christopher Ferris13f26a72016-01-13 13:47:58 -0800261TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300262 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700263 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300264
Yi Kong32bc0fc2018-08-02 17:31:13 -0700265 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300266 size_t buffer_length = 0;
267
268 // The first argument can't be NULL.
269 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700270 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800271 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300272
273 // The second argument can't be NULL.
274 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700275 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800276 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700277 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300278}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000279
Christopher Ferris13f26a72016-01-13 13:47:58 -0800280TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800281 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800282 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800283 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
284 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000285 // error: format '%zd' expects argument of type 'signed size_t',
286 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
287 ssize_t v = 1;
288 char buf[32];
289 snprintf(buf, sizeof(buf), "%zd", v);
290}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800291
Elliott Hughes05493712014-04-17 17:30:03 -0700292// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800293TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700294 char buf[BUFSIZ];
295 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
296 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
297}
298
Elliott Hughescdb4a262020-06-05 16:56:53 -0700299// http://b/152588929
300TEST(STDIO_TEST, snprintf_La) {
301#if defined(__LP64__)
302 char buf[BUFSIZ];
303 union {
304 uint64_t a[2];
305 long double v;
306 } u;
307
308 u.a[0] = UINT64_C(0x9b9b9b9b9b9b9b9b);
309 u.a[1] = UINT64_C(0xdfdfdfdfdfdfdfdf);
310 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
311 EXPECT_STREQ("<-0x1.dfdfdfdfdfdf9b9b9b9b9b9b9b9bp+8160>", buf);
312
313 u.a[0] = UINT64_C(0xffffffffffffffff);
314 u.a[1] = UINT64_C(0x7ffeffffffffffff);
315 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
316 EXPECT_STREQ("<0x1.ffffffffffffffffffffffffffffp+16383>", buf);
317
318 u.a[0] = UINT64_C(0x0000000000000000);
319 u.a[1] = UINT64_C(0x0000000000000000);
320 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%La>", u.v));
321 EXPECT_STREQ("<0x0p+0>", buf);
322#else
323 GTEST_SKIP() << "no ld128";
324#endif
325}
326
Christopher Ferris13f26a72016-01-13 13:47:58 -0800327TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700328 char buf[BUFSIZ];
329 wint_t wc = L'a';
330 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
331 EXPECT_STREQ("<a>", buf);
332}
333
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700334TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
335 char buf[BUFSIZ];
336 wchar_t wc = L'a';
337 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
338 EXPECT_STREQ("<a>", buf);
339}
340
Christopher Ferris13f26a72016-01-13 13:47:58 -0800341TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700342 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700343 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700344 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
345 EXPECT_STREQ("<(null)>", buf);
346
347 wchar_t chars[] = { L'h', L'i', 0 };
348 ws = chars;
349 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
350 EXPECT_STREQ("<hi>", buf);
351}
352
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700353TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
354 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700355 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700356 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
357 EXPECT_STREQ("<(null)>", buf);
358
359 wchar_t chars[] = { L'h', L'i', 0 };
360 ws = chars;
361 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
362 EXPECT_STREQ("<hi>", buf);
363}
364
Elliott Hughese657eb42021-02-18 17:11:56 -0800365TEST_F(STDIO_DEATHTEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700366#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800367 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700368 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700369 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800370 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700371#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800372 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700373#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700374}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700375
Elliott Hughes7cebf832020-08-12 14:25:41 -0700376TEST(STDIO_TEST, snprintf_measure) {
377 char buf[16];
378 ASSERT_EQ(11, snprintf(buf, 0, "Hello %s", "world"));
379}
380
Christopher Ferris13f26a72016-01-13 13:47:58 -0800381TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700382 char buf[BUFSIZ];
383
384 snprintf(buf, sizeof(buf), "a");
385 EXPECT_STREQ("a", buf);
386
387 snprintf(buf, sizeof(buf), "%%");
388 EXPECT_STREQ("%", buf);
389
390 snprintf(buf, sizeof(buf), "01234");
391 EXPECT_STREQ("01234", buf);
392
393 snprintf(buf, sizeof(buf), "a%sb", "01234");
394 EXPECT_STREQ("a01234b", buf);
395
Yi Kong32bc0fc2018-08-02 17:31:13 -0700396 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700397 snprintf(buf, sizeof(buf), "a%sb", s);
398 EXPECT_STREQ("a(null)b", buf);
399
400 snprintf(buf, sizeof(buf), "aa%scc", "bb");
401 EXPECT_STREQ("aabbcc", buf);
402
403 snprintf(buf, sizeof(buf), "a%cc", 'b');
404 EXPECT_STREQ("abc", buf);
405
406 snprintf(buf, sizeof(buf), "a%db", 1234);
407 EXPECT_STREQ("a1234b", buf);
408
409 snprintf(buf, sizeof(buf), "a%db", -8123);
410 EXPECT_STREQ("a-8123b", buf);
411
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700412 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700413 EXPECT_STREQ("a16b", buf);
414
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700415 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700416 EXPECT_STREQ("a16b", buf);
417
418 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
419 EXPECT_STREQ("a68719476736b", buf);
420
421 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
422 EXPECT_STREQ("a70000b", buf);
423
424 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
425 EXPECT_STREQ("a0xb0001234b", buf);
426
427 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
428 EXPECT_STREQ("a12abz", buf);
429
430 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
431 EXPECT_STREQ("a12ABz", buf);
432
433 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
434 EXPECT_STREQ("a00123456z", buf);
435
436 snprintf(buf, sizeof(buf), "a%5dz", 1234);
437 EXPECT_STREQ("a 1234z", buf);
438
439 snprintf(buf, sizeof(buf), "a%05dz", 1234);
440 EXPECT_STREQ("a01234z", buf);
441
442 snprintf(buf, sizeof(buf), "a%8dz", 1234);
443 EXPECT_STREQ("a 1234z", buf);
444
445 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
446 EXPECT_STREQ("a1234 z", buf);
447
448 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
449 EXPECT_STREQ("Aabcdef Z", buf);
450
451 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
452 EXPECT_STREQ("Ahello:1234Z", buf);
453
454 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
455 EXPECT_STREQ("a005:5:05z", buf);
456
Yi Kong32bc0fc2018-08-02 17:31:13 -0700457 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700458 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700459#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700460 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800461#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700462 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800463#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700464
465 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
466 EXPECT_STREQ("a68719476736,6,7,8z", buf);
467
468 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
469 EXPECT_STREQ("a_1.230000_b", buf);
470
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700471 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700472 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400473
474 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
475 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700476}
477
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800478template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700479static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
480 int sscanf_fn(const T*, const T*, ...),
481 const T* fmt_string, const T* fmt, const T* fmt_plus,
482 const T* minus_inf, const T* inf_, const T* plus_inf,
483 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800484 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700485 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700486
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700487 // NaN.
488
489 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800490 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700491 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
492 EXPECT_TRUE(isnan(f));
493
494 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800495 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700496 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
497 EXPECT_TRUE(isnan(f));
498
499 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800500 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700501 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
502 EXPECT_TRUE(isnan(f));
503
504 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800505 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700506 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
507 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800508
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700509 // Inf.
510
511 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800512 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700513 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
514 EXPECT_EQ(HUGE_VALF, f);
515
516 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800517 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700518 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
519 EXPECT_EQ(-HUGE_VALF, f);
520
521 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800522 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
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_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800527 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700528 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
529 EXPECT_EQ(-HUGE_VALF, f);
530
531 // Check case-insensitivity.
532 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
533 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
534 EXPECT_EQ(HUGE_VALF, f);
535 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
536 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
537 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700538}
539
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700540TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
541 CheckInfNan(snprintf, sscanf, "%s",
542 "[%a]", "[%+a]",
543 "[-inf]", "[inf]", "[+inf]",
544 "[-nan]", "[nan]", "[+nan]");
545 CheckInfNan(snprintf, sscanf, "%s",
546 "[%A]", "[%+A]",
547 "[-INF]", "[INF]", "[+INF]",
548 "[-NAN]", "[NAN]", "[+NAN]");
549 CheckInfNan(snprintf, sscanf, "%s",
550 "[%e]", "[%+e]",
551 "[-inf]", "[inf]", "[+inf]",
552 "[-nan]", "[nan]", "[+nan]");
553 CheckInfNan(snprintf, sscanf, "%s",
554 "[%E]", "[%+E]",
555 "[-INF]", "[INF]", "[+INF]",
556 "[-NAN]", "[NAN]", "[+NAN]");
557 CheckInfNan(snprintf, sscanf, "%s",
558 "[%f]", "[%+f]",
559 "[-inf]", "[inf]", "[+inf]",
560 "[-nan]", "[nan]", "[+nan]");
561 CheckInfNan(snprintf, sscanf, "%s",
562 "[%F]", "[%+F]",
563 "[-INF]", "[INF]", "[+INF]",
564 "[-NAN]", "[NAN]", "[+NAN]");
565 CheckInfNan(snprintf, sscanf, "%s",
566 "[%g]", "[%+g]",
567 "[-inf]", "[inf]", "[+inf]",
568 "[-nan]", "[nan]", "[+nan]");
569 CheckInfNan(snprintf, sscanf, "%s",
570 "[%G]", "[%+G]",
571 "[-INF]", "[INF]", "[+INF]",
572 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800573}
Elliott Hughes7823f322014-04-14 12:11:28 -0700574
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700575TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
576 CheckInfNan(swprintf, swscanf, L"%s",
577 L"[%a]", L"[%+a]",
578 L"[-inf]", L"[inf]", L"[+inf]",
579 L"[-nan]", L"[nan]", L"[+nan]");
580 CheckInfNan(swprintf, swscanf, L"%s",
581 L"[%A]", L"[%+A]",
582 L"[-INF]", L"[INF]", L"[+INF]",
583 L"[-NAN]", L"[NAN]", L"[+NAN]");
584 CheckInfNan(swprintf, swscanf, L"%s",
585 L"[%e]", L"[%+e]",
586 L"[-inf]", L"[inf]", L"[+inf]",
587 L"[-nan]", L"[nan]", L"[+nan]");
588 CheckInfNan(swprintf, swscanf, L"%s",
589 L"[%E]", L"[%+E]",
590 L"[-INF]", L"[INF]", L"[+INF]",
591 L"[-NAN]", L"[NAN]", L"[+NAN]");
592 CheckInfNan(swprintf, swscanf, L"%s",
593 L"[%f]", L"[%+f]",
594 L"[-inf]", L"[inf]", L"[+inf]",
595 L"[-nan]", L"[nan]", L"[+nan]");
596 CheckInfNan(swprintf, swscanf, L"%s",
597 L"[%F]", L"[%+F]",
598 L"[-INF]", L"[INF]", L"[+INF]",
599 L"[-NAN]", L"[NAN]", L"[+NAN]");
600 CheckInfNan(swprintf, swscanf, L"%s",
601 L"[%g]", L"[%+g]",
602 L"[-inf]", L"[inf]", L"[+inf]",
603 L"[-nan]", L"[nan]", L"[+nan]");
604 CheckInfNan(swprintf, swscanf, L"%s",
605 L"[%G]", L"[%+G]",
606 L"[-INF]", L"[INF]", L"[+INF]",
607 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700608}
609
Dan Albert9601f162017-08-09 14:59:06 -0700610TEST(STDIO_TEST, swprintf) {
611 constexpr size_t nchars = 32;
612 wchar_t buf[nchars];
613
614 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
615 ASSERT_EQ(std::wstring(L"ab"), buf);
616 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
617 ASSERT_EQ(std::wstring(L"abcde"), buf);
618
619 // Unlike swprintf(), swprintf() returns -1 in case of truncation
620 // and doesn't necessarily zero-terminate the output!
621 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
622
623 const char kString[] = "Hello, World";
624 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
625 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
626 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
627 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
628}
629
630TEST(STDIO_TEST, swprintf_a) {
631 constexpr size_t nchars = 32;
632 wchar_t buf[nchars];
633
634 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
635 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
636}
637
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700638TEST(STDIO_TEST, swprintf_lc) {
639 constexpr size_t nchars = 32;
640 wchar_t buf[nchars];
641
642 wint_t wc = L'a';
643 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
644 EXPECT_EQ(std::wstring(L"<a>"), buf);
645}
646
647TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
648 constexpr size_t nchars = 32;
649 wchar_t buf[nchars];
650
651 wint_t wc = L'a';
652 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
653 EXPECT_EQ(std::wstring(L"<a>"), buf);
654}
655
Elliott Hughes618303c2017-11-02 16:58:44 -0700656TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
657 constexpr size_t nchars = 32;
658 wchar_t buf[nchars];
659
660 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
661 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
662}
663
664TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
665 constexpr size_t nchars = 32;
666 wchar_t buf[nchars];
667
668 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
669 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
670}
671
672TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
673 constexpr size_t nchars = 32;
674 wchar_t buf[nchars];
675
676 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
677 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
678}
679
680TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
681 constexpr size_t nchars = 32;
682 wchar_t buf[nchars];
683
684 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
685 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
686}
687
Dan Albert9601f162017-08-09 14:59:06 -0700688TEST(STDIO_TEST, swprintf_ls) {
689 constexpr size_t nchars = 32;
690 wchar_t buf[nchars];
691
692 static const wchar_t kWideString[] = L"Hello\uff41 World";
693 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
694 ASSERT_EQ(std::wstring(kWideString), buf);
695 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
696 ASSERT_EQ(std::wstring(kWideString), buf);
697}
698
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700699TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
700 constexpr size_t nchars = 32;
701 wchar_t buf[nchars];
702
703 static const wchar_t kWideString[] = L"Hello\uff41 World";
704 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
705 ASSERT_EQ(std::wstring(kWideString), buf);
706 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
707 ASSERT_EQ(std::wstring(kWideString), buf);
708}
709
Christopher Ferris13f26a72016-01-13 13:47:58 -0800710TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700711 char buf[BUFSIZ];
712 snprintf(buf, sizeof(buf), "%d", INT_MAX);
713 EXPECT_STREQ("2147483647", buf);
714}
715
Christopher Ferris13f26a72016-01-13 13:47:58 -0800716TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700717 char buf[BUFSIZ];
718 snprintf(buf, sizeof(buf), "%d", INT_MIN);
719 EXPECT_STREQ("-2147483648", buf);
720}
721
Elliott Hughes618303c2017-11-02 16:58:44 -0700722TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
723 char buf[BUFSIZ];
724 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
725 EXPECT_STREQ("9223372036854775807", buf);
726}
727
728TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
729 char buf[BUFSIZ];
730 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
731 EXPECT_STREQ("-9223372036854775808", buf);
732}
733
734TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
735 char buf[BUFSIZ];
736 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
737 EXPECT_STREQ("18446744073709551615", buf);
738}
739
740TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
741 char buf[BUFSIZ];
742 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
743 EXPECT_STREQ("18446744073709551615", buf);
744}
745
Christopher Ferris13f26a72016-01-13 13:47:58 -0800746TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700747 char buf[BUFSIZ];
748 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700749#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700750 EXPECT_STREQ("9223372036854775807", buf);
751#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700752 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700753#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700754}
755
Christopher Ferris13f26a72016-01-13 13:47:58 -0800756TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700757 char buf[BUFSIZ];
758 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700759#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700760 EXPECT_STREQ("-9223372036854775808", buf);
761#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700762 EXPECT_STREQ("-2147483648", 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_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700767 char buf[BUFSIZ];
768 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
769 EXPECT_STREQ("9223372036854775807", buf);
770}
771
Christopher Ferris13f26a72016-01-13 13:47:58 -0800772TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700773 char buf[BUFSIZ];
774 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
775 EXPECT_STREQ("-9223372036854775808", buf);
776}
777
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700778TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
779 char buf[BUFSIZ];
780 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
781 EXPECT_STREQ("37777777777", buf);
782}
783
784TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
785 char buf[BUFSIZ];
786 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
787 EXPECT_STREQ("4294967295", buf);
788}
789
790TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
791 char buf[BUFSIZ];
792 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
793 EXPECT_STREQ("ffffffff", buf);
794}
795
796TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
797 char buf[BUFSIZ];
798 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
799 EXPECT_STREQ("FFFFFFFF", buf);
800}
801
Christopher Ferris13f26a72016-01-13 13:47:58 -0800802TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700803 char buf[BUFSIZ];
804
805 snprintf(buf, sizeof(buf), "%e", 1.5);
806 EXPECT_STREQ("1.500000e+00", buf);
807
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800808 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700809 EXPECT_STREQ("1.500000e+00", buf);
810}
811
Christopher Ferris13f26a72016-01-13 13:47:58 -0800812TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700813 char buf[BUFSIZ];
814
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800815 snprintf(buf, sizeof(buf), "%e", -0.0);
816 EXPECT_STREQ("-0.000000e+00", buf);
817 snprintf(buf, sizeof(buf), "%E", -0.0);
818 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700819 snprintf(buf, sizeof(buf), "%f", -0.0);
820 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800821 snprintf(buf, sizeof(buf), "%F", -0.0);
822 EXPECT_STREQ("-0.000000", buf);
823 snprintf(buf, sizeof(buf), "%g", -0.0);
824 EXPECT_STREQ("-0", buf);
825 snprintf(buf, sizeof(buf), "%G", -0.0);
826 EXPECT_STREQ("-0", buf);
827 snprintf(buf, sizeof(buf), "%a", -0.0);
828 EXPECT_STREQ("-0x0p+0", buf);
829 snprintf(buf, sizeof(buf), "%A", -0.0);
830 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700831}
832
Christopher Ferris13f26a72016-01-13 13:47:58 -0800833TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700834 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700835 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700836
Elliott Hughes69f05d22014-06-05 20:10:09 -0700837 // http://b/15439554
838 char buf[BUFSIZ];
839
840 // 1-byte character.
841 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
842 EXPECT_STREQ("1x2", buf);
843 // 2-byte character.
844 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
845 EXPECT_STREQ("1¢2", buf);
846 // 3-byte character.
847 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
848 EXPECT_STREQ("1€2", buf);
849 // 4-byte character.
850 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
851 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700852
Wally Yaua40fdbd2014-08-26 09:47:23 -0700853 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700854 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700855}
856
Elliott Hughes43f7c872016-02-05 11:18:41 -0800857static void* snprintf_small_stack_fn(void*) {
858 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
859 char buf[PATH_MAX];
860 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
861 return nullptr;
862}
863
864TEST(STDIO_TEST, snprintf_small_stack) {
865 // Is it safe to call snprintf on a thread with a small stack?
866 // (The snprintf implementation puts some pretty large buffers on the stack.)
867 pthread_attr_t a;
868 ASSERT_EQ(0, pthread_attr_init(&a));
869 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
870
871 pthread_t t;
872 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
873 ASSERT_EQ(0, pthread_join(t, nullptr));
874}
875
Elliott Hughes8200e552016-02-05 21:57:37 -0800876TEST(STDIO_TEST, snprintf_asterisk_overflow) {
877 char buf[128];
878 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
879 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
880 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
881 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
882 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
883
884 // INT_MAX-1, INT_MAX, INT_MAX+1.
885 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
886 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
887 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
888 ASSERT_EQ(ENOMEM, errno);
889}
890
Elliott Hughes5dc31302020-01-07 08:48:10 -0800891// Inspired by https://github.com/landley/toybox/issues/163.
892TEST(STDIO_TEST, printf_NULL) {
893 char buf[128];
894 char* null = nullptr;
895 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 2, null));
896 EXPECT_STREQ("<(n>", buf);
897 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 8, null));
898 EXPECT_STREQ("<(null)>", buf);
899 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 2, null));
900 EXPECT_STREQ("< (n>", buf);
901 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 8, null));
902 EXPECT_STREQ("< (null)>", buf);
903}
904
Elliott Hughes70715da2016-08-01 16:35:17 -0700905TEST(STDIO_TEST, fprintf) {
906 TemporaryFile tf;
907
908 FILE* tfile = fdopen(tf.fd, "r+");
909 ASSERT_TRUE(tfile != nullptr);
910
911 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
912 AssertFileIs(tfile, "123 abc");
913 fclose(tfile);
914}
915
Christopher Ferris13f26a72016-01-13 13:47:58 -0800916TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700917 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700918 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700919 int fd_rdonly = open("/dev/null", O_RDONLY);
920 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700921
922 // Unbuffered case where the fprintf(3) itself fails.
923 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700924 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700925 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700926 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700927 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700928 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700929
930 // Buffered case where we won't notice until the fclose(3).
931 // It's likely this is what was actually seen in http://b/7229520,
932 // and that expecting fprintf to fail is setting yourself up for
933 // disappointment. Remember to check fclose(3)'s return value, kids!
934 ASSERT_NE(nullptr, fp = tmpfile());
935 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(4, fprintf(fp, "fail"));
938 ASSERT_EQ(-1, fclose(fp));
939}
940
Elliott Hughes468efc82018-07-10 14:39:49 -0700941TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800942 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700943 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800944
945 char buf[16];
946 char* s = fgets(buf, sizeof(buf), fp);
947 buf[13] = '\0';
948 ASSERT_STREQ("Linux version", s);
949
950 ASSERT_EQ(0, pclose(fp));
951}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700952
Elliott Hughes468efc82018-07-10 14:39:49 -0700953TEST(STDIO_TEST, popen_socketpair) {
954 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700955 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700956
957 fputs("hello\nworld\n", fp);
958 fflush(fp);
959
960 char buf[16];
961 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
962 EXPECT_STREQ("hello\n", buf);
963 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
964 EXPECT_STREQ("world\n", buf);
965
966 ASSERT_EQ(0, pclose(fp));
967}
968
969TEST(STDIO_TEST, popen_socketpair_shutdown) {
970 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700971 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700972
973 fputs("a\na\na\na\nb\n", fp);
974 fflush(fp);
975 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
976
977 char buf[16];
978 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
979 EXPECT_STREQ(" 4 a\n", buf);
980 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
981 EXPECT_STREQ(" 1 b\n", buf);
982
983 ASSERT_EQ(0, pclose(fp));
984}
985
986TEST(STDIO_TEST, popen_return_value_0) {
987 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700988 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700989 int status = pclose(fp);
990 EXPECT_TRUE(WIFEXITED(status));
991 EXPECT_EQ(0, WEXITSTATUS(status));
992}
993
994TEST(STDIO_TEST, popen_return_value_1) {
995 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700996 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700997 int status = pclose(fp);
998 EXPECT_TRUE(WIFEXITED(status));
999 EXPECT_EQ(1, WEXITSTATUS(status));
1000}
1001
1002TEST(STDIO_TEST, popen_return_value_signal) {
1003 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001004 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001005 int status = pclose(fp);
1006 EXPECT_TRUE(WIFSIGNALED(status));
1007 EXPECT_EQ(7, WTERMSIG(status));
1008}
1009
Christopher Ferris13f26a72016-01-13 13:47:58 -08001010TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001011 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001012 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001013 ASSERT_EQ('L', getc(fp));
1014 ASSERT_EQ('i', getc(fp));
1015 ASSERT_EQ('n', getc(fp));
1016 ASSERT_EQ('u', getc(fp));
1017 ASSERT_EQ('x', getc(fp));
1018 fclose(fp);
1019}
1020
Christopher Ferris13f26a72016-01-13 13:47:58 -08001021TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001022 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001023 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001024 ASSERT_EQ(EOF, putc('x', fp));
1025 fclose(fp);
1026}
Elliott Hughes603332f2014-03-12 17:10:41 -07001027
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001028TEST(STDIO_TEST, sscanf_swscanf) {
1029 struct stuff {
1030 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001031 int i1, i2;
1032 char cs1[3];
1033 char s2[3];
1034 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001035 double d1;
1036 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001037 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001038
1039 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001040 EXPECT_STREQ("hello", s1);
1041 EXPECT_EQ(123, i1);
1042 EXPECT_EQ(456, i2);
1043 EXPECT_EQ('a', cs1[0]);
1044 EXPECT_EQ('b', cs1[1]);
1045 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
1046 EXPECT_STREQ("AB", s2); // Terminating NUL.
1047 EXPECT_EQ('!', c1);
1048 EXPECT_DOUBLE_EQ(1.23, d1);
1049 EXPECT_FLOAT_EQ(9.0f, f1);
1050 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001051 }
1052 } s;
1053
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001054 memset(&s, 'x', sizeof(s));
1055 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1056 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1057 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 -07001058 s.Check();
1059
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001060 memset(&s, 'x', sizeof(s));
1061 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1062 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1063 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 -07001064 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001065}
Elliott Hughes53b24382014-05-02 18:29:25 -07001066
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001067template <typename T>
1068static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1069 const T* input, const T* fmt,
1070 int expected_count, const char* expected_string) {
1071 char buf[256] = {};
1072 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1073 ASSERT_STREQ(expected_string, buf) << fmt;
1074}
1075
1076TEST(STDIO_TEST, sscanf_ccl) {
1077 // `abc` is just those characters.
1078 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1079 // `a-c` is the range 'a' .. 'c'.
1080 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1081 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1082 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1083 // `a-c-e` is equivalent to `a-e`.
1084 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1085 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1086 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1087 // An initial '^' negates the set.
1088 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1089 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1090 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1091 // The first character may be ']' or '-' without being special.
1092 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1093 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1094 // The last character may be '-' without being special.
1095 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1096 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1097 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1098}
1099
1100TEST(STDIO_TEST, swscanf_ccl) {
1101 // `abc` is just those characters.
1102 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1103 // `a-c` is the range 'a' .. 'c'.
1104 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1105 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1106 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1107 // `a-c-e` is equivalent to `a-e`.
1108 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1109 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1110 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1111 // An initial '^' negates the set.
1112 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1113 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1114 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1115 // The first character may be ']' or '-' without being special.
1116 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1117 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1118 // The last character may be '-' without being special.
1119 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1120 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1121 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1122}
1123
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001124template <typename T1, typename T2>
1125static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1126 const T1* input, const T1* fmt,
1127 int expected_count, const T2* expected_string) {
1128 T2* result = nullptr;
1129 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1130 if (expected_string == nullptr) {
1131 ASSERT_EQ(nullptr, result);
1132 } else {
1133 ASSERT_STREQ(expected_string, result) << fmt;
1134 }
1135 free(result);
1136}
1137
1138TEST(STDIO_TEST, sscanf_mc) {
1139 char* p1 = nullptr;
1140 char* p2 = nullptr;
1141 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1142 ASSERT_EQ('h', *p1);
1143 ASSERT_EQ('e', *p2);
1144 free(p1);
1145 free(p2);
1146
1147 p1 = nullptr;
1148 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1149 ASSERT_EQ('h', p1[0]);
1150 ASSERT_EQ('e', p1[1]);
1151 ASSERT_EQ('l', p1[2]);
1152 ASSERT_EQ('l', p1[3]);
1153 free(p1);
1154
1155 p1 = nullptr;
1156 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1157 ASSERT_EQ('h', p1[0]);
1158 ASSERT_EQ('e', p1[1]);
1159 ASSERT_EQ('l', p1[2]);
1160 ASSERT_EQ('l', p1[3]);
1161 ASSERT_EQ('o', p1[4]);
1162 free(p1);
1163}
1164
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001165TEST(STDIO_TEST, sscanf_mlc) {
1166 // This is so useless that clang doesn't even believe it exists...
1167#pragma clang diagnostic push
1168#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1169#pragma clang diagnostic ignored "-Wformat-extra-args"
1170
1171 wchar_t* p1 = nullptr;
1172 wchar_t* p2 = nullptr;
1173 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1174 ASSERT_EQ(L'h', *p1);
1175 ASSERT_EQ(L'e', *p2);
1176 free(p1);
1177 free(p2);
1178
1179 p1 = nullptr;
1180 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1181 ASSERT_EQ(L'h', p1[0]);
1182 ASSERT_EQ(L'e', p1[1]);
1183 ASSERT_EQ(L'l', p1[2]);
1184 ASSERT_EQ(L'l', p1[3]);
1185 free(p1);
1186
1187 p1 = nullptr;
1188 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1189 ASSERT_EQ(L'h', p1[0]);
1190 ASSERT_EQ(L'e', p1[1]);
1191 ASSERT_EQ(L'l', p1[2]);
1192 ASSERT_EQ(L'l', p1[3]);
1193 ASSERT_EQ(L'o', p1[4]);
1194 free(p1);
1195#pragma clang diagnostic pop
1196}
1197
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001198TEST(STDIO_TEST, sscanf_ms) {
1199 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1200 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1201 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1202}
1203
1204TEST(STDIO_TEST, sscanf_mls) {
1205 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1206 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1207 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1208}
1209
1210TEST(STDIO_TEST, sscanf_m_ccl) {
1211 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1212 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1213 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1214}
1215
1216TEST(STDIO_TEST, sscanf_ml_ccl) {
1217 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1218 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1219 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1220}
1221
1222TEST(STDIO_TEST, sscanf_ls) {
1223 wchar_t w[32] = {};
1224 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1225 ASSERT_EQ(L"hello", std::wstring(w));
1226}
1227
1228TEST(STDIO_TEST, sscanf_ls_suppress) {
1229 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1230}
1231
1232TEST(STDIO_TEST, sscanf_ls_n) {
1233 setlocale(LC_ALL, "C.UTF-8");
1234 wchar_t w[32] = {};
1235 int pos = 0;
1236 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1237 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1238 ASSERT_EQ(2, pos);
1239}
1240
1241TEST(STDIO_TEST, sscanf_ls_realloc) {
1242 // This is so useless that clang doesn't even believe it exists...
1243#pragma clang diagnostic push
1244#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1245#pragma clang diagnostic ignored "-Wformat-extra-args"
1246 wchar_t* p1 = nullptr;
1247 wchar_t* p2 = nullptr;
1248 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1249 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1250 ASSERT_EQ(L"world", std::wstring(p2));
1251#pragma clang diagnostic pop
1252}
1253
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001254// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1255TEST(STDIO_TEST, scanf_wscanf_EOF) {
1256 EXPECT_EQ(0, sscanf("b", "ab"));
1257 EXPECT_EQ(EOF, sscanf("", "a"));
1258 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1259 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1260}
1261
1262TEST(STDIO_TEST, scanf_invalid_UTF8) {
1263#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1264 char buf[BUFSIZ];
1265 wchar_t wbuf[BUFSIZ];
1266
1267 memset(buf, 0, sizeof(buf));
1268 memset(wbuf, 0, sizeof(wbuf));
1269 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1270#endif
1271}
1272
1273TEST(STDIO_TEST, scanf_no_match_no_termination) {
1274 char buf[4] = "x";
1275 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1276 EXPECT_EQ('x', buf[0]);
1277 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1278 EXPECT_EQ('x', buf[0]);
1279
1280 wchar_t wbuf[4] = L"x";
1281 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1282 EXPECT_EQ(L'x', wbuf[0]);
1283
1284 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1285 EXPECT_EQ('x', buf[0]);
1286
1287 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1288 EXPECT_EQ(L'x', wbuf[0]);
1289}
1290
1291TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1292#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1293 wchar_t buf[BUFSIZ];
1294
1295 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1296 memset(buf, 0, sizeof(buf));
1297 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1298 EXPECT_EQ(L"x"s, std::wstring(buf));
1299 memset(buf, 0, sizeof(buf));
1300 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1301 EXPECT_EQ(L"x"s, std::wstring(buf));
1302
1303 // Even if scanf has wide characters in a class, they won't match...
1304 // TODO: is that a bug?
1305 memset(buf, 0, sizeof(buf));
1306 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1307 EXPECT_EQ(L"x"s, std::wstring(buf));
1308 // ...unless you use wscanf.
1309 memset(buf, 0, sizeof(buf));
1310 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1311 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1312
1313 // Negation only covers ASCII for scanf...
1314 memset(buf, 0, sizeof(buf));
1315 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1316 EXPECT_EQ(L"x"s, std::wstring(buf));
1317 // ...but covers wide characters for wscanf.
1318 memset(buf, 0, sizeof(buf));
1319 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1320 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1321
1322 // We already determined that non-ASCII characters are ignored in scanf classes.
1323 memset(buf, 0, sizeof(buf));
1324 EXPECT_EQ(1, sscanf("x"
1325 "\xc4\x80" // Matches a byte from each wide char in the class.
1326 "\xc6\x82" // Neither byte is in the class.
1327 "yz",
1328 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1329 EXPECT_EQ(L"x", std::wstring(buf));
1330 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1331 memset(buf, 0, sizeof(buf));
1332 EXPECT_EQ(1, swscanf(L"x"
1333 L"\xc4\x80"
1334 L"\xc6\x82"
1335 L"yz",
1336 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1337 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1338 // not put back together as a wide character.
1339 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1340#endif
1341}
1342
Christopher Ferris13f26a72016-01-13 13:47:58 -08001343TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001344 // If we open a file read-only...
1345 FILE* fp = fopen("/proc/version", "r");
1346
1347 // ...all attempts to write to that file should return failure.
1348
1349 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1350 // glibc gets the wide-character functions wrong.
1351
1352 errno = 0;
1353 EXPECT_EQ(EOF, putc('x', fp));
1354 EXPECT_EQ(EBADF, errno);
1355
1356 errno = 0;
1357 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1358 EXPECT_EQ(EBADF, errno);
1359
1360 errno = 0;
1361 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001362#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001363 EXPECT_EQ(EBADF, errno);
1364#endif
1365
1366 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001367 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1368 EXPECT_EQ(EBADF, errno);
1369
1370 errno = 0;
1371 EXPECT_EQ(EOF, fputs("hello", fp));
1372 EXPECT_EQ(EBADF, errno);
1373
1374 errno = 0;
1375 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001376#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001377 EXPECT_EQ(EBADF, errno);
1378#endif
1379}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001380
1381// Tests that we can only have a consistent and correct fpos_t when using
1382// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001383TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001384 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1385 uselocale(LC_GLOBAL_LOCALE);
1386
1387 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001388 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001389
1390 wchar_t mb_one_bytes = L'h';
1391 wchar_t mb_two_bytes = 0x00a2;
1392 wchar_t mb_three_bytes = 0x20ac;
1393 wchar_t mb_four_bytes = 0x24b62;
1394
1395 // Write to file.
1396 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1397 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1398 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1399 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1400
1401 rewind(fp);
1402
1403 // Record each character position.
1404 fpos_t pos1;
1405 fpos_t pos2;
1406 fpos_t pos3;
1407 fpos_t pos4;
1408 fpos_t pos5;
1409 EXPECT_EQ(0, fgetpos(fp, &pos1));
1410 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1411 EXPECT_EQ(0, fgetpos(fp, &pos2));
1412 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1413 EXPECT_EQ(0, fgetpos(fp, &pos3));
1414 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1415 EXPECT_EQ(0, fgetpos(fp, &pos4));
1416 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1417 EXPECT_EQ(0, fgetpos(fp, &pos5));
1418
Elliott Hughes063525c2014-05-13 11:19:57 -07001419#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001420 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1421 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1422 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1423 // structure.
1424 ASSERT_EQ(0, static_cast<off_t>(pos1));
1425 ASSERT_EQ(1, static_cast<off_t>(pos2));
1426 ASSERT_EQ(3, static_cast<off_t>(pos3));
1427 ASSERT_EQ(6, static_cast<off_t>(pos4));
1428 ASSERT_EQ(10, static_cast<off_t>(pos5));
1429#endif
1430
1431 // Exercise back and forth movements of the position.
1432 ASSERT_EQ(0, fsetpos(fp, &pos2));
1433 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1434 ASSERT_EQ(0, fsetpos(fp, &pos1));
1435 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1436 ASSERT_EQ(0, fsetpos(fp, &pos4));
1437 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1438 ASSERT_EQ(0, fsetpos(fp, &pos3));
1439 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1440 ASSERT_EQ(0, fsetpos(fp, &pos5));
1441 ASSERT_EQ(WEOF, fgetwc(fp));
1442
1443 fclose(fp);
1444}
1445
1446// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001447TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001448 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1449 uselocale(LC_GLOBAL_LOCALE);
1450
Calin Juravle9b95ea92014-05-14 17:07:10 +01001451 // In glibc-2.16 fseek doesn't work properly in wide mode
1452 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1453 // to close and re-open the file. We do it in order to make the test pass
1454 // with all glibcs.
1455
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001456 TemporaryFile tf;
1457 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001458 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001459
1460 wchar_t mb_two_bytes = 0x00a2;
1461 wchar_t mb_three_bytes = 0x20ac;
1462 wchar_t mb_four_bytes = 0x24b62;
1463
1464 // Write to file.
1465 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1466 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1467 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1468
1469 fflush(fp);
1470 fclose(fp);
1471
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001472 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001473 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001474
1475 // Store a valid position.
1476 fpos_t mb_two_bytes_pos;
1477 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1478
1479 // Move inside mb_four_bytes with fseek.
1480 long offset_inside_mb = 6;
1481 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1482
1483 // Store the "inside multi byte" position.
1484 fpos_t pos_inside_mb;
1485 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001486#if defined(__BIONIC__)
1487 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1488#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001489
1490 // Reading from within a byte should produce an error.
1491 ASSERT_EQ(WEOF, fgetwc(fp));
1492 ASSERT_EQ(EILSEQ, errno);
1493
1494 // Reverting to a valid position should work.
1495 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1496 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1497
1498 // Moving withing a multi byte with fsetpos should work but reading should
1499 // produce an error.
1500 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1501 ASSERT_EQ(WEOF, fgetwc(fp));
1502 ASSERT_EQ(EILSEQ, errno);
1503
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001504 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001505}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001506
Christopher Ferris13f26a72016-01-13 13:47:58 -08001507TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001508 char buf[16];
1509 memset(buf, 0, sizeof(buf));
1510 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1511 ASSERT_EQ('<', fputc('<', fp));
1512 ASSERT_NE(EOF, fputs("abc>\n", fp));
1513 fflush(fp);
1514
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001515 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001516 ASSERT_STREQ("<abc>\n", buf);
1517
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001518 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001519 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001520 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001521}
1522
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001523TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001524 FILE* fp = fmemopen(nullptr, 128, "r+");
1525 ASSERT_NE(EOF, fputs("xyz\n", fp));
1526
Elliott Hughes70715da2016-08-01 16:35:17 -07001527 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001528 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001529}
1530
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001531TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1532 FILE* fp;
1533 char buf[8];
1534
1535 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1536 // shall be written at the current position or at the end of the buffer,
1537 // depending on the size of the contents."
1538 memset(buf, 'x', sizeof(buf));
1539 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1540 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1541 ASSERT_EQ(0, fflush(fp));
1542 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1543 // Now write and check that the NUL moves along with our writes...
1544 ASSERT_NE(EOF, fputs("hello", fp));
1545 ASSERT_EQ(0, fflush(fp));
1546 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1547 ASSERT_NE(EOF, fputs("wo", fp));
1548 ASSERT_EQ(0, fflush(fp));
1549 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1550 ASSERT_EQ(0, fclose(fp));
1551
1552 // "If a stream open for update is flushed or closed and the last write has
1553 // advanced the current buffer size, a null byte shall be written at the end
1554 // of the buffer if it fits."
1555 memset(buf, 'x', sizeof(buf));
1556 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1557 // Nothing written yet, so no advance...
1558 ASSERT_EQ(0, fflush(fp));
1559 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1560 ASSERT_NE(EOF, fputs("hello", fp));
1561 ASSERT_EQ(0, fclose(fp));
1562}
1563
1564TEST(STDIO_TEST, fmemopen_size) {
1565 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001566 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001567 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001568
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001569 // POSIX: "The stream shall also maintain the size of the current buffer
1570 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1571 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001572
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001573 // "For modes r and r+ the size shall be set to the value given by the size
1574 // argument."
1575 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1576 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1577 EXPECT_EQ(16, ftell(fp));
1578 EXPECT_EQ(16, ftello(fp));
1579 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1580 EXPECT_EQ(16, ftell(fp));
1581 EXPECT_EQ(16, ftello(fp));
1582 ASSERT_EQ(0, fclose(fp));
1583 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1584 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1585 EXPECT_EQ(16, ftell(fp));
1586 EXPECT_EQ(16, ftello(fp));
1587 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1588 EXPECT_EQ(16, ftell(fp));
1589 EXPECT_EQ(16, ftello(fp));
1590 ASSERT_EQ(0, fclose(fp));
1591
1592 // "For modes w and w+ the initial size shall be zero..."
1593 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1594 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1595 EXPECT_EQ(0, ftell(fp));
1596 EXPECT_EQ(0, ftello(fp));
1597 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1598 EXPECT_EQ(0, ftell(fp));
1599 EXPECT_EQ(0, ftello(fp));
1600 ASSERT_EQ(0, fclose(fp));
1601 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1602 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1603 EXPECT_EQ(0, ftell(fp));
1604 EXPECT_EQ(0, ftello(fp));
1605 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1606 EXPECT_EQ(0, ftell(fp));
1607 EXPECT_EQ(0, ftello(fp));
1608 ASSERT_EQ(0, fclose(fp));
1609
1610 // "...and for modes a and a+ the initial size shall be:
1611 // 1. Zero, if buf is a null pointer
1612 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1613 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1614 EXPECT_EQ(0, ftell(fp));
1615 EXPECT_EQ(0, ftello(fp));
1616 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1617 EXPECT_EQ(0, ftell(fp));
1618 EXPECT_EQ(0, ftello(fp));
1619 ASSERT_EQ(0, fclose(fp));
1620 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1621 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1622 EXPECT_EQ(0, ftell(fp));
1623 EXPECT_EQ(0, ftello(fp));
1624 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1625 EXPECT_EQ(0, ftell(fp));
1626 EXPECT_EQ(0, ftello(fp));
1627 ASSERT_EQ(0, fclose(fp));
1628
1629 // 2. The position of the first null byte in the buffer, if one is found
1630 memset(buf, 'x', sizeof(buf));
1631 buf[3] = '\0';
1632 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1633 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1634 EXPECT_EQ(3, ftell(fp));
1635 EXPECT_EQ(3, ftello(fp));
1636 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1637 EXPECT_EQ(3, ftell(fp));
1638 EXPECT_EQ(3, ftello(fp));
1639 ASSERT_EQ(0, fclose(fp));
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
1651 // 3. The value of the size argument, if buf is not a null pointer and no
1652 // null byte is found.
1653 memset(buf, 'x', sizeof(buf));
1654 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1655 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1656 EXPECT_EQ(16, ftell(fp));
1657 EXPECT_EQ(16, ftello(fp));
1658 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1659 EXPECT_EQ(16, ftell(fp));
1660 EXPECT_EQ(16, ftello(fp));
1661 ASSERT_EQ(0, fclose(fp));
1662 memset(buf, 'x', sizeof(buf));
1663 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1664 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1665 EXPECT_EQ(16, ftell(fp));
1666 EXPECT_EQ(16, ftello(fp));
1667 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1668 EXPECT_EQ(16, ftell(fp));
1669 EXPECT_EQ(16, ftello(fp));
1670 ASSERT_EQ(0, fclose(fp));
1671}
1672
1673TEST(STDIO_TEST, fmemopen_SEEK_END) {
1674 // fseek SEEK_END is relative to the current string length, not the buffer size.
1675 FILE* fp;
1676 char buf[8];
1677 memset(buf, 'x', sizeof(buf));
1678 strcpy(buf, "str");
1679 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1680 ASSERT_NE(EOF, fputs("string", fp));
1681 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1682 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1683 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1684 EXPECT_EQ(0, fclose(fp));
1685
1686 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1687 // than adding).
1688 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1689 ASSERT_NE(EOF, fputs("54321", fp));
1690 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1691 EXPECT_EQ('2', fgetc(fp));
1692 EXPECT_EQ(0, fclose(fp));
1693}
1694
1695TEST(STDIO_TEST, fmemopen_seek_invalid) {
1696 char buf[8];
1697 memset(buf, 'x', sizeof(buf));
1698 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1699 ASSERT_TRUE(fp != nullptr);
1700
1701 // POSIX: "An attempt to seek ... to a negative position or to a position
1702 // larger than the buffer size given in the size argument shall fail."
1703 // (There's no mention of what errno should be set to, and glibc doesn't
1704 // set errno in any of these cases.)
1705 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1706 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1707 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1708 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1709}
1710
1711TEST(STDIO_TEST, fmemopen_read_EOF) {
1712 // POSIX: "A read operation on the stream shall not advance the current
1713 // buffer position beyond the current buffer size."
1714 char buf[8];
1715 memset(buf, 'x', sizeof(buf));
1716 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1717 ASSERT_TRUE(fp != nullptr);
1718 char buf2[BUFSIZ];
1719 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1720 // POSIX: "Reaching the buffer size in a read operation shall count as
1721 // end-of-file.
1722 ASSERT_TRUE(feof(fp));
1723 ASSERT_EQ(EOF, fgetc(fp));
1724 ASSERT_EQ(0, fclose(fp));
1725}
1726
1727TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1728 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1729 char buf[] = "h\0e\0l\0l\0o";
1730 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1731 ASSERT_TRUE(fp != nullptr);
1732 ASSERT_EQ('h', fgetc(fp));
1733 ASSERT_EQ(0, fgetc(fp));
1734 ASSERT_EQ('e', fgetc(fp));
1735 ASSERT_EQ(0, fgetc(fp));
1736 ASSERT_EQ('l', fgetc(fp));
1737 ASSERT_EQ(0, fgetc(fp));
1738 // POSIX: "The read operation shall start at the current buffer position of
1739 // the stream."
1740 char buf2[8];
1741 memset(buf2, 'x', sizeof(buf2));
1742 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1743 ASSERT_EQ('l', buf2[0]);
1744 ASSERT_EQ(0, buf2[1]);
1745 ASSERT_EQ('o', buf2[2]);
1746 ASSERT_EQ(0, buf2[3]);
1747 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1748 ASSERT_TRUE(feof(fp));
1749 ASSERT_EQ(0, fclose(fp));
1750}
1751
1752TEST(STDIO_TEST, fmemopen_write) {
1753 FILE* fp;
1754 char buf[8];
1755
1756 // POSIX: "A write operation shall start either at the current position of
1757 // the stream (if mode has not specified 'a' as the first character)..."
1758 memset(buf, 'x', sizeof(buf));
1759 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1760 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1761 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1762 ASSERT_EQ(' ', fputc(' ', fp));
1763 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1764 ASSERT_EQ(0, fclose(fp));
1765
1766 // "...or at the current size of the stream (if mode had 'a' as the first
1767 // character)." (See the fmemopen_size test for what "size" means, but for
1768 // mode "a", it's the first NUL byte.)
1769 memset(buf, 'x', sizeof(buf));
1770 buf[3] = '\0';
1771 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1772 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1773 ASSERT_EQ(' ', fputc(' ', fp));
1774 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1775 ASSERT_EQ(0, fclose(fp));
1776
1777 // "If the current position at the end of the write is larger than the
1778 // current buffer size, the current buffer size shall be set to the current
1779 // position." (See the fmemopen_size test for what "size" means, but to
1780 // query it we SEEK_END with offset 0, and then ftell.)
1781 memset(buf, 'x', sizeof(buf));
1782 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1783 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1784 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1785 EXPECT_EQ(0, ftell(fp));
1786 ASSERT_EQ(' ', fputc(' ', fp));
1787 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1788 EXPECT_EQ(1, ftell(fp));
1789 ASSERT_NE(EOF, fputs("123", fp));
1790 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1791 EXPECT_EQ(4, ftell(fp));
1792 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1793 ASSERT_EQ(0, fclose(fp));
1794}
1795
1796TEST(STDIO_TEST, fmemopen_write_EOF) {
1797 // POSIX: "A write operation on the stream shall not advance the current
1798 // buffer size beyond the size given in the size argument."
1799 FILE* fp;
1800
1801 // Scalar writes...
1802 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1803 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1804 ASSERT_EQ('x', fputc('x', fp));
1805 ASSERT_EQ('x', fputc('x', fp));
1806 ASSERT_EQ('x', fputc('x', fp));
1807 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1808 ASSERT_EQ(0, fclose(fp));
1809
1810 // Vector writes...
1811 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1812 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1813 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1814 ASSERT_EQ(0, fclose(fp));
1815}
1816
1817TEST(STDIO_TEST, fmemopen_initial_position) {
1818 // POSIX: "The ... current position in the buffer ... shall be initially
1819 // set to either the beginning of the buffer (for r and w modes) ..."
1820 char buf[] = "hello\0world";
1821 FILE* fp;
1822 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1823 EXPECT_EQ(0L, ftell(fp));
1824 EXPECT_EQ(0, fclose(fp));
1825 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1826 EXPECT_EQ(0L, ftell(fp));
1827 EXPECT_EQ(0, fclose(fp));
1828 buf[0] = 'h'; // (Undo the effects of the above.)
1829
1830 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1831 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1832 EXPECT_EQ(5L, ftell(fp));
1833 EXPECT_EQ(0, fclose(fp));
1834
1835 // POSIX: "If no null byte is found in append mode, the initial position
1836 // shall be set to one byte after the end of the buffer."
1837 memset(buf, 'x', sizeof(buf));
1838 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1839 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1840 EXPECT_EQ(0, fclose(fp));
1841}
1842
1843TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1844 // POSIX: "If buf is a null pointer, the initial position shall always be
1845 // set to the beginning of the buffer."
1846 FILE* fp = fmemopen(nullptr, 128, "a+");
1847 ASSERT_TRUE(fp != nullptr);
1848 EXPECT_EQ(0L, ftell(fp));
1849 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1850 EXPECT_EQ(0, fclose(fp));
1851}
1852
1853TEST(STDIO_TEST, fmemopen_zero_length) {
1854 // POSIX says it's up to the implementation whether or not you can have a
1855 // zero-length buffer (but "A future version of this standard may require
1856 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1857 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1858 FILE* fp;
1859 char buf[16];
1860 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1861 ASSERT_EQ(EOF, fgetc(fp));
1862 ASSERT_TRUE(feof(fp));
1863 ASSERT_EQ(0, fclose(fp));
1864 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1865 ASSERT_EQ(EOF, fgetc(fp));
1866 ASSERT_TRUE(feof(fp));
1867 ASSERT_EQ(0, fclose(fp));
1868
1869 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1870 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1871 ASSERT_EQ(EOF, fputc('x', fp));
1872 ASSERT_EQ(0, fclose(fp));
1873 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1874 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1875 ASSERT_EQ(EOF, fputc('x', fp));
1876 ASSERT_EQ(0, fclose(fp));
1877}
1878
Elliott Hughes288465d2019-02-05 15:00:13 -08001879TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1880 char buf[2] = "x";
1881 ASSERT_EQ('x', buf[0]);
1882 FILE* fp = fmemopen(buf, 0, "w");
1883 ASSERT_EQ('x', buf[0]);
1884 ASSERT_EQ(0, fclose(fp));
1885}
1886
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001887TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1888 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1889 // BSD fails, glibc doesn't. We side with the more lenient.
1890 FILE* fp;
1891 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1892 ASSERT_EQ(0, fclose(fp));
1893 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1894 ASSERT_EQ(0, fclose(fp));
1895}
1896
1897TEST(STDIO_TEST, fmemopen_fileno) {
1898 // There's no fd backing an fmemopen FILE*.
1899 FILE* fp = fmemopen(nullptr, 16, "r");
1900 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001901 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001902 ASSERT_EQ(-1, fileno(fp));
1903 ASSERT_EQ(EBADF, errno);
1904 ASSERT_EQ(0, fclose(fp));
1905}
1906
1907TEST(STDIO_TEST, fmemopen_append_after_seek) {
1908 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1909 // there had been an intervening seek.
1910
1911 FILE* fp;
1912 char buf[] = "hello\0world";
1913 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1914 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1915 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1916 ASSERT_NE(EOF, fputc('!', fp));
1917 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1918 ASSERT_EQ(0, fclose(fp));
1919
1920 memcpy(buf, "hello\0world", sizeof(buf));
1921 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1922 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1923 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1924 ASSERT_NE(EOF, fputc('!', fp));
1925 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1926 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001927}
1928
Christopher Ferris13f26a72016-01-13 13:47:58 -08001929TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001930 char* p = nullptr;
1931 size_t size = 0;
1932 FILE* fp = open_memstream(&p, &size);
1933 ASSERT_NE(EOF, fputs("hello, world!", fp));
1934 fclose(fp);
1935
1936 ASSERT_STREQ("hello, world!", p);
1937 ASSERT_EQ(strlen("hello, world!"), size);
1938 free(p);
1939}
1940
Christopher Ferris13f26a72016-01-13 13:47:58 -08001941TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001942#if defined(__BIONIC__)
1943 char* p;
1944 size_t size;
1945
1946 // Invalid buffer.
1947 errno = 0;
1948 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1949 ASSERT_EQ(EINVAL, errno);
1950
1951 // Invalid size.
1952 errno = 0;
1953 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1954 ASSERT_EQ(EINVAL, errno);
1955#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001956 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001957#endif
1958}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001959
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001960TEST(STDIO_TEST, fdopen_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001961 // This fd doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001962 int fd = open("/proc/version", O_RDONLY);
1963 ASSERT_FALSE(CloseOnExec(fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001964 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001965 FILE* fp = fdopen(fd, "re");
1966 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1967 fclose(fp);
1968}
1969
1970TEST(STDIO_TEST, fdopen_remove_CLOEXEC) {
1971 // This fd has O_CLOEXEC...
1972 int fd = open("/proc/version", O_RDONLY | O_CLOEXEC);
1973 ASSERT_TRUE(CloseOnExec(fd));
1974 // ...but the new one doesn't.
1975 FILE* fp = fdopen(fd, "r");
1976 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1977 fclose(fp);
1978}
1979
1980TEST(STDIO_TEST, freopen_add_CLOEXEC) {
1981 // This FILE* doesn't have O_CLOEXEC...
1982 FILE* fp = fopen("/proc/version", "r");
1983 ASSERT_FALSE(CloseOnExec(fileno(fp)));
1984 // ...but the new one does.
1985 fp = freopen("/proc/version", "re", fp);
1986 ASSERT_TRUE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001987
1988 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001989}
1990
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001991TEST(STDIO_TEST, freopen_remove_CLOEXEC) {
1992 // This FILE* has O_CLOEXEC...
1993 FILE* fp = fopen("/proc/version", "re");
1994 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1995 // ...but the new one doesn't.
1996 fp = freopen("/proc/version", "r", fp);
1997 ASSERT_FALSE(CloseOnExec(fileno(fp)));
1998 fclose(fp);
1999}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002000
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002001TEST(STDIO_TEST, freopen_null_filename_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07002002 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002003 FILE* fp = fopen("/proc/version", "r");
2004 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002005 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002006 fp = freopen(nullptr, "re", fp);
2007 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2008 fclose(fp);
2009}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002010
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002011TEST(STDIO_TEST, freopen_null_filename_remove_CLOEXEC) {
2012 // This FILE* has O_CLOEXEC...
2013 FILE* fp = fopen("/proc/version", "re");
2014 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2015 // ...but the new one doesn't.
2016 fp = freopen(nullptr, "r", fp);
2017 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002018 fclose(fp);
2019}
Elliott Hughes20841a12014-12-01 16:13:30 -08002020
Elliott Hughesf226ee52016-02-03 11:24:28 -08002021TEST(STDIO_TEST, fopen64_freopen64) {
2022 FILE* fp = fopen64("/proc/version", "r");
2023 ASSERT_TRUE(fp != nullptr);
2024 fp = freopen64("/proc/version", "re", fp);
2025 ASSERT_TRUE(fp != nullptr);
2026 fclose(fp);
2027}
2028
Elliott Hughes20841a12014-12-01 16:13:30 -08002029// https://code.google.com/p/android/issues/detail?id=81155
2030// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08002031TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08002032 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07002033 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002034
2035 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07002036 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08002037
2038 char buf[65*1024];
2039 memset(buf, 0xff, sizeof(buf));
2040
Yi Kong32bc0fc2018-08-02 17:31:13 -07002041 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002042 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002043 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08002044 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07002045 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002046
2047 fclose(fp);
2048
2049 // 1024 64KiB reads should have been very quick.
2050 ASSERT_LE(t1 - t0, 1);
2051
2052 for (size_t i = 0; i < 64*1024; ++i) {
2053 ASSERT_EQ('\0', buf[i]);
2054 }
2055 for (size_t i = 64*1024; i < 65*1024; ++i) {
2056 ASSERT_EQ('\xff', buf[i]);
2057 }
2058}
Elliott Hughes75b99382015-01-20 11:23:50 -08002059
Christopher Ferris13f26a72016-01-13 13:47:58 -08002060TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002061 std::string digits("0123456789");
2062 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08002063
2064 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
2065 char buf1[4 * 4];
2066 memset(buf1, 0, sizeof(buf1));
2067 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002068 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08002069 ASSERT_TRUE(feof(fp));
2070
2071 rewind(fp);
2072
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002073 // Try to read way too much so stdio tries to read more direct from the stream.
2074 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08002075 memset(buf2, 0, sizeof(buf2));
2076 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002077 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002078 ASSERT_TRUE(feof(fp));
2079
2080 fclose(fp);
2081}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002082
2083static void test_fread_from_write_only_stream(size_t n) {
2084 FILE* fp = fopen("/dev/null", "w");
2085 std::vector<char> buf(n, 0);
2086 errno = 0;
2087 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2088 ASSERT_EQ(EBADF, errno);
2089 ASSERT_TRUE(ferror(fp));
2090 ASSERT_FALSE(feof(fp));
2091 fclose(fp);
2092}
2093
Christopher Ferris13f26a72016-01-13 13:47:58 -08002094TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002095 test_fread_from_write_only_stream(1);
2096}
2097
Christopher Ferris13f26a72016-01-13 13:47:58 -08002098TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002099 test_fread_from_write_only_stream(64*1024);
2100}
2101
2102static void test_fwrite_after_fread(size_t n) {
2103 TemporaryFile tf;
2104
2105 FILE* fp = fdopen(tf.fd, "w+");
2106 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2107 fflush(fp);
2108
2109 // We've flushed but not rewound, so there's nothing to read.
2110 std::vector<char> buf(n, 0);
2111 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2112 ASSERT_TRUE(feof(fp));
2113
2114 // But hitting EOF doesn't prevent us from writing...
2115 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002116 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002117
2118 // And if we rewind, everything's there.
2119 rewind(fp);
2120 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2121 ASSERT_EQ('1', buf[0]);
2122 ASSERT_EQ('2', buf[1]);
2123
2124 fclose(fp);
2125}
2126
Christopher Ferris13f26a72016-01-13 13:47:58 -08002127TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002128 test_fwrite_after_fread(16);
2129}
2130
Christopher Ferris13f26a72016-01-13 13:47:58 -08002131TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002132 test_fwrite_after_fread(64*1024);
2133}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002134
2135// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002136TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002137 TemporaryFile tf;
2138
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002139 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002140 ASSERT_TRUE(fp != nullptr);
2141
2142 char file_data[12288];
2143 for (size_t i = 0; i < 12288; i++) {
2144 file_data[i] = i;
2145 }
2146 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2147 fclose(fp);
2148
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002149 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002150 ASSERT_TRUE(fp != nullptr);
2151
2152 char buffer[8192];
2153 size_t cur_location = 0;
2154 // Small read to populate internal buffer.
2155 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2156 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2157
2158 cur_location = static_cast<size_t>(ftell(fp));
2159 // Large read to force reading into the user supplied buffer and bypassing
2160 // the internal buffer.
2161 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2162 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2163
2164 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002165 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002166 cur_location = static_cast<size_t>(ftell(fp));
2167 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2168 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2169
2170 fclose(fp);
2171}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002172
2173// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002174TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002175 TemporaryFile tf;
2176 char buf[6] = {0};
2177
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002178 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002179 ASSERT_TRUE(fw != nullptr);
2180
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002181 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002182 ASSERT_TRUE(fr != nullptr);
2183
2184 fwrite("a", 1, 1, fw);
2185 fflush(fw);
2186 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2187 ASSERT_STREQ("a", buf);
2188
2189 // 'fr' is now at EOF.
2190 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2191 ASSERT_TRUE(feof(fr));
2192
2193 // Write some more...
2194 fwrite("z", 1, 1, fw);
2195 fflush(fw);
2196
2197 // ...and check that we can read it back.
2198 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2199 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2200 ASSERT_STREQ("z", buf);
2201
2202 // But now we're done.
2203 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2204
2205 fclose(fr);
2206 fclose(fw);
2207}
Elliott Hughes923f1652016-01-19 15:46:05 -08002208
2209TEST(STDIO_TEST, fclose_invalidates_fd) {
2210 // The typical error we're trying to help people catch involves accessing
2211 // memory after it's been freed. But we know that stdin/stdout/stderr are
2212 // special and don't get deallocated, so this test uses stdin.
2213 ASSERT_EQ(0, fclose(stdin));
2214
2215 // Even though using a FILE* after close is undefined behavior, I've closed
2216 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2217 // especially because they might actually correspond to a real stream.
2218 errno = 0;
2219 ASSERT_EQ(-1, fileno(stdin));
2220 ASSERT_EQ(EBADF, errno);
2221}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002222
2223TEST(STDIO_TEST, fseek_ftell_unseekable) {
2224#if defined(__BIONIC__) // glibc has fopencookie instead.
2225 auto read_fn = [](void*, char*, int) { return -1; };
2226 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2227 ASSERT_TRUE(fp != nullptr);
2228
2229 // Check that ftell balks on an unseekable FILE*.
2230 errno = 0;
2231 ASSERT_EQ(-1, ftell(fp));
2232 ASSERT_EQ(ESPIPE, errno);
2233
2234 // SEEK_CUR is rewritten as SEEK_SET internally...
2235 errno = 0;
2236 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2237 ASSERT_EQ(ESPIPE, errno);
2238
2239 // ...so it's worth testing the direct seek path too.
2240 errno = 0;
2241 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2242 ASSERT_EQ(ESPIPE, errno);
2243
2244 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002245#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002246 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002247#endif
2248}
2249
2250TEST(STDIO_TEST, funopen_EINVAL) {
2251#if defined(__BIONIC__)
2252 errno = 0;
2253 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2254 ASSERT_EQ(EINVAL, errno);
2255#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002256 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002257#endif
2258}
2259
2260TEST(STDIO_TEST, funopen_seek) {
2261#if defined(__BIONIC__)
2262 auto read_fn = [](void*, char*, int) { return -1; };
2263
2264 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2265 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2266
2267 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2268 ASSERT_TRUE(fp != nullptr);
2269 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002270#if defined(__LP64__)
2271 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2272 EXPECT_EQ(0xfedcba12LL, pos);
2273#else
2274 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2275 EXPECT_EQ(EOVERFLOW, errno);
2276#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002277
2278 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2279 ASSERT_TRUE(fp64 != nullptr);
2280 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002281 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2282 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002283#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002284 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002285#endif
2286}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002287
2288TEST(STDIO_TEST, lots_of_concurrent_files) {
2289 std::vector<TemporaryFile*> tfs;
2290 std::vector<FILE*> fps;
2291
2292 for (size_t i = 0; i < 256; ++i) {
2293 TemporaryFile* tf = new TemporaryFile;
2294 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002295 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002296 fps.push_back(fp);
2297 fprintf(fp, "hello %zu!\n", i);
2298 fflush(fp);
2299 }
2300
2301 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002302 char expected[BUFSIZ];
2303 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002304
Elliott Hughes70715da2016-08-01 16:35:17 -07002305 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002306 fclose(fps[i]);
2307 delete tfs[i];
2308 }
2309}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002310
2311static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2312 EXPECT_EQ(offset, ftell(fp));
2313 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002314 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002315 fpos_t pos;
2316 fpos64_t pos64;
2317 EXPECT_EQ(0, fgetpos(fp, &pos));
2318 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2319#if defined(__BIONIC__)
2320 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2321 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2322#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002323 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002324#endif
2325}
2326
2327TEST(STDIO_TEST, seek_tell_family_smoke) {
2328 TemporaryFile tf;
2329 FILE* fp = fdopen(tf.fd, "w+");
2330
2331 // Initially we should be at 0.
2332 AssertFileOffsetAt(fp, 0);
2333
2334 // Seek to offset 8192.
2335 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2336 AssertFileOffsetAt(fp, 8192);
2337 fpos_t eight_k_pos;
2338 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2339
2340 // Seek forward another 8192...
2341 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2342 AssertFileOffsetAt(fp, 8192 + 8192);
2343 fpos64_t sixteen_k_pos64;
2344 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2345
2346 // Seek back 8192...
2347 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2348 AssertFileOffsetAt(fp, 8192);
2349
2350 // Since we haven't written anything, the end is also at 0.
2351 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2352 AssertFileOffsetAt(fp, 0);
2353
2354 // Check that our fpos64_t from 16KiB works...
2355 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2356 AssertFileOffsetAt(fp, 8192 + 8192);
2357 // ...as does our fpos_t from 8192.
2358 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2359 AssertFileOffsetAt(fp, 8192);
2360
2361 // Do fseeko and fseeko64 work too?
2362 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2363 AssertFileOffsetAt(fp, 1234);
2364 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2365 AssertFileOffsetAt(fp, 5678);
2366
2367 fclose(fp);
2368}
2369
2370TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2371 TemporaryFile tf;
2372 FILE* fp = fdopen(tf.fd, "w+");
2373
2374 // Bad whence.
2375 errno = 0;
2376 ASSERT_EQ(-1, fseek(fp, 0, 123));
2377 ASSERT_EQ(EINVAL, errno);
2378 errno = 0;
2379 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2380 ASSERT_EQ(EINVAL, errno);
2381 errno = 0;
2382 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2383 ASSERT_EQ(EINVAL, errno);
2384
2385 // Bad offset.
2386 errno = 0;
2387 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2388 ASSERT_EQ(EINVAL, errno);
2389 errno = 0;
2390 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2391 ASSERT_EQ(EINVAL, errno);
2392 errno = 0;
2393 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2394 ASSERT_EQ(EINVAL, errno);
2395
2396 fclose(fp);
2397}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002398
2399TEST(STDIO_TEST, ctermid) {
2400 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2401
2402 char buf[L_ctermid] = {};
2403 ASSERT_EQ(buf, ctermid(buf));
2404 ASSERT_STREQ("/dev/tty", buf);
2405}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002406
2407TEST(STDIO_TEST, remove) {
2408 struct stat sb;
2409
2410 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002411 ASSERT_EQ(0, remove(tf.path));
2412 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002413 ASSERT_EQ(ENOENT, errno);
2414
2415 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002416 ASSERT_EQ(0, remove(td.path));
2417 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002418 ASSERT_EQ(ENOENT, errno);
2419
2420 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002421 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002422 ASSERT_EQ(ENOENT, errno);
2423
2424 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002425 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002426 ASSERT_EQ(ENOENT, errno);
2427}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002428
Elliott Hughese657eb42021-02-18 17:11:56 -08002429TEST_F(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002430 char buf[16];
2431 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2432 testing::KilledBySignal(SIGABRT),
2433#if defined(NOFORTIFY)
2434 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2435#else
2436 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2437#endif
2438 );
2439}
2440
Elliott Hughese657eb42021-02-18 17:11:56 -08002441TEST_F(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002442 std::string buf = "world";
2443 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2444 testing::KilledBySignal(SIGABRT),
2445 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2446}
2447
2448TEST(STDIO_TEST, sprintf_30445072) {
2449 std::string buf = "world";
2450 sprintf(&buf[0], "hello");
2451 ASSERT_EQ(buf, "hello");
2452}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002453
Elliott Hughes654cd832018-08-30 16:00:42 -07002454TEST(STDIO_TEST, printf_m) {
2455 char buf[BUFSIZ];
2456 errno = 0;
2457 snprintf(buf, sizeof(buf), "<%m>");
2458 ASSERT_STREQ("<Success>", buf);
2459 errno = -1;
2460 snprintf(buf, sizeof(buf), "<%m>");
2461 ASSERT_STREQ("<Unknown error -1>", buf);
2462 errno = EINVAL;
2463 snprintf(buf, sizeof(buf), "<%m>");
2464 ASSERT_STREQ("<Invalid argument>", buf);
2465}
2466
Elliott Hughesf340a562018-09-06 10:42:40 -07002467TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2468 char buf[BUFSIZ];
2469 const char* m = strerror(-1);
2470 ASSERT_STREQ("Unknown error -1", m);
2471 errno = -2;
2472 snprintf(buf, sizeof(buf), "<%m>");
2473 ASSERT_STREQ("<Unknown error -2>", buf);
2474 ASSERT_STREQ("Unknown error -1", m);
2475}
2476
Elliott Hughes654cd832018-08-30 16:00:42 -07002477TEST(STDIO_TEST, wprintf_m) {
2478 wchar_t buf[BUFSIZ];
2479 errno = 0;
2480 swprintf(buf, sizeof(buf), L"<%m>");
2481 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2482 errno = -1;
2483 swprintf(buf, sizeof(buf), L"<%m>");
2484 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2485 errno = EINVAL;
2486 swprintf(buf, sizeof(buf), L"<%m>");
2487 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2488}
2489
Elliott Hughesf340a562018-09-06 10:42:40 -07002490TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2491 wchar_t buf[BUFSIZ];
2492 const char* m = strerror(-1);
2493 ASSERT_STREQ("Unknown error -1", m);
2494 errno = -2;
2495 swprintf(buf, sizeof(buf), L"<%m>");
2496 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2497 ASSERT_STREQ("Unknown error -1", m);
2498}
2499
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002500TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2501 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002502 SetFileTo(tf.path, "0123456789");
2503 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002504 EXPECT_EQ(10, ftell(fp));
2505 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2506 EXPECT_EQ(2, ftell(fp));
2507 ASSERT_NE(EOF, fputs("xxx", fp));
2508 ASSERT_EQ(0, fflush(fp));
2509 EXPECT_EQ(13, ftell(fp));
2510 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2511 EXPECT_EQ(13, ftell(fp));
2512 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002513 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002514}
2515
2516TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2517 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002518 SetFileTo(tf.path, "0123456789");
2519 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002520 ASSERT_NE(-1, fd);
2521 // POSIX: "The file position indicator associated with the new stream is set to the position
2522 // indicated by the file offset associated with the file descriptor."
2523 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2524 FILE* fp = fdopen(fd, "a");
2525 EXPECT_EQ(4, ftell(fp));
2526 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2527 EXPECT_EQ(2, ftell(fp));
2528 ASSERT_NE(EOF, fputs("xxx", fp));
2529 ASSERT_EQ(0, fflush(fp));
2530 EXPECT_EQ(13, ftell(fp));
2531 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2532 EXPECT_EQ(13, ftell(fp));
2533 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002534 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002535}
2536
2537TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2538 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002539 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002540 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002541 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002542 EXPECT_EQ(10, ftell(fp));
2543 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2544 EXPECT_EQ(2, ftell(fp));
2545 ASSERT_NE(EOF, fputs("xxx", fp));
2546 ASSERT_EQ(0, fflush(fp));
2547 EXPECT_EQ(13, ftell(fp));
2548 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2549 EXPECT_EQ(13, ftell(fp));
2550 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002551 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002552}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002553
2554TEST(STDIO_TEST, constants) {
2555 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2556 ASSERT_EQ(L_tmpnam, PATH_MAX);
2557}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002558
2559TEST(STDIO_TEST, perror) {
2560 ExecTestHelper eth;
2561 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2562 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2563 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2564}
2565
2566TEST(STDIO_TEST, puts) {
2567 ExecTestHelper eth;
2568 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2569}
2570
Elliott Hughes7cebf832020-08-12 14:25:41 -07002571TEST(STDIO_TEST, putchar) {
2572 ExecTestHelper eth;
2573 eth.Run([&]() { exit(putchar('A')); }, 65, "A");
2574}
2575
2576TEST(STDIO_TEST, putchar_unlocked) {
2577 ExecTestHelper eth;
2578 eth.Run([&]() { exit(putchar('B')); }, 66, "B");
2579}
2580
Elliott Hughes37ad9592017-10-30 17:47:12 -07002581TEST(STDIO_TEST, unlocked) {
2582 TemporaryFile tf;
2583
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002584 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002585 ASSERT_TRUE(fp != nullptr);
2586
2587 clearerr_unlocked(fp);
2588 ASSERT_FALSE(feof_unlocked(fp));
2589 ASSERT_FALSE(ferror_unlocked(fp));
2590
2591 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2592
2593 ASSERT_NE(EOF, putc_unlocked('a', fp));
2594 ASSERT_NE(EOF, putc('b', fp));
2595 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2596 ASSERT_NE(EOF, fputc('d', fp));
2597
2598 rewind(fp);
2599 ASSERT_EQ('a', getc_unlocked(fp));
2600 ASSERT_EQ('b', getc(fp));
2601 ASSERT_EQ('c', fgetc_unlocked(fp));
2602 ASSERT_EQ('d', fgetc(fp));
2603
2604 rewind(fp);
2605 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2606 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2607 ASSERT_EQ(0, fflush_unlocked(fp));
2608
2609 rewind(fp);
2610 char buf[BUFSIZ] = {};
2611 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2612 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2613 ASSERT_STREQ("ABCD", buf);
2614
2615 rewind(fp);
2616 ASSERT_NE(EOF, fputs("hello ", fp));
2617 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2618 ASSERT_NE(EOF, fputc('\n', fp));
2619
2620 rewind(fp);
2621 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2622 ASSERT_STREQ("hello world\n", buf);
2623
2624 ASSERT_EQ(0, fclose(fp));
2625}
Ryan Prichardbf549862017-11-07 15:30:32 -08002626
2627TEST(STDIO_TEST, fseek_64bit) {
2628 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002629 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002630 ASSERT_TRUE(fp != nullptr);
2631 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2632 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2633 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2634 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2635 ASSERT_EQ(0, fclose(fp));
2636}
2637
2638// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2639// isn't representable in long/off_t.
2640TEST(STDIO_TEST, fseek_overflow_32bit) {
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_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2644
2645 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2646#if defined(__BIONIC__) && !defined(__LP64__)
2647 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2648 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2649 ASSERT_EQ(EOVERFLOW, errno);
2650#endif
2651
2652 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2653 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2654 // and SEEK_END -- many C libraries check neither.)
2655 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2656 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2657
2658 fclose(fp);
2659}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002660
2661TEST(STDIO_TEST, dev_std_files) {
2662 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2663 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002664 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2665 ASSERT_LT(0, length);
2666 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2667
2668 length = readlink("/dev/stdout", path, sizeof(path));
2669 ASSERT_LT(0, length);
2670 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2671
2672 length = readlink("/dev/stderr", path, sizeof(path));
2673 ASSERT_LT(0, length);
2674 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002675}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002676
2677TEST(STDIO_TEST, fread_with_locked_file) {
2678 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2679 // files locked on other threads, even if it flushes some line-buffered files.
2680 FILE* fp1 = fopen("/dev/zero", "r");
2681 ASSERT_TRUE(fp1 != nullptr);
2682 flockfile(fp1);
2683
2684 std::thread([] {
2685 for (int mode : { _IONBF, _IOLBF }) {
2686 FILE* fp2 = fopen("/dev/zero", "r");
2687 ASSERT_TRUE(fp2 != nullptr);
2688 setvbuf(fp2, nullptr, mode, 0);
2689 ASSERT_EQ('\0', fgetc(fp2));
2690 fclose(fp2);
2691 }
2692 }).join();
2693
2694 funlockfile(fp1);
2695 fclose(fp1);
2696}
Elliott Hughes31c73092019-05-07 10:03:02 -07002697
2698TEST(STDIO_TEST, SEEK_macros) {
2699 ASSERT_EQ(0, SEEK_SET);
2700 ASSERT_EQ(1, SEEK_CUR);
2701 ASSERT_EQ(2, SEEK_END);
2702 ASSERT_EQ(3, SEEK_DATA);
2703 ASSERT_EQ(4, SEEK_HOLE);
2704 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2705 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2706}
Elliott Hughes05b675e2019-04-17 13:01:06 -07002707
2708TEST(STDIO_TEST, rename) {
2709 TemporaryDir td;
2710 std::string old_path = td.path + "/old"s;
2711 std::string new_path = td.path + "/new"s;
2712
2713 // Create the file, check it exists.
2714 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2715 struct stat sb;
2716 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2717 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2718
2719 // Rename and check it moved.
2720 ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str()));
2721 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2722 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2723}
2724
2725TEST(STDIO_TEST, renameat) {
2726 TemporaryDir td;
2727 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2728 std::string old_path = td.path + "/old"s;
2729 std::string new_path = td.path + "/new"s;
2730
2731 // Create the file, check it exists.
2732 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2733 struct stat sb;
2734 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2735 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2736
2737 // Rename and check it moved.
2738 ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new"));
2739 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2740 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2741}
2742
2743TEST(STDIO_TEST, renameat2) {
Colin Cross4c5595c2021-08-16 15:51:59 -07002744#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -07002745 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28 and musl doesn't have renameat2";
Elliott Hughes05b675e2019-04-17 13:01:06 -07002746#else
2747 TemporaryDir td;
2748 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2749 std::string old_path = td.path + "/old"s;
2750 std::string new_path = td.path + "/new"s;
2751
2752 // Create the file, check it exists.
2753 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2754 struct stat sb;
2755 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2756 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2757
2758 // Rename and check it moved.
2759 ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0));
2760 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2761 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2762
2763 // After this, both "old" and "new" exist.
2764 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2765
2766 // Rename and check it moved.
2767 ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE));
2768 ASSERT_EQ(EEXIST, errno);
2769#endif
2770}
2771
2772TEST(STDIO_TEST, renameat2_flags) {
2773#if defined(__GLIBC__)
2774 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2775#else
2776 ASSERT_NE(0, RENAME_EXCHANGE);
2777 ASSERT_NE(0, RENAME_NOREPLACE);
2778 ASSERT_NE(0, RENAME_WHITEOUT);
2779#endif
2780}
Elliott Hughes7cebf832020-08-12 14:25:41 -07002781
2782TEST(STDIO_TEST, fdopen_failures) {
2783 FILE* fp;
2784 int fd = open("/proc/version", O_RDONLY);
2785 ASSERT_TRUE(fd != -1);
2786
2787 // Nonsense mode.
2788 errno = 0;
2789 fp = fdopen(fd, "nonsense");
2790 ASSERT_TRUE(fp == nullptr);
2791 ASSERT_EQ(EINVAL, errno);
2792
2793 // Mode that isn't a subset of the fd's actual mode.
2794 errno = 0;
2795 fp = fdopen(fd, "w");
2796 ASSERT_TRUE(fp == nullptr);
2797 ASSERT_EQ(EINVAL, errno);
2798
2799 // Can't set append on the underlying fd.
2800 errno = 0;
2801 fp = fdopen(fd, "a");
2802 ASSERT_TRUE(fp == nullptr);
2803 ASSERT_EQ(EINVAL, errno);
2804
2805 // Bad fd.
2806 errno = 0;
2807 fp = fdopen(-1, "re");
2808 ASSERT_TRUE(fp == nullptr);
2809 ASSERT_EQ(EBADF, errno);
2810
2811 close(fd);
2812}
2813
2814TEST(STDIO_TEST, fmemopen_invalid_mode) {
2815 errno = 0;
2816 FILE* fp = fmemopen(nullptr, 16, "nonsense");
2817 ASSERT_TRUE(fp == nullptr);
2818 ASSERT_EQ(EINVAL, errno);
2819}
2820
2821TEST(STDIO_TEST, fopen_invalid_mode) {
2822 errno = 0;
2823 FILE* fp = fopen("/proc/version", "nonsense");
2824 ASSERT_TRUE(fp == nullptr);
2825 ASSERT_EQ(EINVAL, errno);
2826}
2827
2828TEST(STDIO_TEST, freopen_invalid_mode) {
2829 FILE* fp = fopen("/proc/version", "re");
2830 ASSERT_TRUE(fp != nullptr);
2831
2832 errno = 0;
2833 fp = freopen("/proc/version", "nonsense", fp);
2834 ASSERT_TRUE(fp == nullptr);
2835 ASSERT_EQ(EINVAL, errno);
2836}
2837
2838TEST(STDIO_TEST, asprintf_smoke) {
2839 char* p = nullptr;
2840 ASSERT_EQ(11, asprintf(&p, "hello %s", "world"));
2841 ASSERT_STREQ("hello world", p);
2842 free(p);
2843}
2844
2845TEST(STDIO_TEST, fopen_ENOENT) {
2846 errno = 0;
2847 FILE* fp = fopen("/proc/does-not-exist", "re");
2848 ASSERT_TRUE(fp == nullptr);
2849 ASSERT_EQ(ENOENT, errno);
2850}
Elliott Hughes439ebbd2020-12-04 18:51:42 -08002851
2852static void tempnam_test(bool has_TMPDIR, const char* dir, const char* prefix, const char* re) {
2853 if (has_TMPDIR) {
2854 setenv("TMPDIR", "/my/tmp/dir", 1);
2855 } else {
2856 unsetenv("TMPDIR");
2857 }
2858 char* s1 = tempnam(dir, prefix);
2859 char* s2 = tempnam(dir, prefix);
2860 ASSERT_MATCH(s1, re);
2861 ASSERT_MATCH(s2, re);
2862 ASSERT_STRNE(s1, s2);
2863 free(s1);
2864 free(s2);
2865}
2866
2867TEST(STDIO_TEST, tempnam__system_directory_system_prefix_with_TMPDIR) {
2868 tempnam_test(true, nullptr, nullptr, "^/my/tmp/dir/.*");
2869}
2870
2871TEST(STDIO_TEST, tempnam__system_directory_system_prefix_without_TMPDIR) {
2872 tempnam_test(false, nullptr, nullptr, "^/data/local/tmp/.*");
2873}
2874
2875TEST(STDIO_TEST, tempnam__system_directory_user_prefix_with_TMPDIR) {
2876 tempnam_test(true, nullptr, "prefix", "^/my/tmp/dir/prefix.*");
2877}
2878
2879TEST(STDIO_TEST, tempnam__system_directory_user_prefix_without_TMPDIR) {
2880 tempnam_test(false, nullptr, "prefix", "^/data/local/tmp/prefix.*");
2881}
2882
2883TEST(STDIO_TEST, tempnam__user_directory_system_prefix_with_TMPDIR) {
2884 tempnam_test(true, "/a/b/c", nullptr, "^/my/tmp/dir/.*");
2885}
2886
2887TEST(STDIO_TEST, tempnam__user_directory_system_prefix_without_TMPDIR) {
2888 tempnam_test(false, "/a/b/c", nullptr, "^/a/b/c/.*");
2889}
2890
2891TEST(STDIO_TEST, tempnam__user_directory_user_prefix_with_TMPDIR) {
2892 tempnam_test(true, "/a/b/c", "prefix", "^/my/tmp/dir/prefix.*");
2893}
2894
2895TEST(STDIO_TEST, tempnam__user_directory_user_prefix_without_TMPDIR) {
2896 tempnam_test(false, "/a/b/c", "prefix", "^/a/b/c/prefix.*");
2897}
2898
2899static void tmpnam_test(char* s) {
2900 char s1[L_tmpnam], s2[L_tmpnam];
2901
2902 strcpy(s1, tmpnam(s));
2903 strcpy(s2, tmpnam(s));
2904 ASSERT_MATCH(s1, "/tmp/.*");
2905 ASSERT_MATCH(s2, "/tmp/.*");
2906 ASSERT_STRNE(s1, s2);
2907}
2908
2909TEST(STDIO_TEST, tmpnam) {
2910 tmpnam_test(nullptr);
2911}
2912
2913TEST(STDIO_TEST, tmpnam_buf) {
2914 char buf[L_tmpnam];
2915 tmpnam_test(buf);
2916}
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002917
2918TEST(STDIO_TEST, freopen_null_filename_mode) {
2919 TemporaryFile tf;
2920 FILE* fp = fopen(tf.path, "r");
2921 ASSERT_TRUE(fp != nullptr);
2922
2923 // "r" = O_RDONLY
2924 char buf[1];
2925 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2926 ASSERT_EQ(-1, write(fileno(fp), "hello", 1));
2927 // "r+" = O_RDWR
2928 fp = freopen(nullptr, "r+", fp);
2929 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2930 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2931 // "w" = O_WRONLY
2932 fp = freopen(nullptr, "w", fp);
2933 ASSERT_EQ(-1, read(fileno(fp), buf, 1));
2934 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2935
2936 fclose(fp);
2937}