blob: 85147e850d1d24b5769e8d088bd0c36ca6dade14 [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>
Elliott Hughes7823f322014-04-14 12:11:28 -070022#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070023#include <stdio.h>
24#include <sys/types.h>
Elliott Hughes468efc82018-07-10 14:39:49 -070025#include <sys/socket.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070026#include <sys/stat.h>
27#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070028#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010029#include <locale.h>
30
Elliott Hughes3a4c4542017-07-19 17:20:24 -070031#include <string>
Ryan Prichardc8e263b2019-04-30 14:47:34 -070032#include <thread>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080033#include <vector>
34
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080035#include <android-base/file.h>
36
Elliott Hughesfb3873d2016-08-10 11:07:54 -070037#include "BionicDeathTest.h"
Josh Gao2f06e102017-01-10 13:00:37 -080038#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070039
Christopher Ferris13f26a72016-01-13 13:47:58 -080040#if defined(NOFORTIFY)
41#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070042#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080043#else
44#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070045#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080046#endif
47
Elliott Hughes3a4c4542017-07-19 17:20:24 -070048using namespace std::string_literals;
49
Elliott Hughesfb3873d2016-08-10 11:07:54 -070050class stdio_DeathTest : public BionicDeathTest {};
51class stdio_nofortify_DeathTest : public BionicDeathTest {};
52
Elliott Hughes33a8cb12017-07-25 18:06:46 -070053static void SetFileTo(const char* path, const char* content) {
54 FILE* fp;
55 ASSERT_NE(nullptr, fp = fopen(path, "w"));
56 ASSERT_NE(EOF, fputs(content, fp));
57 ASSERT_EQ(0, fclose(fp));
58}
59
60static void AssertFileIs(const char* path, const char* expected) {
61 FILE* fp;
62 ASSERT_NE(nullptr, fp = fopen(path, "r"));
63 char* line = nullptr;
64 size_t length;
65 ASSERT_NE(EOF, getline(&line, &length, fp));
66 ASSERT_EQ(0, fclose(fp));
67 ASSERT_STREQ(expected, line);
68 free(line);
69}
70
Elliott Hughes70715da2016-08-01 16:35:17 -070071static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
72 rewind(fp);
73
74 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080075 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070076 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
77 ASSERT_STREQ(expected, line);
78
79 if (is_fmemopen) {
80 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
81 // extra empty line, but does on every C library I tested...
82 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
83 ASSERT_STREQ("", line);
84 }
85
86 // Make sure there isn't anything else in the file.
87 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
88}
89
Christopher Ferris13f26a72016-01-13 13:47:58 -080090TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080091 // Check that we have a _recursive_ mutex for flockfile.
92 flockfile(stderr);
93 feof(stderr); // We don't care about the result, but this needs to take the lock.
94 funlockfile(stderr);
95}
96
Christopher Ferris13f26a72016-01-13 13:47:58 -080097TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080098 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
99 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700100 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800101 flockfile(fp);
102 feof(fp);
103 funlockfile(fp);
104 fclose(fp);
105}
106
Christopher Ferris13f26a72016-01-13 13:47:58 -0800107TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700108 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700109 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700110
111 int fd = fileno(fp);
112 ASSERT_NE(fd, -1);
113
114 struct stat sb;
115 int rc = fstat(fd, &sb);
116 ASSERT_NE(rc, -1);
117 ASSERT_EQ(sb.st_mode & 0777, 0600U);
118
119 rc = fprintf(fp, "hello\n");
120 ASSERT_EQ(rc, 6);
121
Elliott Hughes70715da2016-08-01 16:35:17 -0700122 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700123 fclose(fp);
124}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300125
Elliott Hughesf226ee52016-02-03 11:24:28 -0800126TEST(STDIO_TEST, tmpfile64) {
127 FILE* fp = tmpfile64();
128 ASSERT_TRUE(fp != nullptr);
129 fclose(fp);
130}
131
Christopher Ferris13f26a72016-01-13 13:47:58 -0800132TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100133 TemporaryFile tf;
134
135 int rc = dprintf(tf.fd, "hello\n");
136 ASSERT_EQ(rc, 6);
137
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800138 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700139 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700140 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100141
Elliott Hughes70715da2016-08-01 16:35:17 -0700142 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700143 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100144}
145
Christopher Ferris13f26a72016-01-13 13:47:58 -0800146TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300147 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700148 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300149
150 const char* line_written = "This is a test";
151 int rc = fprintf(fp, "%s", line_written);
152 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
153
154 rewind(fp);
155
Yi Kong32bc0fc2018-08-02 17:31:13 -0700156 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300157 size_t allocated_length = 0;
158
159 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
160 for (size_t i = 0; i < 5; ++i) {
161 ASSERT_FALSE(feof(fp));
162 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
163 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800164 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300165 }
166 // The last read should have set the end-of-file indicator for the stream.
167 ASSERT_TRUE(feof(fp));
168 clearerr(fp);
169
170 // getdelim returns -1 but doesn't set errno if we're already at EOF.
171 // It should set the end-of-file indicator for the stream, though.
172 errno = 0;
173 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800174 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300175 ASSERT_TRUE(feof(fp));
176
177 free(word_read);
178 fclose(fp);
179}
180
Christopher Ferris13f26a72016-01-13 13:47:58 -0800181TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300182 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700183 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300184
Yi Kong32bc0fc2018-08-02 17:31:13 -0700185 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300186 size_t buffer_length = 0;
187
188 // The first argument can't be NULL.
189 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700190 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800191 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300192
193 // The second argument can't be NULL.
194 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700195 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800196 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700197 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300198}
199
Christopher Ferris13f26a72016-01-13 13:47:58 -0800200TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700201 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700202 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700203 char* word_read;
204 size_t allocated_length;
205 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
206 fclose(fp);
207}
208
Christopher Ferris13f26a72016-01-13 13:47:58 -0800209TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300210 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700211 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300212
213 const char* line_written = "This is a test for getline\n";
214 const size_t line_count = 5;
215
216 for (size_t i = 0; i < line_count; ++i) {
217 int rc = fprintf(fp, "%s", line_written);
218 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
219 }
220
221 rewind(fp);
222
Yi Kong32bc0fc2018-08-02 17:31:13 -0700223 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300224 size_t allocated_length = 0;
225
226 size_t read_line_count = 0;
227 ssize_t read_char_count;
228 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
229 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
230 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800231 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300232 ++read_line_count;
233 }
234 ASSERT_EQ(read_line_count, line_count);
235
236 // The last read should have set the end-of-file indicator for the stream.
237 ASSERT_TRUE(feof(fp));
238 clearerr(fp);
239
240 // getline returns -1 but doesn't set errno if we're already at EOF.
241 // It should set the end-of-file indicator for the stream, though.
242 errno = 0;
243 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800244 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300245 ASSERT_TRUE(feof(fp));
246
247 free(line_read);
248 fclose(fp);
249}
250
Christopher Ferris13f26a72016-01-13 13:47:58 -0800251TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300252 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700253 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300254
Yi Kong32bc0fc2018-08-02 17:31:13 -0700255 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300256 size_t buffer_length = 0;
257
258 // The first argument can't be NULL.
259 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700260 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800261 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300262
263 // The second argument can't be NULL.
264 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700265 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800266 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700267 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300268}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000269
Christopher Ferris13f26a72016-01-13 13:47:58 -0800270TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800271 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800272 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800273 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
274 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000275 // error: format '%zd' expects argument of type 'signed size_t',
276 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
277 ssize_t v = 1;
278 char buf[32];
279 snprintf(buf, sizeof(buf), "%zd", v);
280}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800281
Elliott Hughes05493712014-04-17 17:30:03 -0700282// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800283TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700284 char buf[BUFSIZ];
285 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
286 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
287}
288
Elliott Hughesb5efade2020-06-05 16:56:53 -0700289// http://b/152588929
290TEST(STDIO_TEST, snprintf_La) {
291#if defined(__LP64__)
292 char buf[BUFSIZ];
293 union {
294 uint64_t a[2];
295 long double v;
296 } u;
297
298 u.a[0] = UINT64_C(0x9b9b9b9b9b9b9b9b);
299 u.a[1] = UINT64_C(0xdfdfdfdfdfdfdfdf);
300 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
301 EXPECT_STREQ("<-0x1.dfdfdfdfdfdf9b9b9b9b9b9b9b9bp+8160>", buf);
302
303 u.a[0] = UINT64_C(0xffffffffffffffff);
304 u.a[1] = UINT64_C(0x7ffeffffffffffff);
305 EXPECT_EQ(41, snprintf(buf, sizeof(buf), "<%La>", u.v));
306 EXPECT_STREQ("<0x1.ffffffffffffffffffffffffffffp+16383>", buf);
307
308 u.a[0] = UINT64_C(0x0000000000000000);
309 u.a[1] = UINT64_C(0x0000000000000000);
310 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%La>", u.v));
311 EXPECT_STREQ("<0x0p+0>", buf);
312#else
313 GTEST_SKIP() << "no ld128";
314#endif
315}
316
Christopher Ferris13f26a72016-01-13 13:47:58 -0800317TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700318 char buf[BUFSIZ];
319 wint_t wc = L'a';
320 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
321 EXPECT_STREQ("<a>", buf);
322}
323
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700324TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
325 char buf[BUFSIZ];
326 wchar_t wc = L'a';
327 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
328 EXPECT_STREQ("<a>", buf);
329}
330
Christopher Ferris13f26a72016-01-13 13:47:58 -0800331TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700332 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700333 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700334 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
335 EXPECT_STREQ("<(null)>", buf);
336
337 wchar_t chars[] = { L'h', L'i', 0 };
338 ws = chars;
339 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
340 EXPECT_STREQ("<hi>", buf);
341}
342
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700343TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
344 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700345 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700346 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
347 EXPECT_STREQ("<(null)>", buf);
348
349 wchar_t chars[] = { L'h', L'i', 0 };
350 ws = chars;
351 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
352 EXPECT_STREQ("<hi>", buf);
353}
354
Christopher Ferris13f26a72016-01-13 13:47:58 -0800355TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700356#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800357 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700358 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700359 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800360 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700361#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800362 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700363#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700364}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700365
Christopher Ferris13f26a72016-01-13 13:47:58 -0800366TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700367 char buf[BUFSIZ];
368
369 snprintf(buf, sizeof(buf), "a");
370 EXPECT_STREQ("a", buf);
371
372 snprintf(buf, sizeof(buf), "%%");
373 EXPECT_STREQ("%", buf);
374
375 snprintf(buf, sizeof(buf), "01234");
376 EXPECT_STREQ("01234", buf);
377
378 snprintf(buf, sizeof(buf), "a%sb", "01234");
379 EXPECT_STREQ("a01234b", buf);
380
Yi Kong32bc0fc2018-08-02 17:31:13 -0700381 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700382 snprintf(buf, sizeof(buf), "a%sb", s);
383 EXPECT_STREQ("a(null)b", buf);
384
385 snprintf(buf, sizeof(buf), "aa%scc", "bb");
386 EXPECT_STREQ("aabbcc", buf);
387
388 snprintf(buf, sizeof(buf), "a%cc", 'b');
389 EXPECT_STREQ("abc", buf);
390
391 snprintf(buf, sizeof(buf), "a%db", 1234);
392 EXPECT_STREQ("a1234b", buf);
393
394 snprintf(buf, sizeof(buf), "a%db", -8123);
395 EXPECT_STREQ("a-8123b", buf);
396
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700397 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700398 EXPECT_STREQ("a16b", buf);
399
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700400 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700401 EXPECT_STREQ("a16b", buf);
402
403 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
404 EXPECT_STREQ("a68719476736b", buf);
405
406 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
407 EXPECT_STREQ("a70000b", buf);
408
409 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
410 EXPECT_STREQ("a0xb0001234b", buf);
411
412 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
413 EXPECT_STREQ("a12abz", buf);
414
415 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
416 EXPECT_STREQ("a12ABz", buf);
417
418 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
419 EXPECT_STREQ("a00123456z", buf);
420
421 snprintf(buf, sizeof(buf), "a%5dz", 1234);
422 EXPECT_STREQ("a 1234z", buf);
423
424 snprintf(buf, sizeof(buf), "a%05dz", 1234);
425 EXPECT_STREQ("a01234z", buf);
426
427 snprintf(buf, sizeof(buf), "a%8dz", 1234);
428 EXPECT_STREQ("a 1234z", buf);
429
430 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
431 EXPECT_STREQ("a1234 z", buf);
432
433 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
434 EXPECT_STREQ("Aabcdef Z", buf);
435
436 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
437 EXPECT_STREQ("Ahello:1234Z", buf);
438
439 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
440 EXPECT_STREQ("a005:5:05z", buf);
441
Yi Kong32bc0fc2018-08-02 17:31:13 -0700442 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700443 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700444#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700445 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800446#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700447 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800448#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700449
450 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
451 EXPECT_STREQ("a68719476736,6,7,8z", buf);
452
453 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
454 EXPECT_STREQ("a_1.230000_b", buf);
455
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700456 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700457 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400458
459 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
460 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700461}
462
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800463template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700464static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
465 int sscanf_fn(const T*, const T*, ...),
466 const T* fmt_string, const T* fmt, const T* fmt_plus,
467 const T* minus_inf, const T* inf_, const T* plus_inf,
468 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800469 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700470 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700471
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700472 // NaN.
473
474 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800475 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700476 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
477 EXPECT_TRUE(isnan(f));
478
479 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800480 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700481 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
482 EXPECT_TRUE(isnan(f));
483
484 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800485 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700486 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
487 EXPECT_TRUE(isnan(f));
488
489 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800490 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700491 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
492 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800493
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700494 // Inf.
495
496 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800497 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700498 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
499 EXPECT_EQ(HUGE_VALF, f);
500
501 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800502 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700503 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
504 EXPECT_EQ(-HUGE_VALF, f);
505
506 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800507 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700508 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
509 EXPECT_EQ(HUGE_VALF, f);
510
511 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800512 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700513 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
514 EXPECT_EQ(-HUGE_VALF, f);
515
516 // Check case-insensitivity.
517 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
518 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
519 EXPECT_EQ(HUGE_VALF, f);
520 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
521 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
522 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700523}
524
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700525TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
526 CheckInfNan(snprintf, sscanf, "%s",
527 "[%a]", "[%+a]",
528 "[-inf]", "[inf]", "[+inf]",
529 "[-nan]", "[nan]", "[+nan]");
530 CheckInfNan(snprintf, sscanf, "%s",
531 "[%A]", "[%+A]",
532 "[-INF]", "[INF]", "[+INF]",
533 "[-NAN]", "[NAN]", "[+NAN]");
534 CheckInfNan(snprintf, sscanf, "%s",
535 "[%e]", "[%+e]",
536 "[-inf]", "[inf]", "[+inf]",
537 "[-nan]", "[nan]", "[+nan]");
538 CheckInfNan(snprintf, sscanf, "%s",
539 "[%E]", "[%+E]",
540 "[-INF]", "[INF]", "[+INF]",
541 "[-NAN]", "[NAN]", "[+NAN]");
542 CheckInfNan(snprintf, sscanf, "%s",
543 "[%f]", "[%+f]",
544 "[-inf]", "[inf]", "[+inf]",
545 "[-nan]", "[nan]", "[+nan]");
546 CheckInfNan(snprintf, sscanf, "%s",
547 "[%F]", "[%+F]",
548 "[-INF]", "[INF]", "[+INF]",
549 "[-NAN]", "[NAN]", "[+NAN]");
550 CheckInfNan(snprintf, sscanf, "%s",
551 "[%g]", "[%+g]",
552 "[-inf]", "[inf]", "[+inf]",
553 "[-nan]", "[nan]", "[+nan]");
554 CheckInfNan(snprintf, sscanf, "%s",
555 "[%G]", "[%+G]",
556 "[-INF]", "[INF]", "[+INF]",
557 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800558}
Elliott Hughes7823f322014-04-14 12:11:28 -0700559
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700560TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
561 CheckInfNan(swprintf, swscanf, L"%s",
562 L"[%a]", L"[%+a]",
563 L"[-inf]", L"[inf]", L"[+inf]",
564 L"[-nan]", L"[nan]", L"[+nan]");
565 CheckInfNan(swprintf, swscanf, L"%s",
566 L"[%A]", L"[%+A]",
567 L"[-INF]", L"[INF]", L"[+INF]",
568 L"[-NAN]", L"[NAN]", L"[+NAN]");
569 CheckInfNan(swprintf, swscanf, L"%s",
570 L"[%e]", L"[%+e]",
571 L"[-inf]", L"[inf]", L"[+inf]",
572 L"[-nan]", L"[nan]", L"[+nan]");
573 CheckInfNan(swprintf, swscanf, L"%s",
574 L"[%E]", L"[%+E]",
575 L"[-INF]", L"[INF]", L"[+INF]",
576 L"[-NAN]", L"[NAN]", L"[+NAN]");
577 CheckInfNan(swprintf, swscanf, L"%s",
578 L"[%f]", L"[%+f]",
579 L"[-inf]", L"[inf]", L"[+inf]",
580 L"[-nan]", L"[nan]", L"[+nan]");
581 CheckInfNan(swprintf, swscanf, L"%s",
582 L"[%F]", L"[%+F]",
583 L"[-INF]", L"[INF]", L"[+INF]",
584 L"[-NAN]", L"[NAN]", L"[+NAN]");
585 CheckInfNan(swprintf, swscanf, L"%s",
586 L"[%g]", L"[%+g]",
587 L"[-inf]", L"[inf]", L"[+inf]",
588 L"[-nan]", L"[nan]", L"[+nan]");
589 CheckInfNan(swprintf, swscanf, L"%s",
590 L"[%G]", L"[%+G]",
591 L"[-INF]", L"[INF]", L"[+INF]",
592 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700593}
594
Dan Albert9601f162017-08-09 14:59:06 -0700595TEST(STDIO_TEST, swprintf) {
596 constexpr size_t nchars = 32;
597 wchar_t buf[nchars];
598
599 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
600 ASSERT_EQ(std::wstring(L"ab"), buf);
601 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
602 ASSERT_EQ(std::wstring(L"abcde"), buf);
603
604 // Unlike swprintf(), swprintf() returns -1 in case of truncation
605 // and doesn't necessarily zero-terminate the output!
606 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
607
608 const char kString[] = "Hello, World";
609 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
610 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
611 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
612 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
613}
614
615TEST(STDIO_TEST, swprintf_a) {
616 constexpr size_t nchars = 32;
617 wchar_t buf[nchars];
618
619 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
620 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
621}
622
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700623TEST(STDIO_TEST, swprintf_lc) {
624 constexpr size_t nchars = 32;
625 wchar_t buf[nchars];
626
627 wint_t wc = L'a';
628 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
629 EXPECT_EQ(std::wstring(L"<a>"), buf);
630}
631
632TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
633 constexpr size_t nchars = 32;
634 wchar_t buf[nchars];
635
636 wint_t wc = L'a';
637 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
638 EXPECT_EQ(std::wstring(L"<a>"), buf);
639}
640
Elliott Hughes618303c2017-11-02 16:58:44 -0700641TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
642 constexpr size_t nchars = 32;
643 wchar_t buf[nchars];
644
645 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
646 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
647}
648
649TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
650 constexpr size_t nchars = 32;
651 wchar_t buf[nchars];
652
653 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
654 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
655}
656
657TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
658 constexpr size_t nchars = 32;
659 wchar_t buf[nchars];
660
661 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
662 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
663}
664
665TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
666 constexpr size_t nchars = 32;
667 wchar_t buf[nchars];
668
669 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
670 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
671}
672
Dan Albert9601f162017-08-09 14:59:06 -0700673TEST(STDIO_TEST, swprintf_ls) {
674 constexpr size_t nchars = 32;
675 wchar_t buf[nchars];
676
677 static const wchar_t kWideString[] = L"Hello\uff41 World";
678 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
679 ASSERT_EQ(std::wstring(kWideString), buf);
680 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
681 ASSERT_EQ(std::wstring(kWideString), buf);
682}
683
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700684TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
685 constexpr size_t nchars = 32;
686 wchar_t buf[nchars];
687
688 static const wchar_t kWideString[] = L"Hello\uff41 World";
689 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
690 ASSERT_EQ(std::wstring(kWideString), buf);
691 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
692 ASSERT_EQ(std::wstring(kWideString), buf);
693}
694
Christopher Ferris13f26a72016-01-13 13:47:58 -0800695TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700696 char buf[BUFSIZ];
697 snprintf(buf, sizeof(buf), "%d", INT_MAX);
698 EXPECT_STREQ("2147483647", buf);
699}
700
Christopher Ferris13f26a72016-01-13 13:47:58 -0800701TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700702 char buf[BUFSIZ];
703 snprintf(buf, sizeof(buf), "%d", INT_MIN);
704 EXPECT_STREQ("-2147483648", buf);
705}
706
Elliott Hughes618303c2017-11-02 16:58:44 -0700707TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
708 char buf[BUFSIZ];
709 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
710 EXPECT_STREQ("9223372036854775807", buf);
711}
712
713TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
714 char buf[BUFSIZ];
715 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
716 EXPECT_STREQ("-9223372036854775808", buf);
717}
718
719TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
720 char buf[BUFSIZ];
721 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
722 EXPECT_STREQ("18446744073709551615", buf);
723}
724
725TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
726 char buf[BUFSIZ];
727 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
728 EXPECT_STREQ("18446744073709551615", buf);
729}
730
Christopher Ferris13f26a72016-01-13 13:47:58 -0800731TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700732 char buf[BUFSIZ];
733 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700734#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700735 EXPECT_STREQ("9223372036854775807", buf);
736#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700737 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700738#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700739}
740
Christopher Ferris13f26a72016-01-13 13:47:58 -0800741TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700742 char buf[BUFSIZ];
743 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700744#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700745 EXPECT_STREQ("-9223372036854775808", buf);
746#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700747 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700748#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700749}
750
Christopher Ferris13f26a72016-01-13 13:47:58 -0800751TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700752 char buf[BUFSIZ];
753 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
754 EXPECT_STREQ("9223372036854775807", buf);
755}
756
Christopher Ferris13f26a72016-01-13 13:47:58 -0800757TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700758 char buf[BUFSIZ];
759 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
760 EXPECT_STREQ("-9223372036854775808", buf);
761}
762
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700763TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
764 char buf[BUFSIZ];
765 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
766 EXPECT_STREQ("37777777777", buf);
767}
768
769TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
770 char buf[BUFSIZ];
771 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
772 EXPECT_STREQ("4294967295", buf);
773}
774
775TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
776 char buf[BUFSIZ];
777 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
778 EXPECT_STREQ("ffffffff", buf);
779}
780
781TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
782 char buf[BUFSIZ];
783 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
784 EXPECT_STREQ("FFFFFFFF", buf);
785}
786
Christopher Ferris13f26a72016-01-13 13:47:58 -0800787TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700788 char buf[BUFSIZ];
789
790 snprintf(buf, sizeof(buf), "%e", 1.5);
791 EXPECT_STREQ("1.500000e+00", buf);
792
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800793 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700794 EXPECT_STREQ("1.500000e+00", buf);
795}
796
Christopher Ferris13f26a72016-01-13 13:47:58 -0800797TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700798 char buf[BUFSIZ];
799
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800800 snprintf(buf, sizeof(buf), "%e", -0.0);
801 EXPECT_STREQ("-0.000000e+00", buf);
802 snprintf(buf, sizeof(buf), "%E", -0.0);
803 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700804 snprintf(buf, sizeof(buf), "%f", -0.0);
805 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800806 snprintf(buf, sizeof(buf), "%F", -0.0);
807 EXPECT_STREQ("-0.000000", buf);
808 snprintf(buf, sizeof(buf), "%g", -0.0);
809 EXPECT_STREQ("-0", buf);
810 snprintf(buf, sizeof(buf), "%G", -0.0);
811 EXPECT_STREQ("-0", buf);
812 snprintf(buf, sizeof(buf), "%a", -0.0);
813 EXPECT_STREQ("-0x0p+0", buf);
814 snprintf(buf, sizeof(buf), "%A", -0.0);
815 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700816}
817
Christopher Ferris13f26a72016-01-13 13:47:58 -0800818TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700819 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700820 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700821
Elliott Hughes69f05d22014-06-05 20:10:09 -0700822 // http://b/15439554
823 char buf[BUFSIZ];
824
825 // 1-byte character.
826 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
827 EXPECT_STREQ("1x2", buf);
828 // 2-byte character.
829 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
830 EXPECT_STREQ("1¢2", buf);
831 // 3-byte character.
832 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
833 EXPECT_STREQ("1€2", buf);
834 // 4-byte character.
835 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
836 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700837
Wally Yaua40fdbd2014-08-26 09:47:23 -0700838 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700839 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700840}
841
Elliott Hughes43f7c872016-02-05 11:18:41 -0800842static void* snprintf_small_stack_fn(void*) {
843 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
844 char buf[PATH_MAX];
845 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
846 return nullptr;
847}
848
849TEST(STDIO_TEST, snprintf_small_stack) {
850 // Is it safe to call snprintf on a thread with a small stack?
851 // (The snprintf implementation puts some pretty large buffers on the stack.)
852 pthread_attr_t a;
853 ASSERT_EQ(0, pthread_attr_init(&a));
854 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
855
856 pthread_t t;
857 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
858 ASSERT_EQ(0, pthread_join(t, nullptr));
859}
860
Elliott Hughes8200e552016-02-05 21:57:37 -0800861TEST(STDIO_TEST, snprintf_asterisk_overflow) {
862 char buf[128];
863 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
864 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
865 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
866 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
867 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
868
869 // INT_MAX-1, INT_MAX, INT_MAX+1.
870 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
871 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
872 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
873 ASSERT_EQ(ENOMEM, errno);
874}
875
Elliott Hughes70715da2016-08-01 16:35:17 -0700876TEST(STDIO_TEST, fprintf) {
877 TemporaryFile tf;
878
879 FILE* tfile = fdopen(tf.fd, "r+");
880 ASSERT_TRUE(tfile != nullptr);
881
882 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
883 AssertFileIs(tfile, "123 abc");
884 fclose(tfile);
885}
886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800887TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700888 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700889 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700890 int fd_rdonly = open("/dev/null", O_RDONLY);
891 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700892
893 // Unbuffered case where the fprintf(3) itself fails.
894 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700895 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700896 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700897 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700898 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700899 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700900
901 // Buffered case where we won't notice until the fclose(3).
902 // It's likely this is what was actually seen in http://b/7229520,
903 // and that expecting fprintf to fail is setting yourself up for
904 // disappointment. Remember to check fclose(3)'s return value, kids!
905 ASSERT_NE(nullptr, fp = tmpfile());
906 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700907 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700908 ASSERT_EQ(4, fprintf(fp, "fail"));
909 ASSERT_EQ(-1, fclose(fp));
910}
911
Elliott Hughes468efc82018-07-10 14:39:49 -0700912TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800913 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700914 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800915
916 char buf[16];
917 char* s = fgets(buf, sizeof(buf), fp);
918 buf[13] = '\0';
919 ASSERT_STREQ("Linux version", s);
920
921 ASSERT_EQ(0, pclose(fp));
922}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700923
Elliott Hughes468efc82018-07-10 14:39:49 -0700924TEST(STDIO_TEST, popen_socketpair) {
925 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700926 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700927
928 fputs("hello\nworld\n", fp);
929 fflush(fp);
930
931 char buf[16];
932 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
933 EXPECT_STREQ("hello\n", buf);
934 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
935 EXPECT_STREQ("world\n", buf);
936
937 ASSERT_EQ(0, pclose(fp));
938}
939
940TEST(STDIO_TEST, popen_socketpair_shutdown) {
941 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700942 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700943
944 fputs("a\na\na\na\nb\n", fp);
945 fflush(fp);
946 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
947
948 char buf[16];
949 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
950 EXPECT_STREQ(" 4 a\n", buf);
951 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
952 EXPECT_STREQ(" 1 b\n", buf);
953
954 ASSERT_EQ(0, pclose(fp));
955}
956
957TEST(STDIO_TEST, popen_return_value_0) {
958 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700959 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700960 int status = pclose(fp);
961 EXPECT_TRUE(WIFEXITED(status));
962 EXPECT_EQ(0, WEXITSTATUS(status));
963}
964
965TEST(STDIO_TEST, popen_return_value_1) {
966 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700967 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700968 int status = pclose(fp);
969 EXPECT_TRUE(WIFEXITED(status));
970 EXPECT_EQ(1, WEXITSTATUS(status));
971}
972
973TEST(STDIO_TEST, popen_return_value_signal) {
974 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700975 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700976 int status = pclose(fp);
977 EXPECT_TRUE(WIFSIGNALED(status));
978 EXPECT_EQ(7, WTERMSIG(status));
979}
980
Christopher Ferris13f26a72016-01-13 13:47:58 -0800981TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700982 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700983 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700984 ASSERT_EQ('L', getc(fp));
985 ASSERT_EQ('i', getc(fp));
986 ASSERT_EQ('n', getc(fp));
987 ASSERT_EQ('u', getc(fp));
988 ASSERT_EQ('x', getc(fp));
989 fclose(fp);
990}
991
Christopher Ferris13f26a72016-01-13 13:47:58 -0800992TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700993 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700994 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700995 ASSERT_EQ(EOF, putc('x', fp));
996 fclose(fp);
997}
Elliott Hughes603332f2014-03-12 17:10:41 -0700998
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700999TEST(STDIO_TEST, sscanf_swscanf) {
1000 struct stuff {
1001 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001002 int i1, i2;
1003 char cs1[3];
1004 char s2[3];
1005 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001006 double d1;
1007 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001008 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001009
1010 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001011 EXPECT_STREQ("hello", s1);
1012 EXPECT_EQ(123, i1);
1013 EXPECT_EQ(456, i2);
1014 EXPECT_EQ('a', cs1[0]);
1015 EXPECT_EQ('b', cs1[1]);
1016 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
1017 EXPECT_STREQ("AB", s2); // Terminating NUL.
1018 EXPECT_EQ('!', c1);
1019 EXPECT_DOUBLE_EQ(1.23, d1);
1020 EXPECT_FLOAT_EQ(9.0f, f1);
1021 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001022 }
1023 } s;
1024
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001025 memset(&s, 'x', sizeof(s));
1026 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1027 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1028 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 -07001029 s.Check();
1030
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001031 memset(&s, 'x', sizeof(s));
1032 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1033 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1034 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 -07001035 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001036}
Elliott Hughes53b24382014-05-02 18:29:25 -07001037
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001038template <typename T>
1039static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1040 const T* input, const T* fmt,
1041 int expected_count, const char* expected_string) {
1042 char buf[256] = {};
1043 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1044 ASSERT_STREQ(expected_string, buf) << fmt;
1045}
1046
1047TEST(STDIO_TEST, sscanf_ccl) {
1048 // `abc` is just those characters.
1049 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1050 // `a-c` is the range 'a' .. 'c'.
1051 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1052 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1053 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1054 // `a-c-e` is equivalent to `a-e`.
1055 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1056 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1057 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1058 // An initial '^' negates the set.
1059 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1060 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1061 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1062 // The first character may be ']' or '-' without being special.
1063 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1064 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1065 // The last character may be '-' without being special.
1066 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1067 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1068 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1069}
1070
1071TEST(STDIO_TEST, swscanf_ccl) {
1072 // `abc` is just those characters.
1073 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1074 // `a-c` is the range 'a' .. 'c'.
1075 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1076 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1077 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1078 // `a-c-e` is equivalent to `a-e`.
1079 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1080 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1081 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1082 // An initial '^' negates the set.
1083 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1084 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1085 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1086 // The first character may be ']' or '-' without being special.
1087 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1088 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1089 // The last character may be '-' without being special.
1090 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1091 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1092 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1093}
1094
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001095template <typename T1, typename T2>
1096static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1097 const T1* input, const T1* fmt,
1098 int expected_count, const T2* expected_string) {
1099 T2* result = nullptr;
1100 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1101 if (expected_string == nullptr) {
1102 ASSERT_EQ(nullptr, result);
1103 } else {
1104 ASSERT_STREQ(expected_string, result) << fmt;
1105 }
1106 free(result);
1107}
1108
1109TEST(STDIO_TEST, sscanf_mc) {
1110 char* p1 = nullptr;
1111 char* p2 = nullptr;
1112 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1113 ASSERT_EQ('h', *p1);
1114 ASSERT_EQ('e', *p2);
1115 free(p1);
1116 free(p2);
1117
1118 p1 = nullptr;
1119 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1120 ASSERT_EQ('h', p1[0]);
1121 ASSERT_EQ('e', p1[1]);
1122 ASSERT_EQ('l', p1[2]);
1123 ASSERT_EQ('l', p1[3]);
1124 free(p1);
1125
1126 p1 = nullptr;
1127 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1128 ASSERT_EQ('h', p1[0]);
1129 ASSERT_EQ('e', p1[1]);
1130 ASSERT_EQ('l', p1[2]);
1131 ASSERT_EQ('l', p1[3]);
1132 ASSERT_EQ('o', p1[4]);
1133 free(p1);
1134}
1135
1136
1137TEST(STDIO_TEST, sscanf_mlc) {
1138 // This is so useless that clang doesn't even believe it exists...
1139#pragma clang diagnostic push
1140#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1141#pragma clang diagnostic ignored "-Wformat-extra-args"
1142
1143 wchar_t* p1 = nullptr;
1144 wchar_t* p2 = nullptr;
1145 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1146 ASSERT_EQ(L'h', *p1);
1147 ASSERT_EQ(L'e', *p2);
1148 free(p1);
1149 free(p2);
1150
1151 p1 = nullptr;
1152 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1153 ASSERT_EQ(L'h', p1[0]);
1154 ASSERT_EQ(L'e', p1[1]);
1155 ASSERT_EQ(L'l', p1[2]);
1156 ASSERT_EQ(L'l', p1[3]);
1157 free(p1);
1158
1159 p1 = nullptr;
1160 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1161 ASSERT_EQ(L'h', p1[0]);
1162 ASSERT_EQ(L'e', p1[1]);
1163 ASSERT_EQ(L'l', p1[2]);
1164 ASSERT_EQ(L'l', p1[3]);
1165 ASSERT_EQ(L'o', p1[4]);
1166 free(p1);
1167#pragma clang diagnostic pop
1168}
1169
1170
1171TEST(STDIO_TEST, sscanf_ms) {
1172 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1173 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1174 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1175}
1176
1177TEST(STDIO_TEST, sscanf_mls) {
1178 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1179 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1180 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1181}
1182
1183TEST(STDIO_TEST, sscanf_m_ccl) {
1184 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1185 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1186 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1187}
1188
1189TEST(STDIO_TEST, sscanf_ml_ccl) {
1190 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1191 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1192 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1193}
1194
1195TEST(STDIO_TEST, sscanf_ls) {
1196 wchar_t w[32] = {};
1197 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1198 ASSERT_EQ(L"hello", std::wstring(w));
1199}
1200
1201TEST(STDIO_TEST, sscanf_ls_suppress) {
1202 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1203}
1204
1205TEST(STDIO_TEST, sscanf_ls_n) {
1206 setlocale(LC_ALL, "C.UTF-8");
1207 wchar_t w[32] = {};
1208 int pos = 0;
1209 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1210 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1211 ASSERT_EQ(2, pos);
1212}
1213
1214TEST(STDIO_TEST, sscanf_ls_realloc) {
1215 // This is so useless that clang doesn't even believe it exists...
1216#pragma clang diagnostic push
1217#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1218#pragma clang diagnostic ignored "-Wformat-extra-args"
1219 wchar_t* p1 = nullptr;
1220 wchar_t* p2 = nullptr;
1221 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1222 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1223 ASSERT_EQ(L"world", std::wstring(p2));
1224#pragma clang diagnostic pop
1225}
1226
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001227// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1228TEST(STDIO_TEST, scanf_wscanf_EOF) {
1229 EXPECT_EQ(0, sscanf("b", "ab"));
1230 EXPECT_EQ(EOF, sscanf("", "a"));
1231 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1232 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1233}
1234
1235TEST(STDIO_TEST, scanf_invalid_UTF8) {
1236#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1237 char buf[BUFSIZ];
1238 wchar_t wbuf[BUFSIZ];
1239
1240 memset(buf, 0, sizeof(buf));
1241 memset(wbuf, 0, sizeof(wbuf));
1242 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1243#endif
1244}
1245
1246TEST(STDIO_TEST, scanf_no_match_no_termination) {
1247 char buf[4] = "x";
1248 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1249 EXPECT_EQ('x', buf[0]);
1250 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1251 EXPECT_EQ('x', buf[0]);
1252
1253 wchar_t wbuf[4] = L"x";
1254 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1255 EXPECT_EQ(L'x', wbuf[0]);
1256
1257 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1258 EXPECT_EQ('x', buf[0]);
1259
1260 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1261 EXPECT_EQ(L'x', wbuf[0]);
1262}
1263
1264TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1265#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1266 wchar_t buf[BUFSIZ];
1267
1268 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1269 memset(buf, 0, sizeof(buf));
1270 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1271 EXPECT_EQ(L"x"s, std::wstring(buf));
1272 memset(buf, 0, sizeof(buf));
1273 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1274 EXPECT_EQ(L"x"s, std::wstring(buf));
1275
1276 // Even if scanf has wide characters in a class, they won't match...
1277 // TODO: is that a bug?
1278 memset(buf, 0, sizeof(buf));
1279 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1280 EXPECT_EQ(L"x"s, std::wstring(buf));
1281 // ...unless you use wscanf.
1282 memset(buf, 0, sizeof(buf));
1283 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1284 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1285
1286 // Negation only covers ASCII for scanf...
1287 memset(buf, 0, sizeof(buf));
1288 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1289 EXPECT_EQ(L"x"s, std::wstring(buf));
1290 // ...but covers wide characters for wscanf.
1291 memset(buf, 0, sizeof(buf));
1292 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1293 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1294
1295 // We already determined that non-ASCII characters are ignored in scanf classes.
1296 memset(buf, 0, sizeof(buf));
1297 EXPECT_EQ(1, sscanf("x"
1298 "\xc4\x80" // Matches a byte from each wide char in the class.
1299 "\xc6\x82" // Neither byte is in the class.
1300 "yz",
1301 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1302 EXPECT_EQ(L"x", std::wstring(buf));
1303 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1304 memset(buf, 0, sizeof(buf));
1305 EXPECT_EQ(1, swscanf(L"x"
1306 L"\xc4\x80"
1307 L"\xc6\x82"
1308 L"yz",
1309 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1310 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1311 // not put back together as a wide character.
1312 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1313#endif
1314}
1315
Christopher Ferris13f26a72016-01-13 13:47:58 -08001316TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001317 // If we open a file read-only...
1318 FILE* fp = fopen("/proc/version", "r");
1319
1320 // ...all attempts to write to that file should return failure.
1321
1322 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1323 // glibc gets the wide-character functions wrong.
1324
1325 errno = 0;
1326 EXPECT_EQ(EOF, putc('x', fp));
1327 EXPECT_EQ(EBADF, errno);
1328
1329 errno = 0;
1330 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1331 EXPECT_EQ(EBADF, errno);
1332
1333 errno = 0;
1334 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001335#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001336 EXPECT_EQ(EBADF, errno);
1337#endif
1338
1339 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001340 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1341 EXPECT_EQ(EBADF, errno);
1342
1343 errno = 0;
1344 EXPECT_EQ(EOF, fputs("hello", fp));
1345 EXPECT_EQ(EBADF, errno);
1346
1347 errno = 0;
1348 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001349#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001350 EXPECT_EQ(EBADF, errno);
1351#endif
1352}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001353
1354// Tests that we can only have a consistent and correct fpos_t when using
1355// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001356TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001357 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1358 uselocale(LC_GLOBAL_LOCALE);
1359
1360 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001361 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001362
1363 wchar_t mb_one_bytes = L'h';
1364 wchar_t mb_two_bytes = 0x00a2;
1365 wchar_t mb_three_bytes = 0x20ac;
1366 wchar_t mb_four_bytes = 0x24b62;
1367
1368 // Write to file.
1369 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1370 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1371 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1372 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1373
1374 rewind(fp);
1375
1376 // Record each character position.
1377 fpos_t pos1;
1378 fpos_t pos2;
1379 fpos_t pos3;
1380 fpos_t pos4;
1381 fpos_t pos5;
1382 EXPECT_EQ(0, fgetpos(fp, &pos1));
1383 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1384 EXPECT_EQ(0, fgetpos(fp, &pos2));
1385 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1386 EXPECT_EQ(0, fgetpos(fp, &pos3));
1387 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1388 EXPECT_EQ(0, fgetpos(fp, &pos4));
1389 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1390 EXPECT_EQ(0, fgetpos(fp, &pos5));
1391
Elliott Hughes063525c2014-05-13 11:19:57 -07001392#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001393 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1394 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1395 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1396 // structure.
1397 ASSERT_EQ(0, static_cast<off_t>(pos1));
1398 ASSERT_EQ(1, static_cast<off_t>(pos2));
1399 ASSERT_EQ(3, static_cast<off_t>(pos3));
1400 ASSERT_EQ(6, static_cast<off_t>(pos4));
1401 ASSERT_EQ(10, static_cast<off_t>(pos5));
1402#endif
1403
1404 // Exercise back and forth movements of the position.
1405 ASSERT_EQ(0, fsetpos(fp, &pos2));
1406 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1407 ASSERT_EQ(0, fsetpos(fp, &pos1));
1408 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1409 ASSERT_EQ(0, fsetpos(fp, &pos4));
1410 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1411 ASSERT_EQ(0, fsetpos(fp, &pos3));
1412 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1413 ASSERT_EQ(0, fsetpos(fp, &pos5));
1414 ASSERT_EQ(WEOF, fgetwc(fp));
1415
1416 fclose(fp);
1417}
1418
1419// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001420TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001421 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1422 uselocale(LC_GLOBAL_LOCALE);
1423
Calin Juravle9b95ea92014-05-14 17:07:10 +01001424 // In glibc-2.16 fseek doesn't work properly in wide mode
1425 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1426 // to close and re-open the file. We do it in order to make the test pass
1427 // with all glibcs.
1428
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001429 TemporaryFile tf;
1430 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001431 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001432
1433 wchar_t mb_two_bytes = 0x00a2;
1434 wchar_t mb_three_bytes = 0x20ac;
1435 wchar_t mb_four_bytes = 0x24b62;
1436
1437 // Write to file.
1438 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1439 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1440 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1441
1442 fflush(fp);
1443 fclose(fp);
1444
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001445 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001446 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001447
1448 // Store a valid position.
1449 fpos_t mb_two_bytes_pos;
1450 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1451
1452 // Move inside mb_four_bytes with fseek.
1453 long offset_inside_mb = 6;
1454 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1455
1456 // Store the "inside multi byte" position.
1457 fpos_t pos_inside_mb;
1458 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001459#if defined(__BIONIC__)
1460 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1461#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001462
1463 // Reading from within a byte should produce an error.
1464 ASSERT_EQ(WEOF, fgetwc(fp));
1465 ASSERT_EQ(EILSEQ, errno);
1466
1467 // Reverting to a valid position should work.
1468 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1469 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1470
1471 // Moving withing a multi byte with fsetpos should work but reading should
1472 // produce an error.
1473 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1474 ASSERT_EQ(WEOF, fgetwc(fp));
1475 ASSERT_EQ(EILSEQ, errno);
1476
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001477 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001478}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001479
Christopher Ferris13f26a72016-01-13 13:47:58 -08001480TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001481 char buf[16];
1482 memset(buf, 0, sizeof(buf));
1483 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1484 ASSERT_EQ('<', fputc('<', fp));
1485 ASSERT_NE(EOF, fputs("abc>\n", fp));
1486 fflush(fp);
1487
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001488 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001489 ASSERT_STREQ("<abc>\n", buf);
1490
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001491 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001492 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001493 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001494}
1495
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001496TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001497 FILE* fp = fmemopen(nullptr, 128, "r+");
1498 ASSERT_NE(EOF, fputs("xyz\n", fp));
1499
Elliott Hughes70715da2016-08-01 16:35:17 -07001500 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001501 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001502}
1503
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001504TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1505 FILE* fp;
1506 char buf[8];
1507
1508 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1509 // shall be written at the current position or at the end of the buffer,
1510 // depending on the size of the contents."
1511 memset(buf, 'x', sizeof(buf));
1512 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1513 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1514 ASSERT_EQ(0, fflush(fp));
1515 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1516 // Now write and check that the NUL moves along with our writes...
1517 ASSERT_NE(EOF, fputs("hello", fp));
1518 ASSERT_EQ(0, fflush(fp));
1519 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1520 ASSERT_NE(EOF, fputs("wo", fp));
1521 ASSERT_EQ(0, fflush(fp));
1522 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1523 ASSERT_EQ(0, fclose(fp));
1524
1525 // "If a stream open for update is flushed or closed and the last write has
1526 // advanced the current buffer size, a null byte shall be written at the end
1527 // of the buffer if it fits."
1528 memset(buf, 'x', sizeof(buf));
1529 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1530 // Nothing written yet, so no advance...
1531 ASSERT_EQ(0, fflush(fp));
1532 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1533 ASSERT_NE(EOF, fputs("hello", fp));
1534 ASSERT_EQ(0, fclose(fp));
1535}
1536
1537TEST(STDIO_TEST, fmemopen_size) {
1538 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001539 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001540 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001541
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001542 // POSIX: "The stream shall also maintain the size of the current buffer
1543 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1544 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001545
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001546 // "For modes r and r+ the size shall be set to the value given by the size
1547 // argument."
1548 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1549 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1550 EXPECT_EQ(16, ftell(fp));
1551 EXPECT_EQ(16, ftello(fp));
1552 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1553 EXPECT_EQ(16, ftell(fp));
1554 EXPECT_EQ(16, ftello(fp));
1555 ASSERT_EQ(0, fclose(fp));
1556 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1557 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1558 EXPECT_EQ(16, ftell(fp));
1559 EXPECT_EQ(16, ftello(fp));
1560 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1561 EXPECT_EQ(16, ftell(fp));
1562 EXPECT_EQ(16, ftello(fp));
1563 ASSERT_EQ(0, fclose(fp));
1564
1565 // "For modes w and w+ the initial size shall be zero..."
1566 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1567 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1568 EXPECT_EQ(0, ftell(fp));
1569 EXPECT_EQ(0, ftello(fp));
1570 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1571 EXPECT_EQ(0, ftell(fp));
1572 EXPECT_EQ(0, ftello(fp));
1573 ASSERT_EQ(0, fclose(fp));
1574 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1575 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1576 EXPECT_EQ(0, ftell(fp));
1577 EXPECT_EQ(0, ftello(fp));
1578 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1579 EXPECT_EQ(0, ftell(fp));
1580 EXPECT_EQ(0, ftello(fp));
1581 ASSERT_EQ(0, fclose(fp));
1582
1583 // "...and for modes a and a+ the initial size shall be:
1584 // 1. Zero, if buf is a null pointer
1585 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1586 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1587 EXPECT_EQ(0, ftell(fp));
1588 EXPECT_EQ(0, ftello(fp));
1589 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1590 EXPECT_EQ(0, ftell(fp));
1591 EXPECT_EQ(0, ftello(fp));
1592 ASSERT_EQ(0, fclose(fp));
1593 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1594 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1595 EXPECT_EQ(0, ftell(fp));
1596 EXPECT_EQ(0, ftello(fp));
1597 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1598 EXPECT_EQ(0, ftell(fp));
1599 EXPECT_EQ(0, ftello(fp));
1600 ASSERT_EQ(0, fclose(fp));
1601
1602 // 2. The position of the first null byte in the buffer, if one is found
1603 memset(buf, 'x', sizeof(buf));
1604 buf[3] = '\0';
1605 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1606 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1607 EXPECT_EQ(3, ftell(fp));
1608 EXPECT_EQ(3, ftello(fp));
1609 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1610 EXPECT_EQ(3, ftell(fp));
1611 EXPECT_EQ(3, ftello(fp));
1612 ASSERT_EQ(0, fclose(fp));
1613 memset(buf, 'x', sizeof(buf));
1614 buf[3] = '\0';
1615 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1616 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1617 EXPECT_EQ(3, ftell(fp));
1618 EXPECT_EQ(3, ftello(fp));
1619 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1620 EXPECT_EQ(3, ftell(fp));
1621 EXPECT_EQ(3, ftello(fp));
1622 ASSERT_EQ(0, fclose(fp));
1623
1624 // 3. The value of the size argument, if buf is not a null pointer and no
1625 // null byte is found.
1626 memset(buf, 'x', sizeof(buf));
1627 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1628 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1629 EXPECT_EQ(16, ftell(fp));
1630 EXPECT_EQ(16, ftello(fp));
1631 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1632 EXPECT_EQ(16, ftell(fp));
1633 EXPECT_EQ(16, ftello(fp));
1634 ASSERT_EQ(0, fclose(fp));
1635 memset(buf, 'x', sizeof(buf));
1636 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1637 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1638 EXPECT_EQ(16, ftell(fp));
1639 EXPECT_EQ(16, ftello(fp));
1640 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1641 EXPECT_EQ(16, ftell(fp));
1642 EXPECT_EQ(16, ftello(fp));
1643 ASSERT_EQ(0, fclose(fp));
1644}
1645
1646TEST(STDIO_TEST, fmemopen_SEEK_END) {
1647 // fseek SEEK_END is relative to the current string length, not the buffer size.
1648 FILE* fp;
1649 char buf[8];
1650 memset(buf, 'x', sizeof(buf));
1651 strcpy(buf, "str");
1652 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1653 ASSERT_NE(EOF, fputs("string", fp));
1654 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1655 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1656 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1657 EXPECT_EQ(0, fclose(fp));
1658
1659 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1660 // than adding).
1661 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1662 ASSERT_NE(EOF, fputs("54321", fp));
1663 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1664 EXPECT_EQ('2', fgetc(fp));
1665 EXPECT_EQ(0, fclose(fp));
1666}
1667
1668TEST(STDIO_TEST, fmemopen_seek_invalid) {
1669 char buf[8];
1670 memset(buf, 'x', sizeof(buf));
1671 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1672 ASSERT_TRUE(fp != nullptr);
1673
1674 // POSIX: "An attempt to seek ... to a negative position or to a position
1675 // larger than the buffer size given in the size argument shall fail."
1676 // (There's no mention of what errno should be set to, and glibc doesn't
1677 // set errno in any of these cases.)
1678 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1679 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1680 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1681 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1682}
1683
1684TEST(STDIO_TEST, fmemopen_read_EOF) {
1685 // POSIX: "A read operation on the stream shall not advance the current
1686 // buffer position beyond the current buffer size."
1687 char buf[8];
1688 memset(buf, 'x', sizeof(buf));
1689 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1690 ASSERT_TRUE(fp != nullptr);
1691 char buf2[BUFSIZ];
1692 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1693 // POSIX: "Reaching the buffer size in a read operation shall count as
1694 // end-of-file.
1695 ASSERT_TRUE(feof(fp));
1696 ASSERT_EQ(EOF, fgetc(fp));
1697 ASSERT_EQ(0, fclose(fp));
1698}
1699
1700TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1701 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1702 char buf[] = "h\0e\0l\0l\0o";
1703 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1704 ASSERT_TRUE(fp != nullptr);
1705 ASSERT_EQ('h', fgetc(fp));
1706 ASSERT_EQ(0, fgetc(fp));
1707 ASSERT_EQ('e', fgetc(fp));
1708 ASSERT_EQ(0, fgetc(fp));
1709 ASSERT_EQ('l', fgetc(fp));
1710 ASSERT_EQ(0, fgetc(fp));
1711 // POSIX: "The read operation shall start at the current buffer position of
1712 // the stream."
1713 char buf2[8];
1714 memset(buf2, 'x', sizeof(buf2));
1715 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1716 ASSERT_EQ('l', buf2[0]);
1717 ASSERT_EQ(0, buf2[1]);
1718 ASSERT_EQ('o', buf2[2]);
1719 ASSERT_EQ(0, buf2[3]);
1720 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1721 ASSERT_TRUE(feof(fp));
1722 ASSERT_EQ(0, fclose(fp));
1723}
1724
1725TEST(STDIO_TEST, fmemopen_write) {
1726 FILE* fp;
1727 char buf[8];
1728
1729 // POSIX: "A write operation shall start either at the current position of
1730 // the stream (if mode has not specified 'a' as the first character)..."
1731 memset(buf, 'x', sizeof(buf));
1732 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1733 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1734 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1735 ASSERT_EQ(' ', fputc(' ', fp));
1736 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1737 ASSERT_EQ(0, fclose(fp));
1738
1739 // "...or at the current size of the stream (if mode had 'a' as the first
1740 // character)." (See the fmemopen_size test for what "size" means, but for
1741 // mode "a", it's the first NUL byte.)
1742 memset(buf, 'x', sizeof(buf));
1743 buf[3] = '\0';
1744 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1745 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1746 ASSERT_EQ(' ', fputc(' ', fp));
1747 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1748 ASSERT_EQ(0, fclose(fp));
1749
1750 // "If the current position at the end of the write is larger than the
1751 // current buffer size, the current buffer size shall be set to the current
1752 // position." (See the fmemopen_size test for what "size" means, but to
1753 // query it we SEEK_END with offset 0, and then ftell.)
1754 memset(buf, 'x', sizeof(buf));
1755 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1756 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1757 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1758 EXPECT_EQ(0, ftell(fp));
1759 ASSERT_EQ(' ', fputc(' ', fp));
1760 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1761 EXPECT_EQ(1, ftell(fp));
1762 ASSERT_NE(EOF, fputs("123", fp));
1763 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1764 EXPECT_EQ(4, ftell(fp));
1765 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1766 ASSERT_EQ(0, fclose(fp));
1767}
1768
1769TEST(STDIO_TEST, fmemopen_write_EOF) {
1770 // POSIX: "A write operation on the stream shall not advance the current
1771 // buffer size beyond the size given in the size argument."
1772 FILE* fp;
1773
1774 // Scalar writes...
1775 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1776 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1777 ASSERT_EQ('x', fputc('x', fp));
1778 ASSERT_EQ('x', fputc('x', fp));
1779 ASSERT_EQ('x', fputc('x', fp));
1780 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1781 ASSERT_EQ(0, fclose(fp));
1782
1783 // Vector writes...
1784 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1785 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1786 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1787 ASSERT_EQ(0, fclose(fp));
1788}
1789
1790TEST(STDIO_TEST, fmemopen_initial_position) {
1791 // POSIX: "The ... current position in the buffer ... shall be initially
1792 // set to either the beginning of the buffer (for r and w modes) ..."
1793 char buf[] = "hello\0world";
1794 FILE* fp;
1795 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1796 EXPECT_EQ(0L, ftell(fp));
1797 EXPECT_EQ(0, fclose(fp));
1798 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1799 EXPECT_EQ(0L, ftell(fp));
1800 EXPECT_EQ(0, fclose(fp));
1801 buf[0] = 'h'; // (Undo the effects of the above.)
1802
1803 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1804 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1805 EXPECT_EQ(5L, ftell(fp));
1806 EXPECT_EQ(0, fclose(fp));
1807
1808 // POSIX: "If no null byte is found in append mode, the initial position
1809 // shall be set to one byte after the end of the buffer."
1810 memset(buf, 'x', sizeof(buf));
1811 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1812 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1813 EXPECT_EQ(0, fclose(fp));
1814}
1815
1816TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1817 // POSIX: "If buf is a null pointer, the initial position shall always be
1818 // set to the beginning of the buffer."
1819 FILE* fp = fmemopen(nullptr, 128, "a+");
1820 ASSERT_TRUE(fp != nullptr);
1821 EXPECT_EQ(0L, ftell(fp));
1822 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1823 EXPECT_EQ(0, fclose(fp));
1824}
1825
1826TEST(STDIO_TEST, fmemopen_zero_length) {
1827 // POSIX says it's up to the implementation whether or not you can have a
1828 // zero-length buffer (but "A future version of this standard may require
1829 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1830 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1831 FILE* fp;
1832 char buf[16];
1833 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1834 ASSERT_EQ(EOF, fgetc(fp));
1835 ASSERT_TRUE(feof(fp));
1836 ASSERT_EQ(0, fclose(fp));
1837 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1838 ASSERT_EQ(EOF, fgetc(fp));
1839 ASSERT_TRUE(feof(fp));
1840 ASSERT_EQ(0, fclose(fp));
1841
1842 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1843 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1844 ASSERT_EQ(EOF, fputc('x', fp));
1845 ASSERT_EQ(0, fclose(fp));
1846 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1847 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1848 ASSERT_EQ(EOF, fputc('x', fp));
1849 ASSERT_EQ(0, fclose(fp));
1850}
1851
Elliott Hughes288465d2019-02-05 15:00:13 -08001852TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1853 char buf[2] = "x";
1854 ASSERT_EQ('x', buf[0]);
1855 FILE* fp = fmemopen(buf, 0, "w");
1856 ASSERT_EQ('x', buf[0]);
1857 ASSERT_EQ(0, fclose(fp));
1858}
1859
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001860TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1861 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1862 // BSD fails, glibc doesn't. We side with the more lenient.
1863 FILE* fp;
1864 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1865 ASSERT_EQ(0, fclose(fp));
1866 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1867 ASSERT_EQ(0, fclose(fp));
1868}
1869
1870TEST(STDIO_TEST, fmemopen_fileno) {
1871 // There's no fd backing an fmemopen FILE*.
1872 FILE* fp = fmemopen(nullptr, 16, "r");
1873 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001874 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001875 ASSERT_EQ(-1, fileno(fp));
1876 ASSERT_EQ(EBADF, errno);
1877 ASSERT_EQ(0, fclose(fp));
1878}
1879
1880TEST(STDIO_TEST, fmemopen_append_after_seek) {
1881 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1882 // there had been an intervening seek.
1883
1884 FILE* fp;
1885 char buf[] = "hello\0world";
1886 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1887 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1888 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1889 ASSERT_NE(EOF, fputc('!', fp));
1890 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1891 ASSERT_EQ(0, fclose(fp));
1892
1893 memcpy(buf, "hello\0world", sizeof(buf));
1894 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1895 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1896 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1897 ASSERT_NE(EOF, fputc('!', fp));
1898 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1899 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001900}
1901
Christopher Ferris13f26a72016-01-13 13:47:58 -08001902TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001903 char* p = nullptr;
1904 size_t size = 0;
1905 FILE* fp = open_memstream(&p, &size);
1906 ASSERT_NE(EOF, fputs("hello, world!", fp));
1907 fclose(fp);
1908
1909 ASSERT_STREQ("hello, world!", p);
1910 ASSERT_EQ(strlen("hello, world!"), size);
1911 free(p);
1912}
1913
Christopher Ferris13f26a72016-01-13 13:47:58 -08001914TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001915#if defined(__BIONIC__)
1916 char* p;
1917 size_t size;
1918
1919 // Invalid buffer.
1920 errno = 0;
1921 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1922 ASSERT_EQ(EINVAL, errno);
1923
1924 // Invalid size.
1925 errno = 0;
1926 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1927 ASSERT_EQ(EINVAL, errno);
1928#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001929 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001930#endif
1931}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001932
Christopher Ferris13f26a72016-01-13 13:47:58 -08001933TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001934 int fd = open("/proc/version", O_RDONLY);
1935 ASSERT_TRUE(fd != -1);
1936
1937 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001938 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001939
1940 FILE* fp = fdopen(fd, "re");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001941 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001942
1943 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001944 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001945
1946 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001947}
1948
Christopher Ferris13f26a72016-01-13 13:47:58 -08001949TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001950 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001951 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001952
1953 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001954 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001955
1956 fp = freopen("/proc/version", "re", fp);
1957
1958 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001959 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001960
1961 fclose(fp);
1962}
Elliott Hughes20841a12014-12-01 16:13:30 -08001963
Elliott Hughesf226ee52016-02-03 11:24:28 -08001964TEST(STDIO_TEST, fopen64_freopen64) {
1965 FILE* fp = fopen64("/proc/version", "r");
1966 ASSERT_TRUE(fp != nullptr);
1967 fp = freopen64("/proc/version", "re", fp);
1968 ASSERT_TRUE(fp != nullptr);
1969 fclose(fp);
1970}
1971
Elliott Hughes20841a12014-12-01 16:13:30 -08001972// https://code.google.com/p/android/issues/detail?id=81155
1973// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001974TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001975 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001976 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001977
1978 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07001979 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08001980
1981 char buf[65*1024];
1982 memset(buf, 0xff, sizeof(buf));
1983
Yi Kong32bc0fc2018-08-02 17:31:13 -07001984 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001985 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001986 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001987 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07001988 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001989
1990 fclose(fp);
1991
1992 // 1024 64KiB reads should have been very quick.
1993 ASSERT_LE(t1 - t0, 1);
1994
1995 for (size_t i = 0; i < 64*1024; ++i) {
1996 ASSERT_EQ('\0', buf[i]);
1997 }
1998 for (size_t i = 64*1024; i < 65*1024; ++i) {
1999 ASSERT_EQ('\xff', buf[i]);
2000 }
2001}
Elliott Hughes75b99382015-01-20 11:23:50 -08002002
Christopher Ferris13f26a72016-01-13 13:47:58 -08002003TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002004 std::string digits("0123456789");
2005 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08002006
2007 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
2008 char buf1[4 * 4];
2009 memset(buf1, 0, sizeof(buf1));
2010 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002011 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08002012 ASSERT_TRUE(feof(fp));
2013
2014 rewind(fp);
2015
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002016 // Try to read way too much so stdio tries to read more direct from the stream.
2017 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08002018 memset(buf2, 0, sizeof(buf2));
2019 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08002020 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08002021 ASSERT_TRUE(feof(fp));
2022
2023 fclose(fp);
2024}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002025
2026static void test_fread_from_write_only_stream(size_t n) {
2027 FILE* fp = fopen("/dev/null", "w");
2028 std::vector<char> buf(n, 0);
2029 errno = 0;
2030 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2031 ASSERT_EQ(EBADF, errno);
2032 ASSERT_TRUE(ferror(fp));
2033 ASSERT_FALSE(feof(fp));
2034 fclose(fp);
2035}
2036
Christopher Ferris13f26a72016-01-13 13:47:58 -08002037TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002038 test_fread_from_write_only_stream(1);
2039}
2040
Christopher Ferris13f26a72016-01-13 13:47:58 -08002041TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002042 test_fread_from_write_only_stream(64*1024);
2043}
2044
2045static void test_fwrite_after_fread(size_t n) {
2046 TemporaryFile tf;
2047
2048 FILE* fp = fdopen(tf.fd, "w+");
2049 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2050 fflush(fp);
2051
2052 // We've flushed but not rewound, so there's nothing to read.
2053 std::vector<char> buf(n, 0);
2054 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2055 ASSERT_TRUE(feof(fp));
2056
2057 // But hitting EOF doesn't prevent us from writing...
2058 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002059 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002060
2061 // And if we rewind, everything's there.
2062 rewind(fp);
2063 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2064 ASSERT_EQ('1', buf[0]);
2065 ASSERT_EQ('2', buf[1]);
2066
2067 fclose(fp);
2068}
2069
Christopher Ferris13f26a72016-01-13 13:47:58 -08002070TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002071 test_fwrite_after_fread(16);
2072}
2073
Christopher Ferris13f26a72016-01-13 13:47:58 -08002074TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002075 test_fwrite_after_fread(64*1024);
2076}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002077
2078// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002079TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002080 TemporaryFile tf;
2081
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002082 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002083 ASSERT_TRUE(fp != nullptr);
2084
2085 char file_data[12288];
2086 for (size_t i = 0; i < 12288; i++) {
2087 file_data[i] = i;
2088 }
2089 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2090 fclose(fp);
2091
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002092 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002093 ASSERT_TRUE(fp != nullptr);
2094
2095 char buffer[8192];
2096 size_t cur_location = 0;
2097 // Small read to populate internal buffer.
2098 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2099 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2100
2101 cur_location = static_cast<size_t>(ftell(fp));
2102 // Large read to force reading into the user supplied buffer and bypassing
2103 // the internal buffer.
2104 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2105 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2106
2107 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002108 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002109 cur_location = static_cast<size_t>(ftell(fp));
2110 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2111 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2112
2113 fclose(fp);
2114}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002115
2116// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002117TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002118 TemporaryFile tf;
2119 char buf[6] = {0};
2120
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002121 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002122 ASSERT_TRUE(fw != nullptr);
2123
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002124 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002125 ASSERT_TRUE(fr != nullptr);
2126
2127 fwrite("a", 1, 1, fw);
2128 fflush(fw);
2129 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2130 ASSERT_STREQ("a", buf);
2131
2132 // 'fr' is now at EOF.
2133 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2134 ASSERT_TRUE(feof(fr));
2135
2136 // Write some more...
2137 fwrite("z", 1, 1, fw);
2138 fflush(fw);
2139
2140 // ...and check that we can read it back.
2141 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2142 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2143 ASSERT_STREQ("z", buf);
2144
2145 // But now we're done.
2146 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2147
2148 fclose(fr);
2149 fclose(fw);
2150}
Elliott Hughes923f1652016-01-19 15:46:05 -08002151
2152TEST(STDIO_TEST, fclose_invalidates_fd) {
2153 // The typical error we're trying to help people catch involves accessing
2154 // memory after it's been freed. But we know that stdin/stdout/stderr are
2155 // special and don't get deallocated, so this test uses stdin.
2156 ASSERT_EQ(0, fclose(stdin));
2157
2158 // Even though using a FILE* after close is undefined behavior, I've closed
2159 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2160 // especially because they might actually correspond to a real stream.
2161 errno = 0;
2162 ASSERT_EQ(-1, fileno(stdin));
2163 ASSERT_EQ(EBADF, errno);
2164}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002165
2166TEST(STDIO_TEST, fseek_ftell_unseekable) {
2167#if defined(__BIONIC__) // glibc has fopencookie instead.
2168 auto read_fn = [](void*, char*, int) { return -1; };
2169 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2170 ASSERT_TRUE(fp != nullptr);
2171
2172 // Check that ftell balks on an unseekable FILE*.
2173 errno = 0;
2174 ASSERT_EQ(-1, ftell(fp));
2175 ASSERT_EQ(ESPIPE, errno);
2176
2177 // SEEK_CUR is rewritten as SEEK_SET internally...
2178 errno = 0;
2179 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2180 ASSERT_EQ(ESPIPE, errno);
2181
2182 // ...so it's worth testing the direct seek path too.
2183 errno = 0;
2184 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2185 ASSERT_EQ(ESPIPE, errno);
2186
2187 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002188#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002189 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002190#endif
2191}
2192
2193TEST(STDIO_TEST, funopen_EINVAL) {
2194#if defined(__BIONIC__)
2195 errno = 0;
2196 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2197 ASSERT_EQ(EINVAL, errno);
2198#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002199 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002200#endif
2201}
2202
2203TEST(STDIO_TEST, funopen_seek) {
2204#if defined(__BIONIC__)
2205 auto read_fn = [](void*, char*, int) { return -1; };
2206
2207 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2208 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2209
2210 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2211 ASSERT_TRUE(fp != nullptr);
2212 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002213#if defined(__LP64__)
2214 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2215 EXPECT_EQ(0xfedcba12LL, pos);
2216#else
2217 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2218 EXPECT_EQ(EOVERFLOW, errno);
2219#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002220
2221 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2222 ASSERT_TRUE(fp64 != nullptr);
2223 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002224 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2225 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002226#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002227 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002228#endif
2229}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002230
2231TEST(STDIO_TEST, lots_of_concurrent_files) {
2232 std::vector<TemporaryFile*> tfs;
2233 std::vector<FILE*> fps;
2234
2235 for (size_t i = 0; i < 256; ++i) {
2236 TemporaryFile* tf = new TemporaryFile;
2237 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002238 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002239 fps.push_back(fp);
2240 fprintf(fp, "hello %zu!\n", i);
2241 fflush(fp);
2242 }
2243
2244 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002245 char expected[BUFSIZ];
2246 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002247
Elliott Hughes70715da2016-08-01 16:35:17 -07002248 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002249 fclose(fps[i]);
2250 delete tfs[i];
2251 }
2252}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002253
2254static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2255 EXPECT_EQ(offset, ftell(fp));
2256 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002257 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002258 fpos_t pos;
2259 fpos64_t pos64;
2260 EXPECT_EQ(0, fgetpos(fp, &pos));
2261 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2262#if defined(__BIONIC__)
2263 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2264 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2265#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002266 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002267#endif
2268}
2269
2270TEST(STDIO_TEST, seek_tell_family_smoke) {
2271 TemporaryFile tf;
2272 FILE* fp = fdopen(tf.fd, "w+");
2273
2274 // Initially we should be at 0.
2275 AssertFileOffsetAt(fp, 0);
2276
2277 // Seek to offset 8192.
2278 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2279 AssertFileOffsetAt(fp, 8192);
2280 fpos_t eight_k_pos;
2281 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2282
2283 // Seek forward another 8192...
2284 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2285 AssertFileOffsetAt(fp, 8192 + 8192);
2286 fpos64_t sixteen_k_pos64;
2287 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2288
2289 // Seek back 8192...
2290 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2291 AssertFileOffsetAt(fp, 8192);
2292
2293 // Since we haven't written anything, the end is also at 0.
2294 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2295 AssertFileOffsetAt(fp, 0);
2296
2297 // Check that our fpos64_t from 16KiB works...
2298 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2299 AssertFileOffsetAt(fp, 8192 + 8192);
2300 // ...as does our fpos_t from 8192.
2301 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2302 AssertFileOffsetAt(fp, 8192);
2303
2304 // Do fseeko and fseeko64 work too?
2305 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2306 AssertFileOffsetAt(fp, 1234);
2307 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2308 AssertFileOffsetAt(fp, 5678);
2309
2310 fclose(fp);
2311}
2312
2313TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2314 TemporaryFile tf;
2315 FILE* fp = fdopen(tf.fd, "w+");
2316
2317 // Bad whence.
2318 errno = 0;
2319 ASSERT_EQ(-1, fseek(fp, 0, 123));
2320 ASSERT_EQ(EINVAL, errno);
2321 errno = 0;
2322 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2323 ASSERT_EQ(EINVAL, errno);
2324 errno = 0;
2325 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2326 ASSERT_EQ(EINVAL, errno);
2327
2328 // Bad offset.
2329 errno = 0;
2330 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2331 ASSERT_EQ(EINVAL, errno);
2332 errno = 0;
2333 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2334 ASSERT_EQ(EINVAL, errno);
2335 errno = 0;
2336 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2337 ASSERT_EQ(EINVAL, errno);
2338
2339 fclose(fp);
2340}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002341
2342TEST(STDIO_TEST, ctermid) {
2343 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2344
2345 char buf[L_ctermid] = {};
2346 ASSERT_EQ(buf, ctermid(buf));
2347 ASSERT_STREQ("/dev/tty", buf);
2348}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002349
2350TEST(STDIO_TEST, remove) {
2351 struct stat sb;
2352
2353 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002354 ASSERT_EQ(0, remove(tf.path));
2355 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002356 ASSERT_EQ(ENOENT, errno);
2357
2358 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002359 ASSERT_EQ(0, remove(td.path));
2360 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002361 ASSERT_EQ(ENOENT, errno);
2362
2363 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002364 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002365 ASSERT_EQ(ENOENT, errno);
2366
2367 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002368 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002369 ASSERT_EQ(ENOENT, errno);
2370}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002371
2372TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2373 char buf[16];
2374 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2375 testing::KilledBySignal(SIGABRT),
2376#if defined(NOFORTIFY)
2377 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2378#else
2379 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2380#endif
2381 );
2382}
2383
2384TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2385 std::string buf = "world";
2386 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2387 testing::KilledBySignal(SIGABRT),
2388 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2389}
2390
2391TEST(STDIO_TEST, sprintf_30445072) {
2392 std::string buf = "world";
2393 sprintf(&buf[0], "hello");
2394 ASSERT_EQ(buf, "hello");
2395}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002396
Elliott Hughes654cd832018-08-30 16:00:42 -07002397TEST(STDIO_TEST, printf_m) {
2398 char buf[BUFSIZ];
2399 errno = 0;
2400 snprintf(buf, sizeof(buf), "<%m>");
2401 ASSERT_STREQ("<Success>", buf);
2402 errno = -1;
2403 snprintf(buf, sizeof(buf), "<%m>");
2404 ASSERT_STREQ("<Unknown error -1>", buf);
2405 errno = EINVAL;
2406 snprintf(buf, sizeof(buf), "<%m>");
2407 ASSERT_STREQ("<Invalid argument>", buf);
2408}
2409
Elliott Hughesf340a562018-09-06 10:42:40 -07002410TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2411 char buf[BUFSIZ];
2412 const char* m = strerror(-1);
2413 ASSERT_STREQ("Unknown error -1", m);
2414 errno = -2;
2415 snprintf(buf, sizeof(buf), "<%m>");
2416 ASSERT_STREQ("<Unknown error -2>", buf);
2417 ASSERT_STREQ("Unknown error -1", m);
2418}
2419
Elliott Hughes654cd832018-08-30 16:00:42 -07002420TEST(STDIO_TEST, wprintf_m) {
2421 wchar_t buf[BUFSIZ];
2422 errno = 0;
2423 swprintf(buf, sizeof(buf), L"<%m>");
2424 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2425 errno = -1;
2426 swprintf(buf, sizeof(buf), L"<%m>");
2427 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2428 errno = EINVAL;
2429 swprintf(buf, sizeof(buf), L"<%m>");
2430 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2431}
2432
Elliott Hughesf340a562018-09-06 10:42:40 -07002433TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2434 wchar_t buf[BUFSIZ];
2435 const char* m = strerror(-1);
2436 ASSERT_STREQ("Unknown error -1", m);
2437 errno = -2;
2438 swprintf(buf, sizeof(buf), L"<%m>");
2439 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2440 ASSERT_STREQ("Unknown error -1", m);
2441}
2442
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002443TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2444 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002445 SetFileTo(tf.path, "0123456789");
2446 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002447 EXPECT_EQ(10, ftell(fp));
2448 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2449 EXPECT_EQ(2, ftell(fp));
2450 ASSERT_NE(EOF, fputs("xxx", fp));
2451 ASSERT_EQ(0, fflush(fp));
2452 EXPECT_EQ(13, ftell(fp));
2453 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2454 EXPECT_EQ(13, ftell(fp));
2455 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002456 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002457}
2458
2459TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2460 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002461 SetFileTo(tf.path, "0123456789");
2462 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002463 ASSERT_NE(-1, fd);
2464 // POSIX: "The file position indicator associated with the new stream is set to the position
2465 // indicated by the file offset associated with the file descriptor."
2466 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2467 FILE* fp = fdopen(fd, "a");
2468 EXPECT_EQ(4, ftell(fp));
2469 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2470 EXPECT_EQ(2, ftell(fp));
2471 ASSERT_NE(EOF, fputs("xxx", fp));
2472 ASSERT_EQ(0, fflush(fp));
2473 EXPECT_EQ(13, ftell(fp));
2474 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2475 EXPECT_EQ(13, ftell(fp));
2476 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002477 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002478}
2479
2480TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2481 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002482 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002483 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002484 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002485 EXPECT_EQ(10, ftell(fp));
2486 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2487 EXPECT_EQ(2, ftell(fp));
2488 ASSERT_NE(EOF, fputs("xxx", fp));
2489 ASSERT_EQ(0, fflush(fp));
2490 EXPECT_EQ(13, ftell(fp));
2491 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2492 EXPECT_EQ(13, ftell(fp));
2493 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002494 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002495}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002496
2497TEST(STDIO_TEST, constants) {
2498 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2499 ASSERT_EQ(L_tmpnam, PATH_MAX);
2500}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002501
2502TEST(STDIO_TEST, perror) {
2503 ExecTestHelper eth;
2504 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2505 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2506 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2507}
2508
2509TEST(STDIO_TEST, puts) {
2510 ExecTestHelper eth;
2511 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2512}
2513
2514TEST(STDIO_TEST, unlocked) {
2515 TemporaryFile tf;
2516
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002517 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002518 ASSERT_TRUE(fp != nullptr);
2519
2520 clearerr_unlocked(fp);
2521 ASSERT_FALSE(feof_unlocked(fp));
2522 ASSERT_FALSE(ferror_unlocked(fp));
2523
2524 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2525
2526 ASSERT_NE(EOF, putc_unlocked('a', fp));
2527 ASSERT_NE(EOF, putc('b', fp));
2528 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2529 ASSERT_NE(EOF, fputc('d', fp));
2530
2531 rewind(fp);
2532 ASSERT_EQ('a', getc_unlocked(fp));
2533 ASSERT_EQ('b', getc(fp));
2534 ASSERT_EQ('c', fgetc_unlocked(fp));
2535 ASSERT_EQ('d', fgetc(fp));
2536
2537 rewind(fp);
2538 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2539 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2540 ASSERT_EQ(0, fflush_unlocked(fp));
2541
2542 rewind(fp);
2543 char buf[BUFSIZ] = {};
2544 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2545 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2546 ASSERT_STREQ("ABCD", buf);
2547
2548 rewind(fp);
2549 ASSERT_NE(EOF, fputs("hello ", fp));
2550 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2551 ASSERT_NE(EOF, fputc('\n', fp));
2552
2553 rewind(fp);
2554 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2555 ASSERT_STREQ("hello world\n", buf);
2556
2557 ASSERT_EQ(0, fclose(fp));
2558}
Ryan Prichardbf549862017-11-07 15:30:32 -08002559
2560TEST(STDIO_TEST, fseek_64bit) {
2561 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002562 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002563 ASSERT_TRUE(fp != nullptr);
2564 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2565 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2566 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2567 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2568 ASSERT_EQ(0, fclose(fp));
2569}
2570
2571// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2572// isn't representable in long/off_t.
2573TEST(STDIO_TEST, fseek_overflow_32bit) {
2574 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002575 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002576 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2577
2578 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2579#if defined(__BIONIC__) && !defined(__LP64__)
2580 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2581 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2582 ASSERT_EQ(EOVERFLOW, errno);
2583#endif
2584
2585 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2586 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2587 // and SEEK_END -- many C libraries check neither.)
2588 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2589 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2590
2591 fclose(fp);
2592}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002593
2594TEST(STDIO_TEST, dev_std_files) {
2595 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2596 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002597 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2598 ASSERT_LT(0, length);
2599 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2600
2601 length = readlink("/dev/stdout", path, sizeof(path));
2602 ASSERT_LT(0, length);
2603 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2604
2605 length = readlink("/dev/stderr", path, sizeof(path));
2606 ASSERT_LT(0, length);
2607 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002608}
Ryan Prichardc8e263b2019-04-30 14:47:34 -07002609
2610TEST(STDIO_TEST, fread_with_locked_file) {
2611 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2612 // files locked on other threads, even if it flushes some line-buffered files.
2613 FILE* fp1 = fopen("/dev/zero", "r");
2614 ASSERT_TRUE(fp1 != nullptr);
2615 flockfile(fp1);
2616
2617 std::thread([] {
2618 for (int mode : { _IONBF, _IOLBF }) {
2619 FILE* fp2 = fopen("/dev/zero", "r");
2620 ASSERT_TRUE(fp2 != nullptr);
2621 setvbuf(fp2, nullptr, mode, 0);
2622 ASSERT_EQ('\0', fgetc(fp2));
2623 fclose(fp2);
2624 }
2625 }).join();
2626
2627 funlockfile(fp1);
2628 fclose(fp1);
2629}