blob: 87031f6fc574f4302b40ffa6ff89574d142d6b83 [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__)
Yi Kongf9b13132022-03-16 16:12:22 +0800367#pragma GCC diagnostic push
368#pragma GCC diagnostic ignored "-Wformat"
Elliott Hughes41398d02018-03-07 13:32:58 -0800369 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700370 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700371 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800372 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Yi Kongf9b13132022-03-16 16:12:22 +0800373#pragma GCC diagnostic pop
Elliott Hughese2341d02014-05-02 18:16:32 -0700374#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800375 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700376#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700377}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700378
Elliott Hughes7cebf832020-08-12 14:25:41 -0700379TEST(STDIO_TEST, snprintf_measure) {
380 char buf[16];
381 ASSERT_EQ(11, snprintf(buf, 0, "Hello %s", "world"));
382}
383
Christopher Ferris13f26a72016-01-13 13:47:58 -0800384TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700385 char buf[BUFSIZ];
386
387 snprintf(buf, sizeof(buf), "a");
388 EXPECT_STREQ("a", buf);
389
390 snprintf(buf, sizeof(buf), "%%");
391 EXPECT_STREQ("%", buf);
392
393 snprintf(buf, sizeof(buf), "01234");
394 EXPECT_STREQ("01234", buf);
395
396 snprintf(buf, sizeof(buf), "a%sb", "01234");
397 EXPECT_STREQ("a01234b", buf);
398
Yi Kong32bc0fc2018-08-02 17:31:13 -0700399 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700400 snprintf(buf, sizeof(buf), "a%sb", s);
401 EXPECT_STREQ("a(null)b", buf);
402
403 snprintf(buf, sizeof(buf), "aa%scc", "bb");
404 EXPECT_STREQ("aabbcc", buf);
405
406 snprintf(buf, sizeof(buf), "a%cc", 'b');
407 EXPECT_STREQ("abc", buf);
408
409 snprintf(buf, sizeof(buf), "a%db", 1234);
410 EXPECT_STREQ("a1234b", buf);
411
412 snprintf(buf, sizeof(buf), "a%db", -8123);
413 EXPECT_STREQ("a-8123b", buf);
414
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700415 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700416 EXPECT_STREQ("a16b", buf);
417
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700418 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700419 EXPECT_STREQ("a16b", buf);
420
421 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
422 EXPECT_STREQ("a68719476736b", buf);
423
424 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
425 EXPECT_STREQ("a70000b", buf);
426
427 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
428 EXPECT_STREQ("a0xb0001234b", buf);
429
430 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
431 EXPECT_STREQ("a12abz", buf);
432
433 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
434 EXPECT_STREQ("a12ABz", buf);
435
436 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
437 EXPECT_STREQ("a00123456z", buf);
438
439 snprintf(buf, sizeof(buf), "a%5dz", 1234);
440 EXPECT_STREQ("a 1234z", buf);
441
442 snprintf(buf, sizeof(buf), "a%05dz", 1234);
443 EXPECT_STREQ("a01234z", buf);
444
445 snprintf(buf, sizeof(buf), "a%8dz", 1234);
446 EXPECT_STREQ("a 1234z", buf);
447
448 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
449 EXPECT_STREQ("a1234 z", buf);
450
451 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
452 EXPECT_STREQ("Aabcdef Z", buf);
453
454 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
455 EXPECT_STREQ("Ahello:1234Z", buf);
456
457 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
458 EXPECT_STREQ("a005:5:05z", buf);
459
Yi Kong32bc0fc2018-08-02 17:31:13 -0700460 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700461 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700462#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700463 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800464#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700465 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800466#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700467
468 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
469 EXPECT_STREQ("a68719476736,6,7,8z", buf);
470
471 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
472 EXPECT_STREQ("a_1.230000_b", buf);
473
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700474 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700475 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400476
477 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
478 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700479}
480
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800481template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700482static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
483 int sscanf_fn(const T*, const T*, ...),
484 const T* fmt_string, const T* fmt, const T* fmt_plus,
485 const T* minus_inf, const T* inf_, const T* plus_inf,
486 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800487 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700488 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700489
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700490 // NaN.
491
492 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800493 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700494 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
495 EXPECT_TRUE(isnan(f));
496
497 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800498 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700499 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
500 EXPECT_TRUE(isnan(f));
501
502 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800503 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700504 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
505 EXPECT_TRUE(isnan(f));
506
507 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800508 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700509 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
510 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800511
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700512 // Inf.
513
514 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800515 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700516 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
517 EXPECT_EQ(HUGE_VALF, f);
518
519 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800520 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700521 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
522 EXPECT_EQ(-HUGE_VALF, f);
523
524 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800525 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700526 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
527 EXPECT_EQ(HUGE_VALF, f);
528
529 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800530 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700531 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
532 EXPECT_EQ(-HUGE_VALF, f);
533
534 // Check case-insensitivity.
535 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
536 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
537 EXPECT_EQ(HUGE_VALF, f);
538 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
539 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
540 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700541}
542
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700543TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
544 CheckInfNan(snprintf, sscanf, "%s",
545 "[%a]", "[%+a]",
546 "[-inf]", "[inf]", "[+inf]",
547 "[-nan]", "[nan]", "[+nan]");
548 CheckInfNan(snprintf, sscanf, "%s",
549 "[%A]", "[%+A]",
550 "[-INF]", "[INF]", "[+INF]",
551 "[-NAN]", "[NAN]", "[+NAN]");
552 CheckInfNan(snprintf, sscanf, "%s",
553 "[%e]", "[%+e]",
554 "[-inf]", "[inf]", "[+inf]",
555 "[-nan]", "[nan]", "[+nan]");
556 CheckInfNan(snprintf, sscanf, "%s",
557 "[%E]", "[%+E]",
558 "[-INF]", "[INF]", "[+INF]",
559 "[-NAN]", "[NAN]", "[+NAN]");
560 CheckInfNan(snprintf, sscanf, "%s",
561 "[%f]", "[%+f]",
562 "[-inf]", "[inf]", "[+inf]",
563 "[-nan]", "[nan]", "[+nan]");
564 CheckInfNan(snprintf, sscanf, "%s",
565 "[%F]", "[%+F]",
566 "[-INF]", "[INF]", "[+INF]",
567 "[-NAN]", "[NAN]", "[+NAN]");
568 CheckInfNan(snprintf, sscanf, "%s",
569 "[%g]", "[%+g]",
570 "[-inf]", "[inf]", "[+inf]",
571 "[-nan]", "[nan]", "[+nan]");
572 CheckInfNan(snprintf, sscanf, "%s",
573 "[%G]", "[%+G]",
574 "[-INF]", "[INF]", "[+INF]",
575 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800576}
Elliott Hughes7823f322014-04-14 12:11:28 -0700577
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700578TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
579 CheckInfNan(swprintf, swscanf, L"%s",
580 L"[%a]", L"[%+a]",
581 L"[-inf]", L"[inf]", L"[+inf]",
582 L"[-nan]", L"[nan]", L"[+nan]");
583 CheckInfNan(swprintf, swscanf, L"%s",
584 L"[%A]", L"[%+A]",
585 L"[-INF]", L"[INF]", L"[+INF]",
586 L"[-NAN]", L"[NAN]", L"[+NAN]");
587 CheckInfNan(swprintf, swscanf, L"%s",
588 L"[%e]", L"[%+e]",
589 L"[-inf]", L"[inf]", L"[+inf]",
590 L"[-nan]", L"[nan]", L"[+nan]");
591 CheckInfNan(swprintf, swscanf, L"%s",
592 L"[%E]", L"[%+E]",
593 L"[-INF]", L"[INF]", L"[+INF]",
594 L"[-NAN]", L"[NAN]", L"[+NAN]");
595 CheckInfNan(swprintf, swscanf, L"%s",
596 L"[%f]", L"[%+f]",
597 L"[-inf]", L"[inf]", L"[+inf]",
598 L"[-nan]", L"[nan]", L"[+nan]");
599 CheckInfNan(swprintf, swscanf, L"%s",
600 L"[%F]", L"[%+F]",
601 L"[-INF]", L"[INF]", L"[+INF]",
602 L"[-NAN]", L"[NAN]", L"[+NAN]");
603 CheckInfNan(swprintf, swscanf, L"%s",
604 L"[%g]", L"[%+g]",
605 L"[-inf]", L"[inf]", L"[+inf]",
606 L"[-nan]", L"[nan]", L"[+nan]");
607 CheckInfNan(swprintf, swscanf, L"%s",
608 L"[%G]", L"[%+G]",
609 L"[-INF]", L"[INF]", L"[+INF]",
610 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700611}
612
Dan Albert9601f162017-08-09 14:59:06 -0700613TEST(STDIO_TEST, swprintf) {
614 constexpr size_t nchars = 32;
615 wchar_t buf[nchars];
616
617 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
618 ASSERT_EQ(std::wstring(L"ab"), buf);
619 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
620 ASSERT_EQ(std::wstring(L"abcde"), buf);
621
622 // Unlike swprintf(), swprintf() returns -1 in case of truncation
623 // and doesn't necessarily zero-terminate the output!
624 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
625
626 const char kString[] = "Hello, World";
627 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
628 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
629 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
630 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
631}
632
633TEST(STDIO_TEST, swprintf_a) {
634 constexpr size_t nchars = 32;
635 wchar_t buf[nchars];
636
637 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
638 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
639}
640
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700641TEST(STDIO_TEST, swprintf_lc) {
642 constexpr size_t nchars = 32;
643 wchar_t buf[nchars];
644
645 wint_t wc = L'a';
646 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
647 EXPECT_EQ(std::wstring(L"<a>"), buf);
648}
649
650TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
651 constexpr size_t nchars = 32;
652 wchar_t buf[nchars];
653
654 wint_t wc = L'a';
655 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
656 EXPECT_EQ(std::wstring(L"<a>"), buf);
657}
658
Elliott Hughes618303c2017-11-02 16:58:44 -0700659TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
660 constexpr size_t nchars = 32;
661 wchar_t buf[nchars];
662
663 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
664 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
665}
666
667TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
668 constexpr size_t nchars = 32;
669 wchar_t buf[nchars];
670
671 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
672 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
673}
674
675TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
676 constexpr size_t nchars = 32;
677 wchar_t buf[nchars];
678
679 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
680 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
681}
682
683TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
684 constexpr size_t nchars = 32;
685 wchar_t buf[nchars];
686
687 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
688 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
689}
690
Dan Albert9601f162017-08-09 14:59:06 -0700691TEST(STDIO_TEST, swprintf_ls) {
692 constexpr size_t nchars = 32;
693 wchar_t buf[nchars];
694
695 static const wchar_t kWideString[] = L"Hello\uff41 World";
696 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
697 ASSERT_EQ(std::wstring(kWideString), buf);
698 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
699 ASSERT_EQ(std::wstring(kWideString), buf);
700}
701
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700702TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
703 constexpr size_t nchars = 32;
704 wchar_t buf[nchars];
705
706 static const wchar_t kWideString[] = L"Hello\uff41 World";
707 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
708 ASSERT_EQ(std::wstring(kWideString), buf);
709 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
710 ASSERT_EQ(std::wstring(kWideString), buf);
711}
712
Christopher Ferris13f26a72016-01-13 13:47:58 -0800713TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700714 char buf[BUFSIZ];
715 snprintf(buf, sizeof(buf), "%d", INT_MAX);
716 EXPECT_STREQ("2147483647", buf);
717}
718
Christopher Ferris13f26a72016-01-13 13:47:58 -0800719TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700720 char buf[BUFSIZ];
721 snprintf(buf, sizeof(buf), "%d", INT_MIN);
722 EXPECT_STREQ("-2147483648", buf);
723}
724
Elliott Hughes618303c2017-11-02 16:58:44 -0700725TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
726 char buf[BUFSIZ];
727 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
728 EXPECT_STREQ("9223372036854775807", buf);
729}
730
731TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
732 char buf[BUFSIZ];
733 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
734 EXPECT_STREQ("-9223372036854775808", buf);
735}
736
737TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
738 char buf[BUFSIZ];
739 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
740 EXPECT_STREQ("18446744073709551615", buf);
741}
742
743TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
744 char buf[BUFSIZ];
745 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
746 EXPECT_STREQ("18446744073709551615", buf);
747}
748
Christopher Ferris13f26a72016-01-13 13:47:58 -0800749TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700750 char buf[BUFSIZ];
751 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700752#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700753 EXPECT_STREQ("9223372036854775807", buf);
754#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700755 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700756#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700757}
758
Christopher Ferris13f26a72016-01-13 13:47:58 -0800759TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700760 char buf[BUFSIZ];
761 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700762#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700763 EXPECT_STREQ("-9223372036854775808", buf);
764#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700765 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700766#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700767}
768
Christopher Ferris13f26a72016-01-13 13:47:58 -0800769TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700770 char buf[BUFSIZ];
771 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
772 EXPECT_STREQ("9223372036854775807", buf);
773}
774
Christopher Ferris13f26a72016-01-13 13:47:58 -0800775TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700776 char buf[BUFSIZ];
777 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
778 EXPECT_STREQ("-9223372036854775808", buf);
779}
780
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700781TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
782 char buf[BUFSIZ];
783 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
784 EXPECT_STREQ("37777777777", buf);
785}
786
787TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
788 char buf[BUFSIZ];
789 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
790 EXPECT_STREQ("4294967295", buf);
791}
792
793TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
794 char buf[BUFSIZ];
795 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
796 EXPECT_STREQ("ffffffff", buf);
797}
798
799TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
800 char buf[BUFSIZ];
801 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
802 EXPECT_STREQ("FFFFFFFF", buf);
803}
804
Christopher Ferris13f26a72016-01-13 13:47:58 -0800805TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700806 char buf[BUFSIZ];
807
808 snprintf(buf, sizeof(buf), "%e", 1.5);
809 EXPECT_STREQ("1.500000e+00", buf);
810
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800811 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700812 EXPECT_STREQ("1.500000e+00", buf);
813}
814
Christopher Ferris13f26a72016-01-13 13:47:58 -0800815TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700816 char buf[BUFSIZ];
817
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800818 snprintf(buf, sizeof(buf), "%e", -0.0);
819 EXPECT_STREQ("-0.000000e+00", buf);
820 snprintf(buf, sizeof(buf), "%E", -0.0);
821 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700822 snprintf(buf, sizeof(buf), "%f", -0.0);
823 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800824 snprintf(buf, sizeof(buf), "%F", -0.0);
825 EXPECT_STREQ("-0.000000", buf);
826 snprintf(buf, sizeof(buf), "%g", -0.0);
827 EXPECT_STREQ("-0", buf);
828 snprintf(buf, sizeof(buf), "%G", -0.0);
829 EXPECT_STREQ("-0", buf);
830 snprintf(buf, sizeof(buf), "%a", -0.0);
831 EXPECT_STREQ("-0x0p+0", buf);
832 snprintf(buf, sizeof(buf), "%A", -0.0);
833 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700834}
835
Christopher Ferris13f26a72016-01-13 13:47:58 -0800836TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700837 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700838 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700839
Elliott Hughes69f05d22014-06-05 20:10:09 -0700840 // http://b/15439554
841 char buf[BUFSIZ];
842
843 // 1-byte character.
844 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
845 EXPECT_STREQ("1x2", buf);
846 // 2-byte character.
847 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
848 EXPECT_STREQ("1¢2", buf);
849 // 3-byte character.
850 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
851 EXPECT_STREQ("1€2", buf);
852 // 4-byte character.
853 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
854 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700855
Wally Yaua40fdbd2014-08-26 09:47:23 -0700856 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700857 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700858}
859
Elliott Hughes43f7c872016-02-05 11:18:41 -0800860static void* snprintf_small_stack_fn(void*) {
861 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
862 char buf[PATH_MAX];
863 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
864 return nullptr;
865}
866
867TEST(STDIO_TEST, snprintf_small_stack) {
868 // Is it safe to call snprintf on a thread with a small stack?
869 // (The snprintf implementation puts some pretty large buffers on the stack.)
870 pthread_attr_t a;
871 ASSERT_EQ(0, pthread_attr_init(&a));
872 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
873
874 pthread_t t;
875 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
876 ASSERT_EQ(0, pthread_join(t, nullptr));
877}
878
Elliott Hughes8200e552016-02-05 21:57:37 -0800879TEST(STDIO_TEST, snprintf_asterisk_overflow) {
880 char buf[128];
881 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
882 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
883 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
884 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
885 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
886
887 // INT_MAX-1, INT_MAX, INT_MAX+1.
888 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
889 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
890 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
891 ASSERT_EQ(ENOMEM, errno);
892}
893
Elliott Hughes5dc31302020-01-07 08:48:10 -0800894// Inspired by https://github.com/landley/toybox/issues/163.
895TEST(STDIO_TEST, printf_NULL) {
896 char buf[128];
897 char* null = nullptr;
898 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 2, null));
899 EXPECT_STREQ("<(n>", buf);
900 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%*.*s>", 2, 8, null));
901 EXPECT_STREQ("<(null)>", buf);
902 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 2, null));
903 EXPECT_STREQ("< (n>", buf);
904 EXPECT_EQ(10, snprintf(buf, sizeof(buf), "<%*.*s>", 8, 8, null));
905 EXPECT_STREQ("< (null)>", buf);
906}
907
Elliott Hughes70715da2016-08-01 16:35:17 -0700908TEST(STDIO_TEST, fprintf) {
909 TemporaryFile tf;
910
911 FILE* tfile = fdopen(tf.fd, "r+");
912 ASSERT_TRUE(tfile != nullptr);
913
914 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
915 AssertFileIs(tfile, "123 abc");
916 fclose(tfile);
917}
918
Christopher Ferris13f26a72016-01-13 13:47:58 -0800919TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700920 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700921 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700922 int fd_rdonly = open("/dev/null", O_RDONLY);
923 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700924
925 // Unbuffered case where the fprintf(3) itself fails.
926 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700927 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700928 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700929 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700930 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700931 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700932
933 // Buffered case where we won't notice until the fclose(3).
934 // It's likely this is what was actually seen in http://b/7229520,
935 // and that expecting fprintf to fail is setting yourself up for
936 // disappointment. Remember to check fclose(3)'s return value, kids!
937 ASSERT_NE(nullptr, fp = tmpfile());
938 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700939 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700940 ASSERT_EQ(4, fprintf(fp, "fail"));
941 ASSERT_EQ(-1, fclose(fp));
942}
943
Elliott Hughes468efc82018-07-10 14:39:49 -0700944TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800945 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700946 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800947
948 char buf[16];
949 char* s = fgets(buf, sizeof(buf), fp);
950 buf[13] = '\0';
951 ASSERT_STREQ("Linux version", s);
952
953 ASSERT_EQ(0, pclose(fp));
954}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700955
Elliott Hughes468efc82018-07-10 14:39:49 -0700956TEST(STDIO_TEST, popen_socketpair) {
957 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700958 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700959
960 fputs("hello\nworld\n", fp);
961 fflush(fp);
962
963 char buf[16];
964 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
965 EXPECT_STREQ("hello\n", buf);
966 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
967 EXPECT_STREQ("world\n", buf);
968
969 ASSERT_EQ(0, pclose(fp));
970}
971
972TEST(STDIO_TEST, popen_socketpair_shutdown) {
973 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700974 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700975
976 fputs("a\na\na\na\nb\n", fp);
977 fflush(fp);
978 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
979
980 char buf[16];
981 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
982 EXPECT_STREQ(" 4 a\n", buf);
983 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
984 EXPECT_STREQ(" 1 b\n", buf);
985
986 ASSERT_EQ(0, pclose(fp));
987}
988
989TEST(STDIO_TEST, popen_return_value_0) {
990 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700991 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700992 int status = pclose(fp);
993 EXPECT_TRUE(WIFEXITED(status));
994 EXPECT_EQ(0, WEXITSTATUS(status));
995}
996
997TEST(STDIO_TEST, popen_return_value_1) {
998 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700999 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001000 int status = pclose(fp);
1001 EXPECT_TRUE(WIFEXITED(status));
1002 EXPECT_EQ(1, WEXITSTATUS(status));
1003}
1004
1005TEST(STDIO_TEST, popen_return_value_signal) {
1006 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001007 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -07001008 int status = pclose(fp);
1009 EXPECT_TRUE(WIFSIGNALED(status));
1010 EXPECT_EQ(7, WTERMSIG(status));
1011}
1012
Christopher Ferris13f26a72016-01-13 13:47:58 -08001013TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001014 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001015 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001016 ASSERT_EQ('L', getc(fp));
1017 ASSERT_EQ('i', getc(fp));
1018 ASSERT_EQ('n', getc(fp));
1019 ASSERT_EQ('u', getc(fp));
1020 ASSERT_EQ('x', getc(fp));
1021 fclose(fp);
1022}
1023
Christopher Ferris13f26a72016-01-13 13:47:58 -08001024TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001025 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001026 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -07001027 ASSERT_EQ(EOF, putc('x', fp));
1028 fclose(fp);
1029}
Elliott Hughes603332f2014-03-12 17:10:41 -07001030
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001031TEST(STDIO_TEST, sscanf_swscanf) {
1032 struct stuff {
1033 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001034 int i1, i2;
1035 char cs1[3];
1036 char s2[3];
1037 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001038 double d1;
1039 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001040 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001041
1042 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001043 EXPECT_STREQ("hello", s1);
1044 EXPECT_EQ(123, i1);
1045 EXPECT_EQ(456, i2);
1046 EXPECT_EQ('a', cs1[0]);
1047 EXPECT_EQ('b', cs1[1]);
1048 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
1049 EXPECT_STREQ("AB", s2); // Terminating NUL.
1050 EXPECT_EQ('!', c1);
1051 EXPECT_DOUBLE_EQ(1.23, d1);
1052 EXPECT_FLOAT_EQ(9.0f, f1);
1053 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001054 }
1055 } s;
1056
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001057 memset(&s, 'x', sizeof(s));
1058 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1059 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1060 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 -07001061 s.Check();
1062
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001063 memset(&s, 'x', sizeof(s));
1064 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1065 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1066 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 -07001067 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001068}
Elliott Hughes53b24382014-05-02 18:29:25 -07001069
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001070template <typename T>
1071static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1072 const T* input, const T* fmt,
1073 int expected_count, const char* expected_string) {
1074 char buf[256] = {};
1075 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1076 ASSERT_STREQ(expected_string, buf) << fmt;
1077}
1078
1079TEST(STDIO_TEST, sscanf_ccl) {
1080 // `abc` is just those characters.
1081 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1082 // `a-c` is the range 'a' .. 'c'.
1083 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1084 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1085 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1086 // `a-c-e` is equivalent to `a-e`.
1087 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1088 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1089 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1090 // An initial '^' negates the set.
1091 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1092 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1093 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1094 // The first character may be ']' or '-' without being special.
1095 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1096 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1097 // The last character may be '-' without being special.
1098 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1099 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1100 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1101}
1102
1103TEST(STDIO_TEST, swscanf_ccl) {
1104 // `abc` is just those characters.
1105 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1106 // `a-c` is the range 'a' .. 'c'.
1107 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1108 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1109 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1110 // `a-c-e` is equivalent to `a-e`.
1111 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1112 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1113 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1114 // An initial '^' negates the set.
1115 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1116 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1117 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1118 // The first character may be ']' or '-' without being special.
1119 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1120 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1121 // The last character may be '-' without being special.
1122 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1123 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1124 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1125}
1126
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001127template <typename T1, typename T2>
1128static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1129 const T1* input, const T1* fmt,
1130 int expected_count, const T2* expected_string) {
1131 T2* result = nullptr;
1132 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1133 if (expected_string == nullptr) {
1134 ASSERT_EQ(nullptr, result);
1135 } else {
1136 ASSERT_STREQ(expected_string, result) << fmt;
1137 }
1138 free(result);
1139}
1140
1141TEST(STDIO_TEST, sscanf_mc) {
1142 char* p1 = nullptr;
1143 char* p2 = nullptr;
1144 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1145 ASSERT_EQ('h', *p1);
1146 ASSERT_EQ('e', *p2);
1147 free(p1);
1148 free(p2);
1149
1150 p1 = nullptr;
1151 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1152 ASSERT_EQ('h', p1[0]);
1153 ASSERT_EQ('e', p1[1]);
1154 ASSERT_EQ('l', p1[2]);
1155 ASSERT_EQ('l', p1[3]);
1156 free(p1);
1157
1158 p1 = nullptr;
1159 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1160 ASSERT_EQ('h', p1[0]);
1161 ASSERT_EQ('e', p1[1]);
1162 ASSERT_EQ('l', p1[2]);
1163 ASSERT_EQ('l', p1[3]);
1164 ASSERT_EQ('o', p1[4]);
1165 free(p1);
1166}
1167
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001168TEST(STDIO_TEST, sscanf_mlc) {
1169 // This is so useless that clang doesn't even believe it exists...
1170#pragma clang diagnostic push
1171#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1172#pragma clang diagnostic ignored "-Wformat-extra-args"
1173
1174 wchar_t* p1 = nullptr;
1175 wchar_t* p2 = nullptr;
1176 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1177 ASSERT_EQ(L'h', *p1);
1178 ASSERT_EQ(L'e', *p2);
1179 free(p1);
1180 free(p2);
1181
1182 p1 = nullptr;
1183 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1184 ASSERT_EQ(L'h', p1[0]);
1185 ASSERT_EQ(L'e', p1[1]);
1186 ASSERT_EQ(L'l', p1[2]);
1187 ASSERT_EQ(L'l', p1[3]);
1188 free(p1);
1189
1190 p1 = nullptr;
1191 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1192 ASSERT_EQ(L'h', p1[0]);
1193 ASSERT_EQ(L'e', p1[1]);
1194 ASSERT_EQ(L'l', p1[2]);
1195 ASSERT_EQ(L'l', p1[3]);
1196 ASSERT_EQ(L'o', p1[4]);
1197 free(p1);
1198#pragma clang diagnostic pop
1199}
1200
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001201TEST(STDIO_TEST, sscanf_ms) {
1202 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1203 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1204 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1205}
1206
1207TEST(STDIO_TEST, sscanf_mls) {
1208 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1209 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1210 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1211}
1212
1213TEST(STDIO_TEST, sscanf_m_ccl) {
1214 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1215 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1216 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1217}
1218
1219TEST(STDIO_TEST, sscanf_ml_ccl) {
1220 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1221 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1222 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1223}
1224
1225TEST(STDIO_TEST, sscanf_ls) {
1226 wchar_t w[32] = {};
1227 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1228 ASSERT_EQ(L"hello", std::wstring(w));
1229}
1230
1231TEST(STDIO_TEST, sscanf_ls_suppress) {
1232 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1233}
1234
1235TEST(STDIO_TEST, sscanf_ls_n) {
1236 setlocale(LC_ALL, "C.UTF-8");
1237 wchar_t w[32] = {};
1238 int pos = 0;
1239 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1240 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1241 ASSERT_EQ(2, pos);
1242}
1243
1244TEST(STDIO_TEST, sscanf_ls_realloc) {
1245 // This is so useless that clang doesn't even believe it exists...
1246#pragma clang diagnostic push
1247#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1248#pragma clang diagnostic ignored "-Wformat-extra-args"
1249 wchar_t* p1 = nullptr;
1250 wchar_t* p2 = nullptr;
1251 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1252 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1253 ASSERT_EQ(L"world", std::wstring(p2));
1254#pragma clang diagnostic pop
1255}
1256
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001257// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1258TEST(STDIO_TEST, scanf_wscanf_EOF) {
1259 EXPECT_EQ(0, sscanf("b", "ab"));
1260 EXPECT_EQ(EOF, sscanf("", "a"));
1261 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1262 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1263}
1264
1265TEST(STDIO_TEST, scanf_invalid_UTF8) {
1266#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1267 char buf[BUFSIZ];
1268 wchar_t wbuf[BUFSIZ];
1269
1270 memset(buf, 0, sizeof(buf));
1271 memset(wbuf, 0, sizeof(wbuf));
1272 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1273#endif
1274}
1275
1276TEST(STDIO_TEST, scanf_no_match_no_termination) {
1277 char buf[4] = "x";
1278 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1279 EXPECT_EQ('x', buf[0]);
1280 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1281 EXPECT_EQ('x', buf[0]);
1282
1283 wchar_t wbuf[4] = L"x";
1284 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1285 EXPECT_EQ(L'x', wbuf[0]);
1286
1287 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1288 EXPECT_EQ('x', buf[0]);
1289
1290 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1291 EXPECT_EQ(L'x', wbuf[0]);
1292}
1293
1294TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1295#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1296 wchar_t buf[BUFSIZ];
1297
1298 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1299 memset(buf, 0, sizeof(buf));
1300 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1301 EXPECT_EQ(L"x"s, std::wstring(buf));
1302 memset(buf, 0, sizeof(buf));
1303 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1304 EXPECT_EQ(L"x"s, std::wstring(buf));
1305
1306 // Even if scanf has wide characters in a class, they won't match...
1307 // TODO: is that a bug?
1308 memset(buf, 0, sizeof(buf));
1309 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1310 EXPECT_EQ(L"x"s, std::wstring(buf));
1311 // ...unless you use wscanf.
1312 memset(buf, 0, sizeof(buf));
1313 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1314 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1315
1316 // Negation only covers ASCII for scanf...
1317 memset(buf, 0, sizeof(buf));
1318 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1319 EXPECT_EQ(L"x"s, std::wstring(buf));
1320 // ...but covers wide characters for wscanf.
1321 memset(buf, 0, sizeof(buf));
1322 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1323 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1324
1325 // We already determined that non-ASCII characters are ignored in scanf classes.
1326 memset(buf, 0, sizeof(buf));
1327 EXPECT_EQ(1, sscanf("x"
1328 "\xc4\x80" // Matches a byte from each wide char in the class.
1329 "\xc6\x82" // Neither byte is in the class.
1330 "yz",
1331 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1332 EXPECT_EQ(L"x", std::wstring(buf));
1333 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1334 memset(buf, 0, sizeof(buf));
1335 EXPECT_EQ(1, swscanf(L"x"
1336 L"\xc4\x80"
1337 L"\xc6\x82"
1338 L"yz",
1339 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1340 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1341 // not put back together as a wide character.
1342 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1343#endif
1344}
1345
Christopher Ferris13f26a72016-01-13 13:47:58 -08001346TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001347 // If we open a file read-only...
1348 FILE* fp = fopen("/proc/version", "r");
1349
1350 // ...all attempts to write to that file should return failure.
1351
1352 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1353 // glibc gets the wide-character functions wrong.
1354
1355 errno = 0;
1356 EXPECT_EQ(EOF, putc('x', fp));
1357 EXPECT_EQ(EBADF, errno);
1358
1359 errno = 0;
1360 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1361 EXPECT_EQ(EBADF, errno);
1362
1363 errno = 0;
1364 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001365#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001366 EXPECT_EQ(EBADF, errno);
1367#endif
1368
1369 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001370 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1371 EXPECT_EQ(EBADF, errno);
1372
1373 errno = 0;
1374 EXPECT_EQ(EOF, fputs("hello", fp));
1375 EXPECT_EQ(EBADF, errno);
1376
1377 errno = 0;
1378 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001379#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001380 EXPECT_EQ(EBADF, errno);
1381#endif
1382}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001383
1384// Tests that we can only have a consistent and correct fpos_t when using
1385// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001386TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001387 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1388 uselocale(LC_GLOBAL_LOCALE);
1389
1390 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001391 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001392
1393 wchar_t mb_one_bytes = L'h';
1394 wchar_t mb_two_bytes = 0x00a2;
1395 wchar_t mb_three_bytes = 0x20ac;
1396 wchar_t mb_four_bytes = 0x24b62;
1397
1398 // Write to file.
1399 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1400 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1401 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1402 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1403
1404 rewind(fp);
1405
1406 // Record each character position.
1407 fpos_t pos1;
1408 fpos_t pos2;
1409 fpos_t pos3;
1410 fpos_t pos4;
1411 fpos_t pos5;
1412 EXPECT_EQ(0, fgetpos(fp, &pos1));
1413 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1414 EXPECT_EQ(0, fgetpos(fp, &pos2));
1415 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1416 EXPECT_EQ(0, fgetpos(fp, &pos3));
1417 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1418 EXPECT_EQ(0, fgetpos(fp, &pos4));
1419 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1420 EXPECT_EQ(0, fgetpos(fp, &pos5));
1421
Elliott Hughes063525c2014-05-13 11:19:57 -07001422#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001423 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1424 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1425 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1426 // structure.
1427 ASSERT_EQ(0, static_cast<off_t>(pos1));
1428 ASSERT_EQ(1, static_cast<off_t>(pos2));
1429 ASSERT_EQ(3, static_cast<off_t>(pos3));
1430 ASSERT_EQ(6, static_cast<off_t>(pos4));
1431 ASSERT_EQ(10, static_cast<off_t>(pos5));
1432#endif
1433
1434 // Exercise back and forth movements of the position.
1435 ASSERT_EQ(0, fsetpos(fp, &pos2));
1436 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1437 ASSERT_EQ(0, fsetpos(fp, &pos1));
1438 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1439 ASSERT_EQ(0, fsetpos(fp, &pos4));
1440 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1441 ASSERT_EQ(0, fsetpos(fp, &pos3));
1442 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1443 ASSERT_EQ(0, fsetpos(fp, &pos5));
1444 ASSERT_EQ(WEOF, fgetwc(fp));
1445
1446 fclose(fp);
1447}
1448
1449// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001450TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001451 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1452 uselocale(LC_GLOBAL_LOCALE);
1453
Calin Juravle9b95ea92014-05-14 17:07:10 +01001454 // In glibc-2.16 fseek doesn't work properly in wide mode
1455 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1456 // to close and re-open the file. We do it in order to make the test pass
1457 // with all glibcs.
1458
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001459 TemporaryFile tf;
1460 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001461 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001462
1463 wchar_t mb_two_bytes = 0x00a2;
1464 wchar_t mb_three_bytes = 0x20ac;
1465 wchar_t mb_four_bytes = 0x24b62;
1466
1467 // Write to file.
1468 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1469 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1470 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1471
1472 fflush(fp);
1473 fclose(fp);
1474
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001475 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001476 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001477
1478 // Store a valid position.
1479 fpos_t mb_two_bytes_pos;
1480 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1481
1482 // Move inside mb_four_bytes with fseek.
1483 long offset_inside_mb = 6;
1484 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1485
1486 // Store the "inside multi byte" position.
1487 fpos_t pos_inside_mb;
1488 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001489#if defined(__BIONIC__)
1490 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1491#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001492
1493 // Reading from within a byte should produce an error.
1494 ASSERT_EQ(WEOF, fgetwc(fp));
1495 ASSERT_EQ(EILSEQ, errno);
1496
1497 // Reverting to a valid position should work.
1498 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1499 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1500
1501 // Moving withing a multi byte with fsetpos should work but reading should
1502 // produce an error.
1503 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1504 ASSERT_EQ(WEOF, fgetwc(fp));
1505 ASSERT_EQ(EILSEQ, errno);
1506
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001507 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001508}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001509
Christopher Ferris13f26a72016-01-13 13:47:58 -08001510TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001511 char buf[16];
1512 memset(buf, 0, sizeof(buf));
1513 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1514 ASSERT_EQ('<', fputc('<', fp));
1515 ASSERT_NE(EOF, fputs("abc>\n", fp));
1516 fflush(fp);
1517
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001518 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001519 ASSERT_STREQ("<abc>\n", buf);
1520
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001521 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001522 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001523 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001524}
1525
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001526TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001527 FILE* fp = fmemopen(nullptr, 128, "r+");
1528 ASSERT_NE(EOF, fputs("xyz\n", fp));
1529
Elliott Hughes70715da2016-08-01 16:35:17 -07001530 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001531 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001532}
1533
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001534TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1535 FILE* fp;
1536 char buf[8];
1537
1538 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1539 // shall be written at the current position or at the end of the buffer,
1540 // depending on the size of the contents."
1541 memset(buf, 'x', sizeof(buf));
1542 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1543 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1544 ASSERT_EQ(0, fflush(fp));
1545 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1546 // Now write and check that the NUL moves along with our writes...
1547 ASSERT_NE(EOF, fputs("hello", fp));
1548 ASSERT_EQ(0, fflush(fp));
1549 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1550 ASSERT_NE(EOF, fputs("wo", fp));
1551 ASSERT_EQ(0, fflush(fp));
1552 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1553 ASSERT_EQ(0, fclose(fp));
1554
1555 // "If a stream open for update is flushed or closed and the last write has
1556 // advanced the current buffer size, a null byte shall be written at the end
1557 // of the buffer if it fits."
1558 memset(buf, 'x', sizeof(buf));
1559 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1560 // Nothing written yet, so no advance...
1561 ASSERT_EQ(0, fflush(fp));
1562 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1563 ASSERT_NE(EOF, fputs("hello", fp));
1564 ASSERT_EQ(0, fclose(fp));
1565}
1566
1567TEST(STDIO_TEST, fmemopen_size) {
1568 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001569 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001570 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001571
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001572 // POSIX: "The stream shall also maintain the size of the current buffer
1573 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1574 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001575
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001576 // "For modes r and r+ the size shall be set to the value given by the size
1577 // argument."
1578 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1579 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1580 EXPECT_EQ(16, ftell(fp));
1581 EXPECT_EQ(16, ftello(fp));
1582 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1583 EXPECT_EQ(16, ftell(fp));
1584 EXPECT_EQ(16, ftello(fp));
1585 ASSERT_EQ(0, fclose(fp));
1586 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1587 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1588 EXPECT_EQ(16, ftell(fp));
1589 EXPECT_EQ(16, ftello(fp));
1590 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1591 EXPECT_EQ(16, ftell(fp));
1592 EXPECT_EQ(16, ftello(fp));
1593 ASSERT_EQ(0, fclose(fp));
1594
1595 // "For modes w and w+ the initial size shall be zero..."
1596 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1597 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1598 EXPECT_EQ(0, ftell(fp));
1599 EXPECT_EQ(0, ftello(fp));
1600 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1601 EXPECT_EQ(0, ftell(fp));
1602 EXPECT_EQ(0, ftello(fp));
1603 ASSERT_EQ(0, fclose(fp));
1604 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1605 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1606 EXPECT_EQ(0, ftell(fp));
1607 EXPECT_EQ(0, ftello(fp));
1608 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1609 EXPECT_EQ(0, ftell(fp));
1610 EXPECT_EQ(0, ftello(fp));
1611 ASSERT_EQ(0, fclose(fp));
1612
1613 // "...and for modes a and a+ the initial size shall be:
1614 // 1. Zero, if buf is a null pointer
1615 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1616 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1617 EXPECT_EQ(0, ftell(fp));
1618 EXPECT_EQ(0, ftello(fp));
1619 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1620 EXPECT_EQ(0, ftell(fp));
1621 EXPECT_EQ(0, ftello(fp));
1622 ASSERT_EQ(0, fclose(fp));
1623 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1624 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1625 EXPECT_EQ(0, ftell(fp));
1626 EXPECT_EQ(0, ftello(fp));
1627 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1628 EXPECT_EQ(0, ftell(fp));
1629 EXPECT_EQ(0, ftello(fp));
1630 ASSERT_EQ(0, fclose(fp));
1631
1632 // 2. The position of the first null byte in the buffer, if one is found
1633 memset(buf, 'x', sizeof(buf));
1634 buf[3] = '\0';
1635 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1636 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1637 EXPECT_EQ(3, ftell(fp));
1638 EXPECT_EQ(3, ftello(fp));
1639 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1640 EXPECT_EQ(3, ftell(fp));
1641 EXPECT_EQ(3, ftello(fp));
1642 ASSERT_EQ(0, fclose(fp));
1643 memset(buf, 'x', sizeof(buf));
1644 buf[3] = '\0';
1645 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1646 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1647 EXPECT_EQ(3, ftell(fp));
1648 EXPECT_EQ(3, ftello(fp));
1649 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1650 EXPECT_EQ(3, ftell(fp));
1651 EXPECT_EQ(3, ftello(fp));
1652 ASSERT_EQ(0, fclose(fp));
1653
1654 // 3. The value of the size argument, if buf is not a null pointer and no
1655 // null byte is found.
1656 memset(buf, 'x', sizeof(buf));
1657 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1658 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1659 EXPECT_EQ(16, ftell(fp));
1660 EXPECT_EQ(16, ftello(fp));
1661 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1662 EXPECT_EQ(16, ftell(fp));
1663 EXPECT_EQ(16, ftello(fp));
1664 ASSERT_EQ(0, fclose(fp));
1665 memset(buf, 'x', sizeof(buf));
1666 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1667 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1668 EXPECT_EQ(16, ftell(fp));
1669 EXPECT_EQ(16, ftello(fp));
1670 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1671 EXPECT_EQ(16, ftell(fp));
1672 EXPECT_EQ(16, ftello(fp));
1673 ASSERT_EQ(0, fclose(fp));
1674}
1675
1676TEST(STDIO_TEST, fmemopen_SEEK_END) {
1677 // fseek SEEK_END is relative to the current string length, not the buffer size.
1678 FILE* fp;
1679 char buf[8];
1680 memset(buf, 'x', sizeof(buf));
1681 strcpy(buf, "str");
1682 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1683 ASSERT_NE(EOF, fputs("string", fp));
1684 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1685 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1686 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1687 EXPECT_EQ(0, fclose(fp));
1688
1689 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1690 // than adding).
1691 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1692 ASSERT_NE(EOF, fputs("54321", fp));
1693 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1694 EXPECT_EQ('2', fgetc(fp));
1695 EXPECT_EQ(0, fclose(fp));
1696}
1697
1698TEST(STDIO_TEST, fmemopen_seek_invalid) {
1699 char buf[8];
1700 memset(buf, 'x', sizeof(buf));
1701 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1702 ASSERT_TRUE(fp != nullptr);
1703
1704 // POSIX: "An attempt to seek ... to a negative position or to a position
1705 // larger than the buffer size given in the size argument shall fail."
1706 // (There's no mention of what errno should be set to, and glibc doesn't
1707 // set errno in any of these cases.)
1708 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1709 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1710 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1711 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1712}
1713
1714TEST(STDIO_TEST, fmemopen_read_EOF) {
1715 // POSIX: "A read operation on the stream shall not advance the current
1716 // buffer position beyond the current buffer size."
1717 char buf[8];
1718 memset(buf, 'x', sizeof(buf));
1719 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1720 ASSERT_TRUE(fp != nullptr);
1721 char buf2[BUFSIZ];
1722 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1723 // POSIX: "Reaching the buffer size in a read operation shall count as
1724 // end-of-file.
1725 ASSERT_TRUE(feof(fp));
1726 ASSERT_EQ(EOF, fgetc(fp));
1727 ASSERT_EQ(0, fclose(fp));
1728}
1729
1730TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1731 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1732 char buf[] = "h\0e\0l\0l\0o";
1733 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1734 ASSERT_TRUE(fp != nullptr);
1735 ASSERT_EQ('h', fgetc(fp));
1736 ASSERT_EQ(0, fgetc(fp));
1737 ASSERT_EQ('e', fgetc(fp));
1738 ASSERT_EQ(0, fgetc(fp));
1739 ASSERT_EQ('l', fgetc(fp));
1740 ASSERT_EQ(0, fgetc(fp));
1741 // POSIX: "The read operation shall start at the current buffer position of
1742 // the stream."
1743 char buf2[8];
1744 memset(buf2, 'x', sizeof(buf2));
1745 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1746 ASSERT_EQ('l', buf2[0]);
1747 ASSERT_EQ(0, buf2[1]);
1748 ASSERT_EQ('o', buf2[2]);
1749 ASSERT_EQ(0, buf2[3]);
1750 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1751 ASSERT_TRUE(feof(fp));
1752 ASSERT_EQ(0, fclose(fp));
1753}
1754
1755TEST(STDIO_TEST, fmemopen_write) {
1756 FILE* fp;
1757 char buf[8];
1758
1759 // POSIX: "A write operation shall start either at the current position of
1760 // the stream (if mode has not specified 'a' as the first character)..."
1761 memset(buf, 'x', sizeof(buf));
1762 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1763 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1764 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1765 ASSERT_EQ(' ', fputc(' ', fp));
1766 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1767 ASSERT_EQ(0, fclose(fp));
1768
1769 // "...or at the current size of the stream (if mode had 'a' as the first
1770 // character)." (See the fmemopen_size test for what "size" means, but for
1771 // mode "a", it's the first NUL byte.)
1772 memset(buf, 'x', sizeof(buf));
1773 buf[3] = '\0';
1774 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1775 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1776 ASSERT_EQ(' ', fputc(' ', fp));
1777 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1778 ASSERT_EQ(0, fclose(fp));
1779
1780 // "If the current position at the end of the write is larger than the
1781 // current buffer size, the current buffer size shall be set to the current
1782 // position." (See the fmemopen_size test for what "size" means, but to
1783 // query it we SEEK_END with offset 0, and then ftell.)
1784 memset(buf, 'x', sizeof(buf));
1785 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1786 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1787 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1788 EXPECT_EQ(0, ftell(fp));
1789 ASSERT_EQ(' ', fputc(' ', fp));
1790 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1791 EXPECT_EQ(1, ftell(fp));
1792 ASSERT_NE(EOF, fputs("123", fp));
1793 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1794 EXPECT_EQ(4, ftell(fp));
1795 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1796 ASSERT_EQ(0, fclose(fp));
1797}
1798
1799TEST(STDIO_TEST, fmemopen_write_EOF) {
1800 // POSIX: "A write operation on the stream shall not advance the current
1801 // buffer size beyond the size given in the size argument."
1802 FILE* fp;
1803
1804 // Scalar writes...
1805 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1806 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1807 ASSERT_EQ('x', fputc('x', fp));
1808 ASSERT_EQ('x', fputc('x', fp));
1809 ASSERT_EQ('x', fputc('x', fp));
1810 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1811 ASSERT_EQ(0, fclose(fp));
1812
1813 // Vector writes...
1814 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1815 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1816 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1817 ASSERT_EQ(0, fclose(fp));
1818}
1819
1820TEST(STDIO_TEST, fmemopen_initial_position) {
1821 // POSIX: "The ... current position in the buffer ... shall be initially
1822 // set to either the beginning of the buffer (for r and w modes) ..."
1823 char buf[] = "hello\0world";
1824 FILE* fp;
1825 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1826 EXPECT_EQ(0L, ftell(fp));
1827 EXPECT_EQ(0, fclose(fp));
1828 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1829 EXPECT_EQ(0L, ftell(fp));
1830 EXPECT_EQ(0, fclose(fp));
1831 buf[0] = 'h'; // (Undo the effects of the above.)
1832
1833 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1834 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1835 EXPECT_EQ(5L, ftell(fp));
1836 EXPECT_EQ(0, fclose(fp));
1837
1838 // POSIX: "If no null byte is found in append mode, the initial position
1839 // shall be set to one byte after the end of the buffer."
1840 memset(buf, 'x', sizeof(buf));
1841 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1842 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1843 EXPECT_EQ(0, fclose(fp));
1844}
1845
1846TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1847 // POSIX: "If buf is a null pointer, the initial position shall always be
1848 // set to the beginning of the buffer."
1849 FILE* fp = fmemopen(nullptr, 128, "a+");
1850 ASSERT_TRUE(fp != nullptr);
1851 EXPECT_EQ(0L, ftell(fp));
1852 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1853 EXPECT_EQ(0, fclose(fp));
1854}
1855
1856TEST(STDIO_TEST, fmemopen_zero_length) {
1857 // POSIX says it's up to the implementation whether or not you can have a
1858 // zero-length buffer (but "A future version of this standard may require
1859 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1860 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1861 FILE* fp;
1862 char buf[16];
1863 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1864 ASSERT_EQ(EOF, fgetc(fp));
1865 ASSERT_TRUE(feof(fp));
1866 ASSERT_EQ(0, fclose(fp));
1867 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1868 ASSERT_EQ(EOF, fgetc(fp));
1869 ASSERT_TRUE(feof(fp));
1870 ASSERT_EQ(0, fclose(fp));
1871
1872 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1873 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1874 ASSERT_EQ(EOF, fputc('x', fp));
1875 ASSERT_EQ(0, fclose(fp));
1876 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1877 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1878 ASSERT_EQ(EOF, fputc('x', fp));
1879 ASSERT_EQ(0, fclose(fp));
1880}
1881
Elliott Hughes288465d2019-02-05 15:00:13 -08001882TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1883 char buf[2] = "x";
1884 ASSERT_EQ('x', buf[0]);
1885 FILE* fp = fmemopen(buf, 0, "w");
1886 ASSERT_EQ('x', buf[0]);
1887 ASSERT_EQ(0, fclose(fp));
1888}
1889
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001890TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1891 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1892 // BSD fails, glibc doesn't. We side with the more lenient.
1893 FILE* fp;
1894 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1895 ASSERT_EQ(0, fclose(fp));
1896 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1897 ASSERT_EQ(0, fclose(fp));
1898}
1899
1900TEST(STDIO_TEST, fmemopen_fileno) {
1901 // There's no fd backing an fmemopen FILE*.
1902 FILE* fp = fmemopen(nullptr, 16, "r");
1903 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001904 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001905 ASSERT_EQ(-1, fileno(fp));
1906 ASSERT_EQ(EBADF, errno);
1907 ASSERT_EQ(0, fclose(fp));
1908}
1909
1910TEST(STDIO_TEST, fmemopen_append_after_seek) {
1911 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1912 // there had been an intervening seek.
1913
1914 FILE* fp;
1915 char buf[] = "hello\0world";
1916 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1917 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1918 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1919 ASSERT_NE(EOF, fputc('!', fp));
1920 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1921 ASSERT_EQ(0, fclose(fp));
1922
1923 memcpy(buf, "hello\0world", sizeof(buf));
1924 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1925 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1926 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1927 ASSERT_NE(EOF, fputc('!', fp));
1928 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1929 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001930}
1931
Christopher Ferris13f26a72016-01-13 13:47:58 -08001932TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001933 char* p = nullptr;
1934 size_t size = 0;
1935 FILE* fp = open_memstream(&p, &size);
1936 ASSERT_NE(EOF, fputs("hello, world!", fp));
1937 fclose(fp);
1938
1939 ASSERT_STREQ("hello, world!", p);
1940 ASSERT_EQ(strlen("hello, world!"), size);
1941 free(p);
1942}
1943
Christopher Ferris13f26a72016-01-13 13:47:58 -08001944TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001945#if defined(__BIONIC__)
1946 char* p;
1947 size_t size;
1948
1949 // Invalid buffer.
1950 errno = 0;
1951 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1952 ASSERT_EQ(EINVAL, errno);
1953
1954 // Invalid size.
1955 errno = 0;
1956 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1957 ASSERT_EQ(EINVAL, errno);
1958#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001959 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001960#endif
1961}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001962
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001963TEST(STDIO_TEST, fdopen_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001964 // This fd doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001965 int fd = open("/proc/version", O_RDONLY);
1966 ASSERT_FALSE(CloseOnExec(fd));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001967 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001968 FILE* fp = fdopen(fd, "re");
1969 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1970 fclose(fp);
1971}
1972
1973TEST(STDIO_TEST, fdopen_remove_CLOEXEC) {
1974 // This fd has O_CLOEXEC...
1975 int fd = open("/proc/version", O_RDONLY | O_CLOEXEC);
1976 ASSERT_TRUE(CloseOnExec(fd));
1977 // ...but the new one doesn't.
1978 FILE* fp = fdopen(fd, "r");
1979 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1980 fclose(fp);
1981}
1982
1983TEST(STDIO_TEST, freopen_add_CLOEXEC) {
1984 // This FILE* doesn't have O_CLOEXEC...
1985 FILE* fp = fopen("/proc/version", "r");
1986 ASSERT_FALSE(CloseOnExec(fileno(fp)));
1987 // ...but the new one does.
1988 fp = freopen("/proc/version", "re", fp);
1989 ASSERT_TRUE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07001990
1991 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001992}
1993
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08001994TEST(STDIO_TEST, freopen_remove_CLOEXEC) {
1995 // This FILE* has O_CLOEXEC...
1996 FILE* fp = fopen("/proc/version", "re");
1997 ASSERT_TRUE(CloseOnExec(fileno(fp)));
1998 // ...but the new one doesn't.
1999 fp = freopen("/proc/version", "r", fp);
2000 ASSERT_FALSE(CloseOnExec(fileno(fp)));
2001 fclose(fp);
2002}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002003
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002004TEST(STDIO_TEST, freopen_null_filename_add_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07002005 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002006 FILE* fp = fopen("/proc/version", "r");
2007 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002008 // ...but the new one does.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002009 fp = freopen(nullptr, "re", fp);
2010 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2011 fclose(fp);
2012}
Elliott Hughes31165ed2014-09-23 17:34:29 -07002013
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002014TEST(STDIO_TEST, freopen_null_filename_remove_CLOEXEC) {
2015 // This FILE* has O_CLOEXEC...
2016 FILE* fp = fopen("/proc/version", "re");
2017 ASSERT_TRUE(CloseOnExec(fileno(fp)));
2018 // ...but the new one doesn't.
2019 fp = freopen(nullptr, "r", fp);
2020 ASSERT_FALSE(CloseOnExec(fileno(fp)));
Elliott Hughes31165ed2014-09-23 17:34:29 -07002021 fclose(fp);
2022}
Elliott Hughes20841a12014-12-01 16:13:30 -08002023
Elliott Hughesf226ee52016-02-03 11:24:28 -08002024TEST(STDIO_TEST, fopen64_freopen64) {
2025 FILE* fp = fopen64("/proc/version", "r");
2026 ASSERT_TRUE(fp != nullptr);
2027 fp = freopen64("/proc/version", "re", fp);
2028 ASSERT_TRUE(fp != nullptr);
2029 fclose(fp);
2030}
2031
Elliott Hughes20841a12014-12-01 16:13:30 -08002032// https://code.google.com/p/android/issues/detail?id=81155
2033// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08002034TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08002035 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07002036 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002037
2038 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07002039 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08002040
2041 char buf[65*1024];
2042 memset(buf, 0xff, sizeof(buf));
2043
Yi Kong32bc0fc2018-08-02 17:31:13 -07002044 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002045 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002046 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08002047 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07002048 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08002049
2050 fclose(fp);
2051
2052 // 1024 64KiB reads should have been very quick.
2053 ASSERT_LE(t1 - t0, 1);
2054
2055 for (size_t i = 0; i < 64*1024; ++i) {
2056 ASSERT_EQ('\0', buf[i]);
2057 }
2058 for (size_t i = 64*1024; i < 65*1024; ++i) {
2059 ASSERT_EQ('\xff', buf[i]);
2060 }
2061}
Elliott Hughes75b99382015-01-20 11:23:50 -08002062
Christopher Ferris13f26a72016-01-13 13:47:58 -08002063TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002064 std::string digits("0123456789");
2065 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08002066
2067 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
2068 char buf1[4 * 4];
2069 memset(buf1, 0, sizeof(buf1));
2070 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002071 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08002072 ASSERT_TRUE(feof(fp));
2073
2074 rewind(fp);
2075
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002076 // Try to read way too much so stdio tries to read more direct from the stream.
2077 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08002078 memset(buf2, 0, sizeof(buf2));
2079 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002080 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002081 ASSERT_TRUE(feof(fp));
2082
2083 fclose(fp);
2084}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002085
2086static void test_fread_from_write_only_stream(size_t n) {
2087 FILE* fp = fopen("/dev/null", "w");
2088 std::vector<char> buf(n, 0);
2089 errno = 0;
2090 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2091 ASSERT_EQ(EBADF, errno);
2092 ASSERT_TRUE(ferror(fp));
2093 ASSERT_FALSE(feof(fp));
2094 fclose(fp);
2095}
2096
Christopher Ferris13f26a72016-01-13 13:47:58 -08002097TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002098 test_fread_from_write_only_stream(1);
2099}
2100
Christopher Ferris13f26a72016-01-13 13:47:58 -08002101TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002102 test_fread_from_write_only_stream(64*1024);
2103}
2104
2105static void test_fwrite_after_fread(size_t n) {
2106 TemporaryFile tf;
2107
2108 FILE* fp = fdopen(tf.fd, "w+");
2109 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2110 fflush(fp);
2111
2112 // We've flushed but not rewound, so there's nothing to read.
2113 std::vector<char> buf(n, 0);
2114 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2115 ASSERT_TRUE(feof(fp));
2116
2117 // But hitting EOF doesn't prevent us from writing...
2118 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002119 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002120
2121 // And if we rewind, everything's there.
2122 rewind(fp);
2123 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2124 ASSERT_EQ('1', buf[0]);
2125 ASSERT_EQ('2', buf[1]);
2126
2127 fclose(fp);
2128}
2129
Christopher Ferris13f26a72016-01-13 13:47:58 -08002130TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002131 test_fwrite_after_fread(16);
2132}
2133
Christopher Ferris13f26a72016-01-13 13:47:58 -08002134TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002135 test_fwrite_after_fread(64*1024);
2136}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002137
2138// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002139TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002140 TemporaryFile tf;
2141
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002142 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002143 ASSERT_TRUE(fp != nullptr);
2144
2145 char file_data[12288];
2146 for (size_t i = 0; i < 12288; i++) {
2147 file_data[i] = i;
2148 }
2149 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2150 fclose(fp);
2151
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002152 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002153 ASSERT_TRUE(fp != nullptr);
2154
2155 char buffer[8192];
2156 size_t cur_location = 0;
2157 // Small read to populate internal buffer.
2158 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2159 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2160
2161 cur_location = static_cast<size_t>(ftell(fp));
2162 // Large read to force reading into the user supplied buffer and bypassing
2163 // the internal buffer.
2164 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2165 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2166
2167 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002168 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002169 cur_location = static_cast<size_t>(ftell(fp));
2170 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2171 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2172
2173 fclose(fp);
2174}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002175
2176// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002177TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002178 TemporaryFile tf;
2179 char buf[6] = {0};
2180
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002181 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002182 ASSERT_TRUE(fw != nullptr);
2183
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002184 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002185 ASSERT_TRUE(fr != nullptr);
2186
2187 fwrite("a", 1, 1, fw);
2188 fflush(fw);
2189 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2190 ASSERT_STREQ("a", buf);
2191
2192 // 'fr' is now at EOF.
2193 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2194 ASSERT_TRUE(feof(fr));
2195
2196 // Write some more...
2197 fwrite("z", 1, 1, fw);
2198 fflush(fw);
2199
2200 // ...and check that we can read it back.
2201 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2202 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2203 ASSERT_STREQ("z", buf);
2204
2205 // But now we're done.
2206 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2207
2208 fclose(fr);
2209 fclose(fw);
2210}
Elliott Hughes923f1652016-01-19 15:46:05 -08002211
2212TEST(STDIO_TEST, fclose_invalidates_fd) {
2213 // The typical error we're trying to help people catch involves accessing
2214 // memory after it's been freed. But we know that stdin/stdout/stderr are
2215 // special and don't get deallocated, so this test uses stdin.
2216 ASSERT_EQ(0, fclose(stdin));
2217
2218 // Even though using a FILE* after close is undefined behavior, I've closed
2219 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2220 // especially because they might actually correspond to a real stream.
2221 errno = 0;
2222 ASSERT_EQ(-1, fileno(stdin));
2223 ASSERT_EQ(EBADF, errno);
2224}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002225
2226TEST(STDIO_TEST, fseek_ftell_unseekable) {
2227#if defined(__BIONIC__) // glibc has fopencookie instead.
2228 auto read_fn = [](void*, char*, int) { return -1; };
2229 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2230 ASSERT_TRUE(fp != nullptr);
2231
2232 // Check that ftell balks on an unseekable FILE*.
2233 errno = 0;
2234 ASSERT_EQ(-1, ftell(fp));
2235 ASSERT_EQ(ESPIPE, errno);
2236
2237 // SEEK_CUR is rewritten as SEEK_SET internally...
2238 errno = 0;
2239 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2240 ASSERT_EQ(ESPIPE, errno);
2241
2242 // ...so it's worth testing the direct seek path too.
2243 errno = 0;
2244 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2245 ASSERT_EQ(ESPIPE, errno);
2246
2247 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002248#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002249 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002250#endif
2251}
2252
2253TEST(STDIO_TEST, funopen_EINVAL) {
2254#if defined(__BIONIC__)
2255 errno = 0;
2256 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2257 ASSERT_EQ(EINVAL, errno);
2258#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002259 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002260#endif
2261}
2262
2263TEST(STDIO_TEST, funopen_seek) {
2264#if defined(__BIONIC__)
2265 auto read_fn = [](void*, char*, int) { return -1; };
2266
2267 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2268 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2269
2270 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2271 ASSERT_TRUE(fp != nullptr);
2272 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002273#if defined(__LP64__)
2274 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2275 EXPECT_EQ(0xfedcba12LL, pos);
2276#else
2277 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2278 EXPECT_EQ(EOVERFLOW, errno);
2279#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002280
2281 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2282 ASSERT_TRUE(fp64 != nullptr);
2283 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002284 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2285 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002286#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002287 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002288#endif
2289}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002290
2291TEST(STDIO_TEST, lots_of_concurrent_files) {
2292 std::vector<TemporaryFile*> tfs;
2293 std::vector<FILE*> fps;
2294
2295 for (size_t i = 0; i < 256; ++i) {
2296 TemporaryFile* tf = new TemporaryFile;
2297 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002298 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002299 fps.push_back(fp);
2300 fprintf(fp, "hello %zu!\n", i);
2301 fflush(fp);
2302 }
2303
2304 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002305 char expected[BUFSIZ];
2306 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002307
Elliott Hughes70715da2016-08-01 16:35:17 -07002308 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002309 fclose(fps[i]);
2310 delete tfs[i];
2311 }
2312}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002313
2314static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2315 EXPECT_EQ(offset, ftell(fp));
2316 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002317 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002318 fpos_t pos;
2319 fpos64_t pos64;
2320 EXPECT_EQ(0, fgetpos(fp, &pos));
2321 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2322#if defined(__BIONIC__)
2323 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2324 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2325#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002326 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002327#endif
2328}
2329
2330TEST(STDIO_TEST, seek_tell_family_smoke) {
2331 TemporaryFile tf;
2332 FILE* fp = fdopen(tf.fd, "w+");
2333
2334 // Initially we should be at 0.
2335 AssertFileOffsetAt(fp, 0);
2336
2337 // Seek to offset 8192.
2338 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2339 AssertFileOffsetAt(fp, 8192);
2340 fpos_t eight_k_pos;
2341 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2342
2343 // Seek forward another 8192...
2344 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2345 AssertFileOffsetAt(fp, 8192 + 8192);
2346 fpos64_t sixteen_k_pos64;
2347 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2348
2349 // Seek back 8192...
2350 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2351 AssertFileOffsetAt(fp, 8192);
2352
2353 // Since we haven't written anything, the end is also at 0.
2354 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2355 AssertFileOffsetAt(fp, 0);
2356
2357 // Check that our fpos64_t from 16KiB works...
2358 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2359 AssertFileOffsetAt(fp, 8192 + 8192);
2360 // ...as does our fpos_t from 8192.
2361 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2362 AssertFileOffsetAt(fp, 8192);
2363
2364 // Do fseeko and fseeko64 work too?
2365 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2366 AssertFileOffsetAt(fp, 1234);
2367 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2368 AssertFileOffsetAt(fp, 5678);
2369
2370 fclose(fp);
2371}
2372
2373TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2374 TemporaryFile tf;
2375 FILE* fp = fdopen(tf.fd, "w+");
2376
2377 // Bad whence.
2378 errno = 0;
2379 ASSERT_EQ(-1, fseek(fp, 0, 123));
2380 ASSERT_EQ(EINVAL, errno);
2381 errno = 0;
2382 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2383 ASSERT_EQ(EINVAL, errno);
2384 errno = 0;
2385 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2386 ASSERT_EQ(EINVAL, errno);
2387
2388 // Bad offset.
2389 errno = 0;
2390 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2391 ASSERT_EQ(EINVAL, errno);
2392 errno = 0;
2393 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2394 ASSERT_EQ(EINVAL, errno);
2395 errno = 0;
2396 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2397 ASSERT_EQ(EINVAL, errno);
2398
2399 fclose(fp);
2400}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002401
2402TEST(STDIO_TEST, ctermid) {
2403 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2404
2405 char buf[L_ctermid] = {};
2406 ASSERT_EQ(buf, ctermid(buf));
2407 ASSERT_STREQ("/dev/tty", buf);
2408}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002409
2410TEST(STDIO_TEST, remove) {
2411 struct stat sb;
2412
2413 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002414 ASSERT_EQ(0, remove(tf.path));
2415 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002416 ASSERT_EQ(ENOENT, errno);
2417
2418 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002419 ASSERT_EQ(0, remove(td.path));
2420 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002421 ASSERT_EQ(ENOENT, errno);
2422
2423 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002424 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002425 ASSERT_EQ(ENOENT, errno);
2426
2427 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002428 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002429 ASSERT_EQ(ENOENT, errno);
2430}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002431
Elliott Hughese657eb42021-02-18 17:11:56 -08002432TEST_F(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002433 char buf[16];
2434 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2435 testing::KilledBySignal(SIGABRT),
2436#if defined(NOFORTIFY)
2437 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2438#else
2439 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2440#endif
2441 );
2442}
2443
Elliott Hughese657eb42021-02-18 17:11:56 -08002444TEST_F(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002445 std::string buf = "world";
2446 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2447 testing::KilledBySignal(SIGABRT),
2448 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2449}
2450
2451TEST(STDIO_TEST, sprintf_30445072) {
2452 std::string buf = "world";
2453 sprintf(&buf[0], "hello");
2454 ASSERT_EQ(buf, "hello");
2455}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002456
Elliott Hughes654cd832018-08-30 16:00:42 -07002457TEST(STDIO_TEST, printf_m) {
2458 char buf[BUFSIZ];
2459 errno = 0;
2460 snprintf(buf, sizeof(buf), "<%m>");
2461 ASSERT_STREQ("<Success>", buf);
2462 errno = -1;
2463 snprintf(buf, sizeof(buf), "<%m>");
2464 ASSERT_STREQ("<Unknown error -1>", buf);
2465 errno = EINVAL;
2466 snprintf(buf, sizeof(buf), "<%m>");
Steven Moreland4ef83d62021-10-07 00:19:18 +00002467 ASSERT_STREQ("<Invalid argument>", buf);
Elliott Hughes654cd832018-08-30 16:00:42 -07002468}
2469
Elliott Hughesf340a562018-09-06 10:42:40 -07002470TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2471 char buf[BUFSIZ];
2472 const char* m = strerror(-1);
2473 ASSERT_STREQ("Unknown error -1", m);
2474 errno = -2;
2475 snprintf(buf, sizeof(buf), "<%m>");
2476 ASSERT_STREQ("<Unknown error -2>", buf);
2477 ASSERT_STREQ("Unknown error -1", m);
2478}
2479
Elliott Hughes654cd832018-08-30 16:00:42 -07002480TEST(STDIO_TEST, wprintf_m) {
2481 wchar_t buf[BUFSIZ];
2482 errno = 0;
2483 swprintf(buf, sizeof(buf), L"<%m>");
2484 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2485 errno = -1;
2486 swprintf(buf, sizeof(buf), L"<%m>");
2487 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2488 errno = EINVAL;
2489 swprintf(buf, sizeof(buf), L"<%m>");
Steven Moreland4ef83d62021-10-07 00:19:18 +00002490 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
Elliott Hughes654cd832018-08-30 16:00:42 -07002491}
2492
Elliott Hughesf340a562018-09-06 10:42:40 -07002493TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2494 wchar_t buf[BUFSIZ];
2495 const char* m = strerror(-1);
2496 ASSERT_STREQ("Unknown error -1", m);
2497 errno = -2;
2498 swprintf(buf, sizeof(buf), L"<%m>");
2499 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2500 ASSERT_STREQ("Unknown error -1", m);
2501}
2502
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002503TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2504 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002505 SetFileTo(tf.path, "0123456789");
2506 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002507 EXPECT_EQ(10, ftell(fp));
2508 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2509 EXPECT_EQ(2, ftell(fp));
2510 ASSERT_NE(EOF, fputs("xxx", fp));
2511 ASSERT_EQ(0, fflush(fp));
2512 EXPECT_EQ(13, ftell(fp));
2513 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2514 EXPECT_EQ(13, ftell(fp));
2515 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002516 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002517}
2518
2519TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2520 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002521 SetFileTo(tf.path, "0123456789");
2522 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002523 ASSERT_NE(-1, fd);
2524 // POSIX: "The file position indicator associated with the new stream is set to the position
2525 // indicated by the file offset associated with the file descriptor."
2526 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2527 FILE* fp = fdopen(fd, "a");
2528 EXPECT_EQ(4, ftell(fp));
2529 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2530 EXPECT_EQ(2, ftell(fp));
2531 ASSERT_NE(EOF, fputs("xxx", fp));
2532 ASSERT_EQ(0, fflush(fp));
2533 EXPECT_EQ(13, ftell(fp));
2534 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2535 EXPECT_EQ(13, ftell(fp));
2536 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002537 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002538}
2539
2540TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2541 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002542 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002543 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002544 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002545 EXPECT_EQ(10, ftell(fp));
2546 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2547 EXPECT_EQ(2, ftell(fp));
2548 ASSERT_NE(EOF, fputs("xxx", fp));
2549 ASSERT_EQ(0, fflush(fp));
2550 EXPECT_EQ(13, ftell(fp));
2551 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2552 EXPECT_EQ(13, ftell(fp));
2553 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002554 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002555}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002556
2557TEST(STDIO_TEST, constants) {
2558 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2559 ASSERT_EQ(L_tmpnam, PATH_MAX);
2560}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002561
2562TEST(STDIO_TEST, perror) {
2563 ExecTestHelper eth;
Steven Moreland4ef83d62021-10-07 00:19:18 +00002564 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2565 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2566 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002567}
2568
2569TEST(STDIO_TEST, puts) {
2570 ExecTestHelper eth;
2571 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2572}
2573
Elliott Hughes7cebf832020-08-12 14:25:41 -07002574TEST(STDIO_TEST, putchar) {
2575 ExecTestHelper eth;
2576 eth.Run([&]() { exit(putchar('A')); }, 65, "A");
2577}
2578
2579TEST(STDIO_TEST, putchar_unlocked) {
2580 ExecTestHelper eth;
2581 eth.Run([&]() { exit(putchar('B')); }, 66, "B");
2582}
2583
Elliott Hughes37ad9592017-10-30 17:47:12 -07002584TEST(STDIO_TEST, unlocked) {
2585 TemporaryFile tf;
2586
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002587 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002588 ASSERT_TRUE(fp != nullptr);
2589
2590 clearerr_unlocked(fp);
2591 ASSERT_FALSE(feof_unlocked(fp));
2592 ASSERT_FALSE(ferror_unlocked(fp));
2593
2594 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2595
2596 ASSERT_NE(EOF, putc_unlocked('a', fp));
2597 ASSERT_NE(EOF, putc('b', fp));
2598 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2599 ASSERT_NE(EOF, fputc('d', fp));
2600
2601 rewind(fp);
2602 ASSERT_EQ('a', getc_unlocked(fp));
2603 ASSERT_EQ('b', getc(fp));
2604 ASSERT_EQ('c', fgetc_unlocked(fp));
2605 ASSERT_EQ('d', fgetc(fp));
2606
2607 rewind(fp);
2608 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2609 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2610 ASSERT_EQ(0, fflush_unlocked(fp));
2611
2612 rewind(fp);
2613 char buf[BUFSIZ] = {};
2614 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2615 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2616 ASSERT_STREQ("ABCD", buf);
2617
2618 rewind(fp);
2619 ASSERT_NE(EOF, fputs("hello ", fp));
2620 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2621 ASSERT_NE(EOF, fputc('\n', fp));
2622
2623 rewind(fp);
2624 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2625 ASSERT_STREQ("hello world\n", buf);
2626
2627 ASSERT_EQ(0, fclose(fp));
2628}
Ryan Prichardbf549862017-11-07 15:30:32 -08002629
2630TEST(STDIO_TEST, fseek_64bit) {
2631 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002632 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002633 ASSERT_TRUE(fp != nullptr);
2634 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2635 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2636 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2637 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2638 ASSERT_EQ(0, fclose(fp));
2639}
2640
2641// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2642// isn't representable in long/off_t.
2643TEST(STDIO_TEST, fseek_overflow_32bit) {
2644 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002645 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002646 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2647
2648 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2649#if defined(__BIONIC__) && !defined(__LP64__)
2650 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2651 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2652 ASSERT_EQ(EOVERFLOW, errno);
2653#endif
2654
2655 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2656 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2657 // and SEEK_END -- many C libraries check neither.)
2658 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2659 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2660
2661 fclose(fp);
2662}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002663
2664TEST(STDIO_TEST, dev_std_files) {
2665 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2666 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002667 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2668 ASSERT_LT(0, length);
2669 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2670
2671 length = readlink("/dev/stdout", path, sizeof(path));
2672 ASSERT_LT(0, length);
2673 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2674
2675 length = readlink("/dev/stderr", path, sizeof(path));
2676 ASSERT_LT(0, length);
2677 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002678}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002679
2680TEST(STDIO_TEST, fread_with_locked_file) {
2681 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2682 // files locked on other threads, even if it flushes some line-buffered files.
2683 FILE* fp1 = fopen("/dev/zero", "r");
2684 ASSERT_TRUE(fp1 != nullptr);
2685 flockfile(fp1);
2686
2687 std::thread([] {
2688 for (int mode : { _IONBF, _IOLBF }) {
2689 FILE* fp2 = fopen("/dev/zero", "r");
2690 ASSERT_TRUE(fp2 != nullptr);
2691 setvbuf(fp2, nullptr, mode, 0);
2692 ASSERT_EQ('\0', fgetc(fp2));
2693 fclose(fp2);
2694 }
2695 }).join();
2696
2697 funlockfile(fp1);
2698 fclose(fp1);
2699}
Elliott Hughes31c73092019-05-07 10:03:02 -07002700
2701TEST(STDIO_TEST, SEEK_macros) {
2702 ASSERT_EQ(0, SEEK_SET);
2703 ASSERT_EQ(1, SEEK_CUR);
2704 ASSERT_EQ(2, SEEK_END);
2705 ASSERT_EQ(3, SEEK_DATA);
2706 ASSERT_EQ(4, SEEK_HOLE);
2707 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2708 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2709}
Elliott Hughes05b675e2019-04-17 13:01:06 -07002710
2711TEST(STDIO_TEST, rename) {
2712 TemporaryDir td;
2713 std::string old_path = td.path + "/old"s;
2714 std::string new_path = td.path + "/new"s;
2715
2716 // Create the file, check it exists.
2717 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2718 struct stat sb;
2719 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2720 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2721
2722 // Rename and check it moved.
2723 ASSERT_EQ(0, rename(old_path.c_str(), new_path.c_str()));
2724 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2725 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2726}
2727
2728TEST(STDIO_TEST, renameat) {
2729 TemporaryDir td;
2730 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2731 std::string old_path = td.path + "/old"s;
2732 std::string new_path = td.path + "/new"s;
2733
2734 // Create the file, check it exists.
2735 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2736 struct stat sb;
2737 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2738 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2739
2740 // Rename and check it moved.
2741 ASSERT_EQ(0, renameat(dirfd, "old", dirfd, "new"));
2742 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2743 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2744}
2745
2746TEST(STDIO_TEST, renameat2) {
Colin Cross4c5595c2021-08-16 15:51:59 -07002747#if defined(__GLIBC__) || defined(ANDROID_HOST_MUSL)
Colin Cross7da20342021-07-28 11:18:11 -07002748 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28 and musl doesn't have renameat2";
Elliott Hughes05b675e2019-04-17 13:01:06 -07002749#else
2750 TemporaryDir td;
2751 android::base::unique_fd dirfd{open(td.path, O_PATH)};
2752 std::string old_path = td.path + "/old"s;
2753 std::string new_path = td.path + "/new"s;
2754
2755 // Create the file, check it exists.
2756 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2757 struct stat sb;
2758 ASSERT_EQ(0, stat(old_path.c_str(), &sb));
2759 ASSERT_EQ(-1, stat(new_path.c_str(), &sb));
2760
2761 // Rename and check it moved.
2762 ASSERT_EQ(0, renameat2(dirfd, "old", dirfd, "new", 0));
2763 ASSERT_EQ(-1, stat(old_path.c_str(), &sb));
2764 ASSERT_EQ(0, stat(new_path.c_str(), &sb));
2765
2766 // After this, both "old" and "new" exist.
2767 ASSERT_EQ(0, close(creat(old_path.c_str(), 0666)));
2768
2769 // Rename and check it moved.
2770 ASSERT_EQ(-1, renameat2(dirfd, "old", dirfd, "new", RENAME_NOREPLACE));
2771 ASSERT_EQ(EEXIST, errno);
2772#endif
2773}
2774
2775TEST(STDIO_TEST, renameat2_flags) {
2776#if defined(__GLIBC__)
2777 GTEST_SKIP() << "glibc doesn't have renameat2 until 2.28";
2778#else
2779 ASSERT_NE(0, RENAME_EXCHANGE);
2780 ASSERT_NE(0, RENAME_NOREPLACE);
2781 ASSERT_NE(0, RENAME_WHITEOUT);
2782#endif
2783}
Elliott Hughes7cebf832020-08-12 14:25:41 -07002784
2785TEST(STDIO_TEST, fdopen_failures) {
2786 FILE* fp;
2787 int fd = open("/proc/version", O_RDONLY);
2788 ASSERT_TRUE(fd != -1);
2789
2790 // Nonsense mode.
2791 errno = 0;
2792 fp = fdopen(fd, "nonsense");
2793 ASSERT_TRUE(fp == nullptr);
2794 ASSERT_EQ(EINVAL, errno);
2795
2796 // Mode that isn't a subset of the fd's actual mode.
2797 errno = 0;
2798 fp = fdopen(fd, "w");
2799 ASSERT_TRUE(fp == nullptr);
2800 ASSERT_EQ(EINVAL, errno);
2801
2802 // Can't set append on the underlying fd.
2803 errno = 0;
2804 fp = fdopen(fd, "a");
2805 ASSERT_TRUE(fp == nullptr);
2806 ASSERT_EQ(EINVAL, errno);
2807
2808 // Bad fd.
2809 errno = 0;
2810 fp = fdopen(-1, "re");
2811 ASSERT_TRUE(fp == nullptr);
2812 ASSERT_EQ(EBADF, errno);
2813
2814 close(fd);
2815}
2816
2817TEST(STDIO_TEST, fmemopen_invalid_mode) {
2818 errno = 0;
2819 FILE* fp = fmemopen(nullptr, 16, "nonsense");
2820 ASSERT_TRUE(fp == nullptr);
2821 ASSERT_EQ(EINVAL, errno);
2822}
2823
2824TEST(STDIO_TEST, fopen_invalid_mode) {
2825 errno = 0;
2826 FILE* fp = fopen("/proc/version", "nonsense");
2827 ASSERT_TRUE(fp == nullptr);
2828 ASSERT_EQ(EINVAL, errno);
2829}
2830
2831TEST(STDIO_TEST, freopen_invalid_mode) {
2832 FILE* fp = fopen("/proc/version", "re");
2833 ASSERT_TRUE(fp != nullptr);
2834
2835 errno = 0;
2836 fp = freopen("/proc/version", "nonsense", fp);
2837 ASSERT_TRUE(fp == nullptr);
2838 ASSERT_EQ(EINVAL, errno);
2839}
2840
2841TEST(STDIO_TEST, asprintf_smoke) {
2842 char* p = nullptr;
2843 ASSERT_EQ(11, asprintf(&p, "hello %s", "world"));
2844 ASSERT_STREQ("hello world", p);
2845 free(p);
2846}
2847
2848TEST(STDIO_TEST, fopen_ENOENT) {
2849 errno = 0;
2850 FILE* fp = fopen("/proc/does-not-exist", "re");
2851 ASSERT_TRUE(fp == nullptr);
2852 ASSERT_EQ(ENOENT, errno);
2853}
Elliott Hughes439ebbd2020-12-04 18:51:42 -08002854
2855static void tempnam_test(bool has_TMPDIR, const char* dir, const char* prefix, const char* re) {
2856 if (has_TMPDIR) {
2857 setenv("TMPDIR", "/my/tmp/dir", 1);
2858 } else {
2859 unsetenv("TMPDIR");
2860 }
2861 char* s1 = tempnam(dir, prefix);
2862 char* s2 = tempnam(dir, prefix);
2863 ASSERT_MATCH(s1, re);
2864 ASSERT_MATCH(s2, re);
2865 ASSERT_STRNE(s1, s2);
2866 free(s1);
2867 free(s2);
2868}
2869
2870TEST(STDIO_TEST, tempnam__system_directory_system_prefix_with_TMPDIR) {
2871 tempnam_test(true, nullptr, nullptr, "^/my/tmp/dir/.*");
2872}
2873
2874TEST(STDIO_TEST, tempnam__system_directory_system_prefix_without_TMPDIR) {
2875 tempnam_test(false, nullptr, nullptr, "^/data/local/tmp/.*");
2876}
2877
2878TEST(STDIO_TEST, tempnam__system_directory_user_prefix_with_TMPDIR) {
2879 tempnam_test(true, nullptr, "prefix", "^/my/tmp/dir/prefix.*");
2880}
2881
2882TEST(STDIO_TEST, tempnam__system_directory_user_prefix_without_TMPDIR) {
2883 tempnam_test(false, nullptr, "prefix", "^/data/local/tmp/prefix.*");
2884}
2885
2886TEST(STDIO_TEST, tempnam__user_directory_system_prefix_with_TMPDIR) {
2887 tempnam_test(true, "/a/b/c", nullptr, "^/my/tmp/dir/.*");
2888}
2889
2890TEST(STDIO_TEST, tempnam__user_directory_system_prefix_without_TMPDIR) {
2891 tempnam_test(false, "/a/b/c", nullptr, "^/a/b/c/.*");
2892}
2893
2894TEST(STDIO_TEST, tempnam__user_directory_user_prefix_with_TMPDIR) {
2895 tempnam_test(true, "/a/b/c", "prefix", "^/my/tmp/dir/prefix.*");
2896}
2897
2898TEST(STDIO_TEST, tempnam__user_directory_user_prefix_without_TMPDIR) {
2899 tempnam_test(false, "/a/b/c", "prefix", "^/a/b/c/prefix.*");
2900}
2901
2902static void tmpnam_test(char* s) {
2903 char s1[L_tmpnam], s2[L_tmpnam];
2904
2905 strcpy(s1, tmpnam(s));
2906 strcpy(s2, tmpnam(s));
2907 ASSERT_MATCH(s1, "/tmp/.*");
2908 ASSERT_MATCH(s2, "/tmp/.*");
2909 ASSERT_STRNE(s1, s2);
2910}
2911
2912TEST(STDIO_TEST, tmpnam) {
2913 tmpnam_test(nullptr);
2914}
2915
2916TEST(STDIO_TEST, tmpnam_buf) {
2917 char buf[L_tmpnam];
2918 tmpnam_test(buf);
2919}
Elliott Hughesf9cfecf2021-02-04 16:58:13 -08002920
2921TEST(STDIO_TEST, freopen_null_filename_mode) {
2922 TemporaryFile tf;
2923 FILE* fp = fopen(tf.path, "r");
2924 ASSERT_TRUE(fp != nullptr);
2925
2926 // "r" = O_RDONLY
2927 char buf[1];
2928 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2929 ASSERT_EQ(-1, write(fileno(fp), "hello", 1));
2930 // "r+" = O_RDWR
2931 fp = freopen(nullptr, "r+", fp);
2932 ASSERT_EQ(0, read(fileno(fp), buf, 1));
2933 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2934 // "w" = O_WRONLY
2935 fp = freopen(nullptr, "w", fp);
2936 ASSERT_EQ(-1, read(fileno(fp), buf, 1));
2937 ASSERT_EQ(1, write(fileno(fp), "hello", 1));
2938
2939 fclose(fp);
2940}