blob: 6282ec3ccf6e2e8fe461052fd9277a1096963be6 [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>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080032#include <vector>
33
Elliott Hughesfb3873d2016-08-10 11:07:54 -070034#include "BionicDeathTest.h"
Calin Juravle03e4ebe2014-05-08 14:42:06 +010035#include "TemporaryFile.h"
Josh Gao2f06e102017-01-10 13:00:37 -080036#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070037
Christopher Ferris13f26a72016-01-13 13:47:58 -080038#if defined(NOFORTIFY)
39#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070040#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080041#else
42#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070043#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080044#endif
45
Elliott Hughes3a4c4542017-07-19 17:20:24 -070046using namespace std::string_literals;
47
Elliott Hughesfb3873d2016-08-10 11:07:54 -070048class stdio_DeathTest : public BionicDeathTest {};
49class stdio_nofortify_DeathTest : public BionicDeathTest {};
50
Elliott Hughes33a8cb12017-07-25 18:06:46 -070051static void SetFileTo(const char* path, const char* content) {
52 FILE* fp;
53 ASSERT_NE(nullptr, fp = fopen(path, "w"));
54 ASSERT_NE(EOF, fputs(content, fp));
55 ASSERT_EQ(0, fclose(fp));
56}
57
58static void AssertFileIs(const char* path, const char* expected) {
59 FILE* fp;
60 ASSERT_NE(nullptr, fp = fopen(path, "r"));
61 char* line = nullptr;
62 size_t length;
63 ASSERT_NE(EOF, getline(&line, &length, fp));
64 ASSERT_EQ(0, fclose(fp));
65 ASSERT_STREQ(expected, line);
66 free(line);
67}
68
Elliott Hughes70715da2016-08-01 16:35:17 -070069static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
70 rewind(fp);
71
72 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080073 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070074 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
75 ASSERT_STREQ(expected, line);
76
77 if (is_fmemopen) {
78 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
79 // extra empty line, but does on every C library I tested...
80 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
81 ASSERT_STREQ("", line);
82 }
83
84 // Make sure there isn't anything else in the file.
85 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
86}
87
Christopher Ferris13f26a72016-01-13 13:47:58 -080088TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080089 // Check that we have a _recursive_ mutex for flockfile.
90 flockfile(stderr);
91 feof(stderr); // We don't care about the result, but this needs to take the lock.
92 funlockfile(stderr);
93}
94
Christopher Ferris13f26a72016-01-13 13:47:58 -080095TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080096 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
97 FILE* fp = fopen("/dev/null", "w");
98 ASSERT_TRUE(fp != NULL);
99 flockfile(fp);
100 feof(fp);
101 funlockfile(fp);
102 fclose(fp);
103}
104
Christopher Ferris13f26a72016-01-13 13:47:58 -0800105TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700106 FILE* fp = tmpfile();
107 ASSERT_TRUE(fp != NULL);
108
109 int fd = fileno(fp);
110 ASSERT_NE(fd, -1);
111
112 struct stat sb;
113 int rc = fstat(fd, &sb);
114 ASSERT_NE(rc, -1);
115 ASSERT_EQ(sb.st_mode & 0777, 0600U);
116
117 rc = fprintf(fp, "hello\n");
118 ASSERT_EQ(rc, 6);
119
Elliott Hughes70715da2016-08-01 16:35:17 -0700120 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700121 fclose(fp);
122}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300123
Elliott Hughesf226ee52016-02-03 11:24:28 -0800124TEST(STDIO_TEST, tmpfile64) {
125 FILE* fp = tmpfile64();
126 ASSERT_TRUE(fp != nullptr);
127 fclose(fp);
128}
129
Christopher Ferris13f26a72016-01-13 13:47:58 -0800130TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100131 TemporaryFile tf;
132
133 int rc = dprintf(tf.fd, "hello\n");
134 ASSERT_EQ(rc, 6);
135
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800136 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700137 FILE* tfile = fdopen(tf.fd, "r");
138 ASSERT_TRUE(tfile != NULL);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100139
Elliott Hughes70715da2016-08-01 16:35:17 -0700140 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700141 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100142}
143
Christopher Ferris13f26a72016-01-13 13:47:58 -0800144TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300145 FILE* fp = tmpfile();
146 ASSERT_TRUE(fp != NULL);
147
148 const char* line_written = "This is a test";
149 int rc = fprintf(fp, "%s", line_written);
150 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
151
152 rewind(fp);
153
154 char* word_read = NULL;
155 size_t allocated_length = 0;
156
157 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
158 for (size_t i = 0; i < 5; ++i) {
159 ASSERT_FALSE(feof(fp));
160 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
161 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800162 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300163 }
164 // The last read should have set the end-of-file indicator for the stream.
165 ASSERT_TRUE(feof(fp));
166 clearerr(fp);
167
168 // getdelim returns -1 but doesn't set errno if we're already at EOF.
169 // It should set the end-of-file indicator for the stream, though.
170 errno = 0;
171 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800172 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300173 ASSERT_TRUE(feof(fp));
174
175 free(word_read);
176 fclose(fp);
177}
178
Christopher Ferris13f26a72016-01-13 13:47:58 -0800179TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300180 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800181 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300182
183 char* buffer = NULL;
184 size_t buffer_length = 0;
185
186 // The first argument can't be NULL.
187 errno = 0;
188 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800189 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300190
191 // The second argument can't be NULL.
192 errno = 0;
193 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800194 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300195
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700196 // The underlying fd can't be closed.
197 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300198 errno = 0;
199 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800200 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700201 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300202}
203
Christopher Ferris13f26a72016-01-13 13:47:58 -0800204TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700205 FILE* fp = fopen("/proc", "r");
206 ASSERT_TRUE(fp != NULL);
207 char* word_read;
208 size_t allocated_length;
209 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
210 fclose(fp);
211}
212
Christopher Ferris13f26a72016-01-13 13:47:58 -0800213TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300214 FILE* fp = tmpfile();
215 ASSERT_TRUE(fp != NULL);
216
217 const char* line_written = "This is a test for getline\n";
218 const size_t line_count = 5;
219
220 for (size_t i = 0; i < line_count; ++i) {
221 int rc = fprintf(fp, "%s", line_written);
222 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
223 }
224
225 rewind(fp);
226
227 char* line_read = NULL;
228 size_t allocated_length = 0;
229
230 size_t read_line_count = 0;
231 ssize_t read_char_count;
232 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
233 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
234 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800235 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300236 ++read_line_count;
237 }
238 ASSERT_EQ(read_line_count, line_count);
239
240 // The last read should have set the end-of-file indicator for the stream.
241 ASSERT_TRUE(feof(fp));
242 clearerr(fp);
243
244 // getline returns -1 but doesn't set errno if we're already at EOF.
245 // It should set the end-of-file indicator for the stream, though.
246 errno = 0;
247 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800248 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300249 ASSERT_TRUE(feof(fp));
250
251 free(line_read);
252 fclose(fp);
253}
254
Christopher Ferris13f26a72016-01-13 13:47:58 -0800255TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300256 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800257 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300258
259 char* buffer = NULL;
260 size_t buffer_length = 0;
261
262 // The first argument can't be NULL.
263 errno = 0;
264 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800265 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300266
267 // The second argument can't be NULL.
268 errno = 0;
269 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800270 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300271
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700272 // The underlying fd can't be closed.
273 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300274 errno = 0;
275 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800276 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700277 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300278}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000279
Christopher Ferris13f26a72016-01-13 13:47:58 -0800280TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800281 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800282 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800283 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
284 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000285 // error: format '%zd' expects argument of type 'signed size_t',
286 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
287 ssize_t v = 1;
288 char buf[32];
289 snprintf(buf, sizeof(buf), "%zd", v);
290}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800291
Elliott Hughes05493712014-04-17 17:30:03 -0700292// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800293TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700294 char buf[BUFSIZ];
295 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
296 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
297}
298
Christopher Ferris13f26a72016-01-13 13:47:58 -0800299TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700300 char buf[BUFSIZ];
301 wint_t wc = L'a';
302 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
303 EXPECT_STREQ("<a>", buf);
304}
305
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700306TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
307 char buf[BUFSIZ];
308 wchar_t wc = L'a';
309 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
310 EXPECT_STREQ("<a>", buf);
311}
312
Christopher Ferris13f26a72016-01-13 13:47:58 -0800313TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700314 char buf[BUFSIZ];
315 wchar_t* ws = NULL;
316 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
317 EXPECT_STREQ("<(null)>", buf);
318
319 wchar_t chars[] = { L'h', L'i', 0 };
320 ws = chars;
321 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
322 EXPECT_STREQ("<hi>", buf);
323}
324
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700325TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
326 char buf[BUFSIZ];
327 wchar_t* ws = NULL;
328 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
329 EXPECT_STREQ("<(null)>", buf);
330
331 wchar_t chars[] = { L'h', L'i', 0 };
332 ws = chars;
333 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
334 EXPECT_STREQ("<hi>", buf);
335}
336
Christopher Ferris13f26a72016-01-13 13:47:58 -0800337TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700338#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800339 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700340 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700341 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800342 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700343#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800344 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700345#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700346}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700347
Christopher Ferris13f26a72016-01-13 13:47:58 -0800348TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700349 char buf[BUFSIZ];
350
351 snprintf(buf, sizeof(buf), "a");
352 EXPECT_STREQ("a", buf);
353
354 snprintf(buf, sizeof(buf), "%%");
355 EXPECT_STREQ("%", buf);
356
357 snprintf(buf, sizeof(buf), "01234");
358 EXPECT_STREQ("01234", buf);
359
360 snprintf(buf, sizeof(buf), "a%sb", "01234");
361 EXPECT_STREQ("a01234b", buf);
362
363 char* s = NULL;
364 snprintf(buf, sizeof(buf), "a%sb", s);
365 EXPECT_STREQ("a(null)b", buf);
366
367 snprintf(buf, sizeof(buf), "aa%scc", "bb");
368 EXPECT_STREQ("aabbcc", buf);
369
370 snprintf(buf, sizeof(buf), "a%cc", 'b');
371 EXPECT_STREQ("abc", buf);
372
373 snprintf(buf, sizeof(buf), "a%db", 1234);
374 EXPECT_STREQ("a1234b", buf);
375
376 snprintf(buf, sizeof(buf), "a%db", -8123);
377 EXPECT_STREQ("a-8123b", buf);
378
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700379 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700380 EXPECT_STREQ("a16b", buf);
381
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700382 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700383 EXPECT_STREQ("a16b", buf);
384
385 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
386 EXPECT_STREQ("a68719476736b", buf);
387
388 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
389 EXPECT_STREQ("a70000b", buf);
390
391 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
392 EXPECT_STREQ("a0xb0001234b", buf);
393
394 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
395 EXPECT_STREQ("a12abz", buf);
396
397 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
398 EXPECT_STREQ("a12ABz", buf);
399
400 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
401 EXPECT_STREQ("a00123456z", buf);
402
403 snprintf(buf, sizeof(buf), "a%5dz", 1234);
404 EXPECT_STREQ("a 1234z", buf);
405
406 snprintf(buf, sizeof(buf), "a%05dz", 1234);
407 EXPECT_STREQ("a01234z", buf);
408
409 snprintf(buf, sizeof(buf), "a%8dz", 1234);
410 EXPECT_STREQ("a 1234z", buf);
411
412 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
413 EXPECT_STREQ("a1234 z", buf);
414
415 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
416 EXPECT_STREQ("Aabcdef Z", buf);
417
418 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
419 EXPECT_STREQ("Ahello:1234Z", buf);
420
421 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
422 EXPECT_STREQ("a005:5:05z", buf);
423
424 void* p = NULL;
425 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700426#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700427 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800428#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700429 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800430#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700431
432 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
433 EXPECT_STREQ("a68719476736,6,7,8z", buf);
434
435 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
436 EXPECT_STREQ("a_1.230000_b", buf);
437
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700438 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700439 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400440
441 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
442 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700443}
444
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800445template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700446static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
447 int sscanf_fn(const T*, const T*, ...),
448 const T* fmt_string, const T* fmt, const T* fmt_plus,
449 const T* minus_inf, const T* inf_, const T* plus_inf,
450 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800451 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700452 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700453
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700454 // NaN.
455
456 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800457 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700458 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
459 EXPECT_TRUE(isnan(f));
460
461 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800462 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700463 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
464 EXPECT_TRUE(isnan(f));
465
466 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800467 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700468 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
469 EXPECT_TRUE(isnan(f));
470
471 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800472 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700473 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
474 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800475
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700476 // Inf.
477
478 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800479 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700480 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
481 EXPECT_EQ(HUGE_VALF, f);
482
483 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800484 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700485 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
486 EXPECT_EQ(-HUGE_VALF, f);
487
488 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800489 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700490 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
491 EXPECT_EQ(HUGE_VALF, f);
492
493 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800494 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700495 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
496 EXPECT_EQ(-HUGE_VALF, f);
497
498 // Check case-insensitivity.
499 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
500 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
501 EXPECT_EQ(HUGE_VALF, f);
502 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
503 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
504 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700505}
506
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700507TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
508 CheckInfNan(snprintf, sscanf, "%s",
509 "[%a]", "[%+a]",
510 "[-inf]", "[inf]", "[+inf]",
511 "[-nan]", "[nan]", "[+nan]");
512 CheckInfNan(snprintf, sscanf, "%s",
513 "[%A]", "[%+A]",
514 "[-INF]", "[INF]", "[+INF]",
515 "[-NAN]", "[NAN]", "[+NAN]");
516 CheckInfNan(snprintf, sscanf, "%s",
517 "[%e]", "[%+e]",
518 "[-inf]", "[inf]", "[+inf]",
519 "[-nan]", "[nan]", "[+nan]");
520 CheckInfNan(snprintf, sscanf, "%s",
521 "[%E]", "[%+E]",
522 "[-INF]", "[INF]", "[+INF]",
523 "[-NAN]", "[NAN]", "[+NAN]");
524 CheckInfNan(snprintf, sscanf, "%s",
525 "[%f]", "[%+f]",
526 "[-inf]", "[inf]", "[+inf]",
527 "[-nan]", "[nan]", "[+nan]");
528 CheckInfNan(snprintf, sscanf, "%s",
529 "[%F]", "[%+F]",
530 "[-INF]", "[INF]", "[+INF]",
531 "[-NAN]", "[NAN]", "[+NAN]");
532 CheckInfNan(snprintf, sscanf, "%s",
533 "[%g]", "[%+g]",
534 "[-inf]", "[inf]", "[+inf]",
535 "[-nan]", "[nan]", "[+nan]");
536 CheckInfNan(snprintf, sscanf, "%s",
537 "[%G]", "[%+G]",
538 "[-INF]", "[INF]", "[+INF]",
539 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800540}
Elliott Hughes7823f322014-04-14 12:11:28 -0700541
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700542TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
543 CheckInfNan(swprintf, swscanf, L"%s",
544 L"[%a]", L"[%+a]",
545 L"[-inf]", L"[inf]", L"[+inf]",
546 L"[-nan]", L"[nan]", L"[+nan]");
547 CheckInfNan(swprintf, swscanf, L"%s",
548 L"[%A]", L"[%+A]",
549 L"[-INF]", L"[INF]", L"[+INF]",
550 L"[-NAN]", L"[NAN]", L"[+NAN]");
551 CheckInfNan(swprintf, swscanf, L"%s",
552 L"[%e]", L"[%+e]",
553 L"[-inf]", L"[inf]", L"[+inf]",
554 L"[-nan]", L"[nan]", L"[+nan]");
555 CheckInfNan(swprintf, swscanf, L"%s",
556 L"[%E]", L"[%+E]",
557 L"[-INF]", L"[INF]", L"[+INF]",
558 L"[-NAN]", L"[NAN]", L"[+NAN]");
559 CheckInfNan(swprintf, swscanf, L"%s",
560 L"[%f]", L"[%+f]",
561 L"[-inf]", L"[inf]", L"[+inf]",
562 L"[-nan]", L"[nan]", L"[+nan]");
563 CheckInfNan(swprintf, swscanf, L"%s",
564 L"[%F]", L"[%+F]",
565 L"[-INF]", L"[INF]", L"[+INF]",
566 L"[-NAN]", L"[NAN]", L"[+NAN]");
567 CheckInfNan(swprintf, swscanf, L"%s",
568 L"[%g]", L"[%+g]",
569 L"[-inf]", L"[inf]", L"[+inf]",
570 L"[-nan]", L"[nan]", L"[+nan]");
571 CheckInfNan(swprintf, swscanf, L"%s",
572 L"[%G]", L"[%+G]",
573 L"[-INF]", L"[INF]", L"[+INF]",
574 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700575}
576
Dan Albert9601f162017-08-09 14:59:06 -0700577TEST(STDIO_TEST, swprintf) {
578 constexpr size_t nchars = 32;
579 wchar_t buf[nchars];
580
581 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
582 ASSERT_EQ(std::wstring(L"ab"), buf);
583 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
584 ASSERT_EQ(std::wstring(L"abcde"), buf);
585
586 // Unlike swprintf(), swprintf() returns -1 in case of truncation
587 // and doesn't necessarily zero-terminate the output!
588 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
589
590 const char kString[] = "Hello, World";
591 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
592 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
593 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
594 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
595}
596
597TEST(STDIO_TEST, swprintf_a) {
598 constexpr size_t nchars = 32;
599 wchar_t buf[nchars];
600
601 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
602 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
603}
604
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700605TEST(STDIO_TEST, swprintf_lc) {
606 constexpr size_t nchars = 32;
607 wchar_t buf[nchars];
608
609 wint_t wc = L'a';
610 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
611 EXPECT_EQ(std::wstring(L"<a>"), buf);
612}
613
614TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
615 constexpr size_t nchars = 32;
616 wchar_t buf[nchars];
617
618 wint_t wc = L'a';
619 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
620 EXPECT_EQ(std::wstring(L"<a>"), buf);
621}
622
Elliott Hughes618303c2017-11-02 16:58:44 -0700623TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
624 constexpr size_t nchars = 32;
625 wchar_t buf[nchars];
626
627 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
628 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
629}
630
631TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
632 constexpr size_t nchars = 32;
633 wchar_t buf[nchars];
634
635 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
636 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
637}
638
639TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
640 constexpr size_t nchars = 32;
641 wchar_t buf[nchars];
642
643 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
644 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
645}
646
647TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
648 constexpr size_t nchars = 32;
649 wchar_t buf[nchars];
650
651 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
652 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
653}
654
Dan Albert9601f162017-08-09 14:59:06 -0700655TEST(STDIO_TEST, swprintf_ls) {
656 constexpr size_t nchars = 32;
657 wchar_t buf[nchars];
658
659 static const wchar_t kWideString[] = L"Hello\uff41 World";
660 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
661 ASSERT_EQ(std::wstring(kWideString), buf);
662 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
663 ASSERT_EQ(std::wstring(kWideString), buf);
664}
665
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700666TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
667 constexpr size_t nchars = 32;
668 wchar_t buf[nchars];
669
670 static const wchar_t kWideString[] = L"Hello\uff41 World";
671 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
672 ASSERT_EQ(std::wstring(kWideString), buf);
673 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
674 ASSERT_EQ(std::wstring(kWideString), buf);
675}
676
Christopher Ferris13f26a72016-01-13 13:47:58 -0800677TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700678 char buf[BUFSIZ];
679 snprintf(buf, sizeof(buf), "%d", INT_MAX);
680 EXPECT_STREQ("2147483647", buf);
681}
682
Christopher Ferris13f26a72016-01-13 13:47:58 -0800683TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700684 char buf[BUFSIZ];
685 snprintf(buf, sizeof(buf), "%d", INT_MIN);
686 EXPECT_STREQ("-2147483648", buf);
687}
688
Elliott Hughes618303c2017-11-02 16:58:44 -0700689TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
690 char buf[BUFSIZ];
691 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
692 EXPECT_STREQ("9223372036854775807", buf);
693}
694
695TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
696 char buf[BUFSIZ];
697 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
698 EXPECT_STREQ("-9223372036854775808", buf);
699}
700
701TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
702 char buf[BUFSIZ];
703 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
704 EXPECT_STREQ("18446744073709551615", buf);
705}
706
707TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
708 char buf[BUFSIZ];
709 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
710 EXPECT_STREQ("18446744073709551615", buf);
711}
712
Christopher Ferris13f26a72016-01-13 13:47:58 -0800713TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700714 char buf[BUFSIZ];
715 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700716#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700717 EXPECT_STREQ("9223372036854775807", buf);
718#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700719 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700720#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700721}
722
Christopher Ferris13f26a72016-01-13 13:47:58 -0800723TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700724 char buf[BUFSIZ];
725 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700726#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700727 EXPECT_STREQ("-9223372036854775808", buf);
728#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700729 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700730#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700731}
732
Christopher Ferris13f26a72016-01-13 13:47:58 -0800733TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700734 char buf[BUFSIZ];
735 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
736 EXPECT_STREQ("9223372036854775807", buf);
737}
738
Christopher Ferris13f26a72016-01-13 13:47:58 -0800739TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700740 char buf[BUFSIZ];
741 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
742 EXPECT_STREQ("-9223372036854775808", buf);
743}
744
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700745TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
746 char buf[BUFSIZ];
747 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
748 EXPECT_STREQ("37777777777", buf);
749}
750
751TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
752 char buf[BUFSIZ];
753 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
754 EXPECT_STREQ("4294967295", buf);
755}
756
757TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
758 char buf[BUFSIZ];
759 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
760 EXPECT_STREQ("ffffffff", buf);
761}
762
763TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
764 char buf[BUFSIZ];
765 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
766 EXPECT_STREQ("FFFFFFFF", buf);
767}
768
Christopher Ferris13f26a72016-01-13 13:47:58 -0800769TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700770 char buf[BUFSIZ];
771
772 snprintf(buf, sizeof(buf), "%e", 1.5);
773 EXPECT_STREQ("1.500000e+00", buf);
774
775 snprintf(buf, sizeof(buf), "%Le", 1.5l);
776 EXPECT_STREQ("1.500000e+00", buf);
777}
778
Christopher Ferris13f26a72016-01-13 13:47:58 -0800779TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700780 char buf[BUFSIZ];
781
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800782 snprintf(buf, sizeof(buf), "%e", -0.0);
783 EXPECT_STREQ("-0.000000e+00", buf);
784 snprintf(buf, sizeof(buf), "%E", -0.0);
785 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700786 snprintf(buf, sizeof(buf), "%f", -0.0);
787 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800788 snprintf(buf, sizeof(buf), "%F", -0.0);
789 EXPECT_STREQ("-0.000000", buf);
790 snprintf(buf, sizeof(buf), "%g", -0.0);
791 EXPECT_STREQ("-0", buf);
792 snprintf(buf, sizeof(buf), "%G", -0.0);
793 EXPECT_STREQ("-0", buf);
794 snprintf(buf, sizeof(buf), "%a", -0.0);
795 EXPECT_STREQ("-0x0p+0", buf);
796 snprintf(buf, sizeof(buf), "%A", -0.0);
797 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700798}
799
Christopher Ferris13f26a72016-01-13 13:47:58 -0800800TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700801 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700802 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700803
Elliott Hughes69f05d22014-06-05 20:10:09 -0700804 // http://b/15439554
805 char buf[BUFSIZ];
806
807 // 1-byte character.
808 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
809 EXPECT_STREQ("1x2", buf);
810 // 2-byte character.
811 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
812 EXPECT_STREQ("1¢2", buf);
813 // 3-byte character.
814 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
815 EXPECT_STREQ("1€2", buf);
816 // 4-byte character.
817 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
818 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700819
Wally Yaua40fdbd2014-08-26 09:47:23 -0700820 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700821 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700822}
823
Elliott Hughes43f7c872016-02-05 11:18:41 -0800824static void* snprintf_small_stack_fn(void*) {
825 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
826 char buf[PATH_MAX];
827 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
828 return nullptr;
829}
830
831TEST(STDIO_TEST, snprintf_small_stack) {
832 // Is it safe to call snprintf on a thread with a small stack?
833 // (The snprintf implementation puts some pretty large buffers on the stack.)
834 pthread_attr_t a;
835 ASSERT_EQ(0, pthread_attr_init(&a));
836 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
837
838 pthread_t t;
839 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
840 ASSERT_EQ(0, pthread_join(t, nullptr));
841}
842
Elliott Hughes8200e552016-02-05 21:57:37 -0800843TEST(STDIO_TEST, snprintf_asterisk_overflow) {
844 char buf[128];
845 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
846 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
847 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
848 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
849 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
850
851 // INT_MAX-1, INT_MAX, INT_MAX+1.
852 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
853 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
854 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
855 ASSERT_EQ(ENOMEM, errno);
856}
857
Elliott Hughes70715da2016-08-01 16:35:17 -0700858TEST(STDIO_TEST, fprintf) {
859 TemporaryFile tf;
860
861 FILE* tfile = fdopen(tf.fd, "r+");
862 ASSERT_TRUE(tfile != nullptr);
863
864 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
865 AssertFileIs(tfile, "123 abc");
866 fclose(tfile);
867}
868
Christopher Ferris13f26a72016-01-13 13:47:58 -0800869TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700870 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700871 FILE* fp;
872
873 // Unbuffered case where the fprintf(3) itself fails.
874 ASSERT_NE(nullptr, fp = tmpfile());
875 setbuf(fp, NULL);
876 ASSERT_EQ(4, fprintf(fp, "epic"));
877 ASSERT_EQ(0, close(fileno(fp)));
878 ASSERT_EQ(-1, fprintf(fp, "fail"));
879 ASSERT_EQ(-1, fclose(fp));
880
881 // Buffered case where we won't notice until the fclose(3).
882 // It's likely this is what was actually seen in http://b/7229520,
883 // and that expecting fprintf to fail is setting yourself up for
884 // disappointment. Remember to check fclose(3)'s return value, kids!
885 ASSERT_NE(nullptr, fp = tmpfile());
886 ASSERT_EQ(4, fprintf(fp, "epic"));
887 ASSERT_EQ(0, close(fileno(fp)));
888 ASSERT_EQ(4, fprintf(fp, "fail"));
889 ASSERT_EQ(-1, fclose(fp));
890}
891
Elliott Hughes468efc82018-07-10 14:39:49 -0700892TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800893 FILE* fp = popen("cat /proc/version", "r");
894 ASSERT_TRUE(fp != NULL);
895
896 char buf[16];
897 char* s = fgets(buf, sizeof(buf), fp);
898 buf[13] = '\0';
899 ASSERT_STREQ("Linux version", s);
900
901 ASSERT_EQ(0, pclose(fp));
902}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700903
Elliott Hughes468efc82018-07-10 14:39:49 -0700904TEST(STDIO_TEST, popen_socketpair) {
905 FILE* fp = popen("cat", "r+");
906 ASSERT_TRUE(fp != NULL);
907
908 fputs("hello\nworld\n", fp);
909 fflush(fp);
910
911 char buf[16];
912 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
913 EXPECT_STREQ("hello\n", buf);
914 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
915 EXPECT_STREQ("world\n", buf);
916
917 ASSERT_EQ(0, pclose(fp));
918}
919
920TEST(STDIO_TEST, popen_socketpair_shutdown) {
921 FILE* fp = popen("uniq -c", "r+");
922 ASSERT_TRUE(fp != NULL);
923
924 fputs("a\na\na\na\nb\n", fp);
925 fflush(fp);
926 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
927
928 char buf[16];
929 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
930 EXPECT_STREQ(" 4 a\n", buf);
931 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
932 EXPECT_STREQ(" 1 b\n", buf);
933
934 ASSERT_EQ(0, pclose(fp));
935}
936
937TEST(STDIO_TEST, popen_return_value_0) {
938 FILE* fp = popen("true", "r");
939 ASSERT_TRUE(fp != NULL);
940 int status = pclose(fp);
941 EXPECT_TRUE(WIFEXITED(status));
942 EXPECT_EQ(0, WEXITSTATUS(status));
943}
944
945TEST(STDIO_TEST, popen_return_value_1) {
946 FILE* fp = popen("false", "r");
947 ASSERT_TRUE(fp != NULL);
948 int status = pclose(fp);
949 EXPECT_TRUE(WIFEXITED(status));
950 EXPECT_EQ(1, WEXITSTATUS(status));
951}
952
953TEST(STDIO_TEST, popen_return_value_signal) {
954 FILE* fp = popen("kill -7 $$", "r");
955 ASSERT_TRUE(fp != NULL);
956 int status = pclose(fp);
957 EXPECT_TRUE(WIFSIGNALED(status));
958 EXPECT_EQ(7, WTERMSIG(status));
959}
960
Christopher Ferris13f26a72016-01-13 13:47:58 -0800961TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700962 FILE* fp = fopen("/proc/version", "r");
963 ASSERT_TRUE(fp != NULL);
964 ASSERT_EQ('L', getc(fp));
965 ASSERT_EQ('i', getc(fp));
966 ASSERT_EQ('n', getc(fp));
967 ASSERT_EQ('u', getc(fp));
968 ASSERT_EQ('x', getc(fp));
969 fclose(fp);
970}
971
Christopher Ferris13f26a72016-01-13 13:47:58 -0800972TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700973 FILE* fp = fopen("/proc/version", "r");
974 ASSERT_TRUE(fp != NULL);
975 ASSERT_EQ(EOF, putc('x', fp));
976 fclose(fp);
977}
Elliott Hughes603332f2014-03-12 17:10:41 -0700978
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700979TEST(STDIO_TEST, sscanf_swscanf) {
980 struct stuff {
981 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800982 int i1, i2;
983 char cs1[3];
984 char s2[3];
985 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700986 double d1;
987 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800988 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700989
990 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800991 EXPECT_STREQ("hello", s1);
992 EXPECT_EQ(123, i1);
993 EXPECT_EQ(456, i2);
994 EXPECT_EQ('a', cs1[0]);
995 EXPECT_EQ('b', cs1[1]);
996 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
997 EXPECT_STREQ("AB", s2); // Terminating NUL.
998 EXPECT_EQ('!', c1);
999 EXPECT_DOUBLE_EQ(1.23, d1);
1000 EXPECT_FLOAT_EQ(9.0f, f1);
1001 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -07001002 }
1003 } s;
1004
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001005 memset(&s, 'x', sizeof(s));
1006 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1007 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1008 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 -07001009 s.Check();
1010
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001011 memset(&s, 'x', sizeof(s));
1012 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1013 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1014 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 -07001015 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001016}
Elliott Hughes53b24382014-05-02 18:29:25 -07001017
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001018template <typename T>
1019static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1020 const T* input, const T* fmt,
1021 int expected_count, const char* expected_string) {
1022 char buf[256] = {};
1023 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1024 ASSERT_STREQ(expected_string, buf) << fmt;
1025}
1026
1027TEST(STDIO_TEST, sscanf_ccl) {
1028 // `abc` is just those characters.
1029 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1030 // `a-c` is the range 'a' .. 'c'.
1031 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1032 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1033 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1034 // `a-c-e` is equivalent to `a-e`.
1035 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1036 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1037 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1038 // An initial '^' negates the set.
1039 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1040 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1041 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1042 // The first character may be ']' or '-' without being special.
1043 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1044 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1045 // The last character may be '-' without being special.
1046 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1047 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1048 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1049}
1050
1051TEST(STDIO_TEST, swscanf_ccl) {
1052 // `abc` is just those characters.
1053 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1054 // `a-c` is the range 'a' .. 'c'.
1055 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1056 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1057 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1058 // `a-c-e` is equivalent to `a-e`.
1059 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1060 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1061 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1062 // An initial '^' negates the set.
1063 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1064 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1065 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1066 // The first character may be ']' or '-' without being special.
1067 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1068 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1069 // The last character may be '-' without being special.
1070 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1071 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1072 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1073}
1074
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001075template <typename T1, typename T2>
1076static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1077 const T1* input, const T1* fmt,
1078 int expected_count, const T2* expected_string) {
1079 T2* result = nullptr;
1080 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1081 if (expected_string == nullptr) {
1082 ASSERT_EQ(nullptr, result);
1083 } else {
1084 ASSERT_STREQ(expected_string, result) << fmt;
1085 }
1086 free(result);
1087}
1088
1089TEST(STDIO_TEST, sscanf_mc) {
1090 char* p1 = nullptr;
1091 char* p2 = nullptr;
1092 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1093 ASSERT_EQ('h', *p1);
1094 ASSERT_EQ('e', *p2);
1095 free(p1);
1096 free(p2);
1097
1098 p1 = nullptr;
1099 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1100 ASSERT_EQ('h', p1[0]);
1101 ASSERT_EQ('e', p1[1]);
1102 ASSERT_EQ('l', p1[2]);
1103 ASSERT_EQ('l', p1[3]);
1104 free(p1);
1105
1106 p1 = nullptr;
1107 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1108 ASSERT_EQ('h', p1[0]);
1109 ASSERT_EQ('e', p1[1]);
1110 ASSERT_EQ('l', p1[2]);
1111 ASSERT_EQ('l', p1[3]);
1112 ASSERT_EQ('o', p1[4]);
1113 free(p1);
1114}
1115
1116
1117TEST(STDIO_TEST, sscanf_mlc) {
1118 // This is so useless that clang doesn't even believe it exists...
1119#pragma clang diagnostic push
1120#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1121#pragma clang diagnostic ignored "-Wformat-extra-args"
1122
1123 wchar_t* p1 = nullptr;
1124 wchar_t* p2 = nullptr;
1125 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1126 ASSERT_EQ(L'h', *p1);
1127 ASSERT_EQ(L'e', *p2);
1128 free(p1);
1129 free(p2);
1130
1131 p1 = nullptr;
1132 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1133 ASSERT_EQ(L'h', p1[0]);
1134 ASSERT_EQ(L'e', p1[1]);
1135 ASSERT_EQ(L'l', p1[2]);
1136 ASSERT_EQ(L'l', p1[3]);
1137 free(p1);
1138
1139 p1 = nullptr;
1140 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1141 ASSERT_EQ(L'h', p1[0]);
1142 ASSERT_EQ(L'e', p1[1]);
1143 ASSERT_EQ(L'l', p1[2]);
1144 ASSERT_EQ(L'l', p1[3]);
1145 ASSERT_EQ(L'o', p1[4]);
1146 free(p1);
1147#pragma clang diagnostic pop
1148}
1149
1150
1151TEST(STDIO_TEST, sscanf_ms) {
1152 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1153 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1154 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1155}
1156
1157TEST(STDIO_TEST, sscanf_mls) {
1158 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1159 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1160 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1161}
1162
1163TEST(STDIO_TEST, sscanf_m_ccl) {
1164 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1165 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1166 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1167}
1168
1169TEST(STDIO_TEST, sscanf_ml_ccl) {
1170 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1171 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1172 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1173}
1174
1175TEST(STDIO_TEST, sscanf_ls) {
1176 wchar_t w[32] = {};
1177 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1178 ASSERT_EQ(L"hello", std::wstring(w));
1179}
1180
1181TEST(STDIO_TEST, sscanf_ls_suppress) {
1182 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1183}
1184
1185TEST(STDIO_TEST, sscanf_ls_n) {
1186 setlocale(LC_ALL, "C.UTF-8");
1187 wchar_t w[32] = {};
1188 int pos = 0;
1189 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1190 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1191 ASSERT_EQ(2, pos);
1192}
1193
1194TEST(STDIO_TEST, sscanf_ls_realloc) {
1195 // This is so useless that clang doesn't even believe it exists...
1196#pragma clang diagnostic push
1197#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1198#pragma clang diagnostic ignored "-Wformat-extra-args"
1199 wchar_t* p1 = nullptr;
1200 wchar_t* p2 = nullptr;
1201 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1202 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1203 ASSERT_EQ(L"world", std::wstring(p2));
1204#pragma clang diagnostic pop
1205}
1206
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001207// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1208TEST(STDIO_TEST, scanf_wscanf_EOF) {
1209 EXPECT_EQ(0, sscanf("b", "ab"));
1210 EXPECT_EQ(EOF, sscanf("", "a"));
1211 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1212 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1213}
1214
1215TEST(STDIO_TEST, scanf_invalid_UTF8) {
1216#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1217 char buf[BUFSIZ];
1218 wchar_t wbuf[BUFSIZ];
1219
1220 memset(buf, 0, sizeof(buf));
1221 memset(wbuf, 0, sizeof(wbuf));
1222 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1223#endif
1224}
1225
1226TEST(STDIO_TEST, scanf_no_match_no_termination) {
1227 char buf[4] = "x";
1228 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1229 EXPECT_EQ('x', buf[0]);
1230 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1231 EXPECT_EQ('x', buf[0]);
1232
1233 wchar_t wbuf[4] = L"x";
1234 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1235 EXPECT_EQ(L'x', wbuf[0]);
1236
1237 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1238 EXPECT_EQ('x', buf[0]);
1239
1240 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1241 EXPECT_EQ(L'x', wbuf[0]);
1242}
1243
1244TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1245#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1246 wchar_t buf[BUFSIZ];
1247
1248 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1249 memset(buf, 0, sizeof(buf));
1250 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1251 EXPECT_EQ(L"x"s, std::wstring(buf));
1252 memset(buf, 0, sizeof(buf));
1253 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1254 EXPECT_EQ(L"x"s, std::wstring(buf));
1255
1256 // Even if scanf has wide characters in a class, they won't match...
1257 // TODO: is that a bug?
1258 memset(buf, 0, sizeof(buf));
1259 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1260 EXPECT_EQ(L"x"s, std::wstring(buf));
1261 // ...unless you use wscanf.
1262 memset(buf, 0, sizeof(buf));
1263 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1264 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1265
1266 // Negation only covers ASCII for scanf...
1267 memset(buf, 0, sizeof(buf));
1268 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1269 EXPECT_EQ(L"x"s, std::wstring(buf));
1270 // ...but covers wide characters for wscanf.
1271 memset(buf, 0, sizeof(buf));
1272 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1273 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1274
1275 // We already determined that non-ASCII characters are ignored in scanf classes.
1276 memset(buf, 0, sizeof(buf));
1277 EXPECT_EQ(1, sscanf("x"
1278 "\xc4\x80" // Matches a byte from each wide char in the class.
1279 "\xc6\x82" // Neither byte is in the class.
1280 "yz",
1281 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1282 EXPECT_EQ(L"x", std::wstring(buf));
1283 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1284 memset(buf, 0, sizeof(buf));
1285 EXPECT_EQ(1, swscanf(L"x"
1286 L"\xc4\x80"
1287 L"\xc6\x82"
1288 L"yz",
1289 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1290 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1291 // not put back together as a wide character.
1292 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1293#endif
1294}
1295
Christopher Ferris13f26a72016-01-13 13:47:58 -08001296TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001297 // If we open a file read-only...
1298 FILE* fp = fopen("/proc/version", "r");
1299
1300 // ...all attempts to write to that file should return failure.
1301
1302 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1303 // glibc gets the wide-character functions wrong.
1304
1305 errno = 0;
1306 EXPECT_EQ(EOF, putc('x', fp));
1307 EXPECT_EQ(EBADF, errno);
1308
1309 errno = 0;
1310 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1311 EXPECT_EQ(EBADF, errno);
1312
1313 errno = 0;
1314 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001315#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001316 EXPECT_EQ(EBADF, errno);
1317#endif
1318
1319 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001320 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1321 EXPECT_EQ(EBADF, errno);
1322
1323 errno = 0;
1324 EXPECT_EQ(EOF, fputs("hello", fp));
1325 EXPECT_EQ(EBADF, errno);
1326
1327 errno = 0;
1328 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001329#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001330 EXPECT_EQ(EBADF, errno);
1331#endif
1332}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001333
1334// Tests that we can only have a consistent and correct fpos_t when using
1335// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001336TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001337 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1338 uselocale(LC_GLOBAL_LOCALE);
1339
1340 FILE* fp = tmpfile();
1341 ASSERT_TRUE(fp != NULL);
1342
1343 wchar_t mb_one_bytes = L'h';
1344 wchar_t mb_two_bytes = 0x00a2;
1345 wchar_t mb_three_bytes = 0x20ac;
1346 wchar_t mb_four_bytes = 0x24b62;
1347
1348 // Write to file.
1349 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1350 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1351 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1352 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1353
1354 rewind(fp);
1355
1356 // Record each character position.
1357 fpos_t pos1;
1358 fpos_t pos2;
1359 fpos_t pos3;
1360 fpos_t pos4;
1361 fpos_t pos5;
1362 EXPECT_EQ(0, fgetpos(fp, &pos1));
1363 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1364 EXPECT_EQ(0, fgetpos(fp, &pos2));
1365 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1366 EXPECT_EQ(0, fgetpos(fp, &pos3));
1367 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1368 EXPECT_EQ(0, fgetpos(fp, &pos4));
1369 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1370 EXPECT_EQ(0, fgetpos(fp, &pos5));
1371
Elliott Hughes063525c2014-05-13 11:19:57 -07001372#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001373 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1374 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1375 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1376 // structure.
1377 ASSERT_EQ(0, static_cast<off_t>(pos1));
1378 ASSERT_EQ(1, static_cast<off_t>(pos2));
1379 ASSERT_EQ(3, static_cast<off_t>(pos3));
1380 ASSERT_EQ(6, static_cast<off_t>(pos4));
1381 ASSERT_EQ(10, static_cast<off_t>(pos5));
1382#endif
1383
1384 // Exercise back and forth movements of the position.
1385 ASSERT_EQ(0, fsetpos(fp, &pos2));
1386 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1387 ASSERT_EQ(0, fsetpos(fp, &pos1));
1388 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1389 ASSERT_EQ(0, fsetpos(fp, &pos4));
1390 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1391 ASSERT_EQ(0, fsetpos(fp, &pos3));
1392 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1393 ASSERT_EQ(0, fsetpos(fp, &pos5));
1394 ASSERT_EQ(WEOF, fgetwc(fp));
1395
1396 fclose(fp);
1397}
1398
1399// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001400TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001401 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1402 uselocale(LC_GLOBAL_LOCALE);
1403
Calin Juravle9b95ea92014-05-14 17:07:10 +01001404 // In glibc-2.16 fseek doesn't work properly in wide mode
1405 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1406 // to close and re-open the file. We do it in order to make the test pass
1407 // with all glibcs.
1408
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001409 TemporaryFile tf;
1410 FILE* fp = fdopen(tf.fd, "w+");
1411 ASSERT_TRUE(fp != NULL);
1412
1413 wchar_t mb_two_bytes = 0x00a2;
1414 wchar_t mb_three_bytes = 0x20ac;
1415 wchar_t mb_four_bytes = 0x24b62;
1416
1417 // Write to file.
1418 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1419 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1420 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1421
1422 fflush(fp);
1423 fclose(fp);
1424
1425 fp = fopen(tf.filename, "r");
1426 ASSERT_TRUE(fp != NULL);
1427
1428 // Store a valid position.
1429 fpos_t mb_two_bytes_pos;
1430 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1431
1432 // Move inside mb_four_bytes with fseek.
1433 long offset_inside_mb = 6;
1434 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1435
1436 // Store the "inside multi byte" position.
1437 fpos_t pos_inside_mb;
1438 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001439#if defined(__BIONIC__)
1440 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1441#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001442
1443 // Reading from within a byte should produce an error.
1444 ASSERT_EQ(WEOF, fgetwc(fp));
1445 ASSERT_EQ(EILSEQ, errno);
1446
1447 // Reverting to a valid position should work.
1448 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1449 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1450
1451 // Moving withing a multi byte with fsetpos should work but reading should
1452 // produce an error.
1453 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1454 ASSERT_EQ(WEOF, fgetwc(fp));
1455 ASSERT_EQ(EILSEQ, errno);
1456
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001457 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001458}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001459
Christopher Ferris13f26a72016-01-13 13:47:58 -08001460TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001461 char buf[16];
1462 memset(buf, 0, sizeof(buf));
1463 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1464 ASSERT_EQ('<', fputc('<', fp));
1465 ASSERT_NE(EOF, fputs("abc>\n", fp));
1466 fflush(fp);
1467
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001468 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001469 ASSERT_STREQ("<abc>\n", buf);
1470
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001471 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001472 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001473 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001474}
1475
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001476TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001477 FILE* fp = fmemopen(nullptr, 128, "r+");
1478 ASSERT_NE(EOF, fputs("xyz\n", fp));
1479
Elliott Hughes70715da2016-08-01 16:35:17 -07001480 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001481 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001482}
1483
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001484TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1485 FILE* fp;
1486 char buf[8];
1487
1488 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1489 // shall be written at the current position or at the end of the buffer,
1490 // depending on the size of the contents."
1491 memset(buf, 'x', sizeof(buf));
1492 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1493 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1494 ASSERT_EQ(0, fflush(fp));
1495 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1496 // Now write and check that the NUL moves along with our writes...
1497 ASSERT_NE(EOF, fputs("hello", fp));
1498 ASSERT_EQ(0, fflush(fp));
1499 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1500 ASSERT_NE(EOF, fputs("wo", fp));
1501 ASSERT_EQ(0, fflush(fp));
1502 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1503 ASSERT_EQ(0, fclose(fp));
1504
1505 // "If a stream open for update is flushed or closed and the last write has
1506 // advanced the current buffer size, a null byte shall be written at the end
1507 // of the buffer if it fits."
1508 memset(buf, 'x', sizeof(buf));
1509 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1510 // Nothing written yet, so no advance...
1511 ASSERT_EQ(0, fflush(fp));
1512 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1513 ASSERT_NE(EOF, fputs("hello", fp));
1514 ASSERT_EQ(0, fclose(fp));
1515}
1516
1517TEST(STDIO_TEST, fmemopen_size) {
1518 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001519 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001520 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001521
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001522 // POSIX: "The stream shall also maintain the size of the current buffer
1523 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1524 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001525
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001526 // "For modes r and r+ the size shall be set to the value given by the size
1527 // argument."
1528 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1529 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1530 EXPECT_EQ(16, ftell(fp));
1531 EXPECT_EQ(16, ftello(fp));
1532 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1533 EXPECT_EQ(16, ftell(fp));
1534 EXPECT_EQ(16, ftello(fp));
1535 ASSERT_EQ(0, fclose(fp));
1536 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1537 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1538 EXPECT_EQ(16, ftell(fp));
1539 EXPECT_EQ(16, ftello(fp));
1540 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1541 EXPECT_EQ(16, ftell(fp));
1542 EXPECT_EQ(16, ftello(fp));
1543 ASSERT_EQ(0, fclose(fp));
1544
1545 // "For modes w and w+ the initial size shall be zero..."
1546 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1547 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1548 EXPECT_EQ(0, ftell(fp));
1549 EXPECT_EQ(0, ftello(fp));
1550 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1551 EXPECT_EQ(0, ftell(fp));
1552 EXPECT_EQ(0, ftello(fp));
1553 ASSERT_EQ(0, fclose(fp));
1554 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1555 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1556 EXPECT_EQ(0, ftell(fp));
1557 EXPECT_EQ(0, ftello(fp));
1558 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1559 EXPECT_EQ(0, ftell(fp));
1560 EXPECT_EQ(0, ftello(fp));
1561 ASSERT_EQ(0, fclose(fp));
1562
1563 // "...and for modes a and a+ the initial size shall be:
1564 // 1. Zero, if buf is a null pointer
1565 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1566 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1567 EXPECT_EQ(0, ftell(fp));
1568 EXPECT_EQ(0, ftello(fp));
1569 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1570 EXPECT_EQ(0, ftell(fp));
1571 EXPECT_EQ(0, ftello(fp));
1572 ASSERT_EQ(0, fclose(fp));
1573 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1574 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1575 EXPECT_EQ(0, ftell(fp));
1576 EXPECT_EQ(0, ftello(fp));
1577 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1578 EXPECT_EQ(0, ftell(fp));
1579 EXPECT_EQ(0, ftello(fp));
1580 ASSERT_EQ(0, fclose(fp));
1581
1582 // 2. The position of the first null byte in the buffer, if one is found
1583 memset(buf, 'x', sizeof(buf));
1584 buf[3] = '\0';
1585 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1586 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1587 EXPECT_EQ(3, ftell(fp));
1588 EXPECT_EQ(3, ftello(fp));
1589 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1590 EXPECT_EQ(3, ftell(fp));
1591 EXPECT_EQ(3, ftello(fp));
1592 ASSERT_EQ(0, fclose(fp));
1593 memset(buf, 'x', sizeof(buf));
1594 buf[3] = '\0';
1595 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1596 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1597 EXPECT_EQ(3, ftell(fp));
1598 EXPECT_EQ(3, ftello(fp));
1599 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1600 EXPECT_EQ(3, ftell(fp));
1601 EXPECT_EQ(3, ftello(fp));
1602 ASSERT_EQ(0, fclose(fp));
1603
1604 // 3. The value of the size argument, if buf is not a null pointer and no
1605 // null byte is found.
1606 memset(buf, 'x', sizeof(buf));
1607 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1608 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1609 EXPECT_EQ(16, ftell(fp));
1610 EXPECT_EQ(16, ftello(fp));
1611 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1612 EXPECT_EQ(16, ftell(fp));
1613 EXPECT_EQ(16, ftello(fp));
1614 ASSERT_EQ(0, fclose(fp));
1615 memset(buf, 'x', sizeof(buf));
1616 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1617 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1618 EXPECT_EQ(16, ftell(fp));
1619 EXPECT_EQ(16, ftello(fp));
1620 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1621 EXPECT_EQ(16, ftell(fp));
1622 EXPECT_EQ(16, ftello(fp));
1623 ASSERT_EQ(0, fclose(fp));
1624}
1625
1626TEST(STDIO_TEST, fmemopen_SEEK_END) {
1627 // fseek SEEK_END is relative to the current string length, not the buffer size.
1628 FILE* fp;
1629 char buf[8];
1630 memset(buf, 'x', sizeof(buf));
1631 strcpy(buf, "str");
1632 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1633 ASSERT_NE(EOF, fputs("string", fp));
1634 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1635 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1636 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1637 EXPECT_EQ(0, fclose(fp));
1638
1639 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1640 // than adding).
1641 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1642 ASSERT_NE(EOF, fputs("54321", fp));
1643 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1644 EXPECT_EQ('2', fgetc(fp));
1645 EXPECT_EQ(0, fclose(fp));
1646}
1647
1648TEST(STDIO_TEST, fmemopen_seek_invalid) {
1649 char buf[8];
1650 memset(buf, 'x', sizeof(buf));
1651 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1652 ASSERT_TRUE(fp != nullptr);
1653
1654 // POSIX: "An attempt to seek ... to a negative position or to a position
1655 // larger than the buffer size given in the size argument shall fail."
1656 // (There's no mention of what errno should be set to, and glibc doesn't
1657 // set errno in any of these cases.)
1658 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1659 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1660 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1661 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1662}
1663
1664TEST(STDIO_TEST, fmemopen_read_EOF) {
1665 // POSIX: "A read operation on the stream shall not advance the current
1666 // buffer position beyond the current buffer size."
1667 char buf[8];
1668 memset(buf, 'x', sizeof(buf));
1669 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1670 ASSERT_TRUE(fp != nullptr);
1671 char buf2[BUFSIZ];
1672 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1673 // POSIX: "Reaching the buffer size in a read operation shall count as
1674 // end-of-file.
1675 ASSERT_TRUE(feof(fp));
1676 ASSERT_EQ(EOF, fgetc(fp));
1677 ASSERT_EQ(0, fclose(fp));
1678}
1679
1680TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1681 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1682 char buf[] = "h\0e\0l\0l\0o";
1683 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1684 ASSERT_TRUE(fp != nullptr);
1685 ASSERT_EQ('h', fgetc(fp));
1686 ASSERT_EQ(0, fgetc(fp));
1687 ASSERT_EQ('e', fgetc(fp));
1688 ASSERT_EQ(0, fgetc(fp));
1689 ASSERT_EQ('l', fgetc(fp));
1690 ASSERT_EQ(0, fgetc(fp));
1691 // POSIX: "The read operation shall start at the current buffer position of
1692 // the stream."
1693 char buf2[8];
1694 memset(buf2, 'x', sizeof(buf2));
1695 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1696 ASSERT_EQ('l', buf2[0]);
1697 ASSERT_EQ(0, buf2[1]);
1698 ASSERT_EQ('o', buf2[2]);
1699 ASSERT_EQ(0, buf2[3]);
1700 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1701 ASSERT_TRUE(feof(fp));
1702 ASSERT_EQ(0, fclose(fp));
1703}
1704
1705TEST(STDIO_TEST, fmemopen_write) {
1706 FILE* fp;
1707 char buf[8];
1708
1709 // POSIX: "A write operation shall start either at the current position of
1710 // the stream (if mode has not specified 'a' as the first character)..."
1711 memset(buf, 'x', sizeof(buf));
1712 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1713 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1714 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1715 ASSERT_EQ(' ', fputc(' ', fp));
1716 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1717 ASSERT_EQ(0, fclose(fp));
1718
1719 // "...or at the current size of the stream (if mode had 'a' as the first
1720 // character)." (See the fmemopen_size test for what "size" means, but for
1721 // mode "a", it's the first NUL byte.)
1722 memset(buf, 'x', sizeof(buf));
1723 buf[3] = '\0';
1724 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1725 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1726 ASSERT_EQ(' ', fputc(' ', fp));
1727 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1728 ASSERT_EQ(0, fclose(fp));
1729
1730 // "If the current position at the end of the write is larger than the
1731 // current buffer size, the current buffer size shall be set to the current
1732 // position." (See the fmemopen_size test for what "size" means, but to
1733 // query it we SEEK_END with offset 0, and then ftell.)
1734 memset(buf, 'x', sizeof(buf));
1735 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1736 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1737 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1738 EXPECT_EQ(0, ftell(fp));
1739 ASSERT_EQ(' ', fputc(' ', fp));
1740 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1741 EXPECT_EQ(1, ftell(fp));
1742 ASSERT_NE(EOF, fputs("123", fp));
1743 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1744 EXPECT_EQ(4, ftell(fp));
1745 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1746 ASSERT_EQ(0, fclose(fp));
1747}
1748
1749TEST(STDIO_TEST, fmemopen_write_EOF) {
1750 // POSIX: "A write operation on the stream shall not advance the current
1751 // buffer size beyond the size given in the size argument."
1752 FILE* fp;
1753
1754 // Scalar writes...
1755 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1756 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1757 ASSERT_EQ('x', fputc('x', fp));
1758 ASSERT_EQ('x', fputc('x', fp));
1759 ASSERT_EQ('x', fputc('x', fp));
1760 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1761 ASSERT_EQ(0, fclose(fp));
1762
1763 // Vector writes...
1764 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1765 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1766 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1767 ASSERT_EQ(0, fclose(fp));
1768}
1769
1770TEST(STDIO_TEST, fmemopen_initial_position) {
1771 // POSIX: "The ... current position in the buffer ... shall be initially
1772 // set to either the beginning of the buffer (for r and w modes) ..."
1773 char buf[] = "hello\0world";
1774 FILE* fp;
1775 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1776 EXPECT_EQ(0L, ftell(fp));
1777 EXPECT_EQ(0, fclose(fp));
1778 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1779 EXPECT_EQ(0L, ftell(fp));
1780 EXPECT_EQ(0, fclose(fp));
1781 buf[0] = 'h'; // (Undo the effects of the above.)
1782
1783 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1784 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1785 EXPECT_EQ(5L, ftell(fp));
1786 EXPECT_EQ(0, fclose(fp));
1787
1788 // POSIX: "If no null byte is found in append mode, the initial position
1789 // shall be set to one byte after the end of the buffer."
1790 memset(buf, 'x', sizeof(buf));
1791 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1792 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1793 EXPECT_EQ(0, fclose(fp));
1794}
1795
1796TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1797 // POSIX: "If buf is a null pointer, the initial position shall always be
1798 // set to the beginning of the buffer."
1799 FILE* fp = fmemopen(nullptr, 128, "a+");
1800 ASSERT_TRUE(fp != nullptr);
1801 EXPECT_EQ(0L, ftell(fp));
1802 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1803 EXPECT_EQ(0, fclose(fp));
1804}
1805
1806TEST(STDIO_TEST, fmemopen_zero_length) {
1807 // POSIX says it's up to the implementation whether or not you can have a
1808 // zero-length buffer (but "A future version of this standard may require
1809 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1810 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1811 FILE* fp;
1812 char buf[16];
1813 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1814 ASSERT_EQ(EOF, fgetc(fp));
1815 ASSERT_TRUE(feof(fp));
1816 ASSERT_EQ(0, fclose(fp));
1817 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1818 ASSERT_EQ(EOF, fgetc(fp));
1819 ASSERT_TRUE(feof(fp));
1820 ASSERT_EQ(0, fclose(fp));
1821
1822 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1823 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1824 ASSERT_EQ(EOF, fputc('x', fp));
1825 ASSERT_EQ(0, fclose(fp));
1826 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1827 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1828 ASSERT_EQ(EOF, fputc('x', fp));
1829 ASSERT_EQ(0, fclose(fp));
1830}
1831
1832TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1833 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1834 // BSD fails, glibc doesn't. We side with the more lenient.
1835 FILE* fp;
1836 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1837 ASSERT_EQ(0, fclose(fp));
1838 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1839 ASSERT_EQ(0, fclose(fp));
1840}
1841
1842TEST(STDIO_TEST, fmemopen_fileno) {
1843 // There's no fd backing an fmemopen FILE*.
1844 FILE* fp = fmemopen(nullptr, 16, "r");
1845 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001846 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001847 ASSERT_EQ(-1, fileno(fp));
1848 ASSERT_EQ(EBADF, errno);
1849 ASSERT_EQ(0, fclose(fp));
1850}
1851
1852TEST(STDIO_TEST, fmemopen_append_after_seek) {
1853 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1854 // there had been an intervening seek.
1855
1856 FILE* fp;
1857 char buf[] = "hello\0world";
1858 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1859 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1860 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1861 ASSERT_NE(EOF, fputc('!', fp));
1862 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1863 ASSERT_EQ(0, fclose(fp));
1864
1865 memcpy(buf, "hello\0world", sizeof(buf));
1866 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1867 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1868 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1869 ASSERT_NE(EOF, fputc('!', fp));
1870 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1871 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001872}
1873
Christopher Ferris13f26a72016-01-13 13:47:58 -08001874TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001875 char* p = nullptr;
1876 size_t size = 0;
1877 FILE* fp = open_memstream(&p, &size);
1878 ASSERT_NE(EOF, fputs("hello, world!", fp));
1879 fclose(fp);
1880
1881 ASSERT_STREQ("hello, world!", p);
1882 ASSERT_EQ(strlen("hello, world!"), size);
1883 free(p);
1884}
1885
Christopher Ferris13f26a72016-01-13 13:47:58 -08001886TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001887#if defined(__BIONIC__)
1888 char* p;
1889 size_t size;
1890
1891 // Invalid buffer.
1892 errno = 0;
1893 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1894 ASSERT_EQ(EINVAL, errno);
1895
1896 // Invalid size.
1897 errno = 0;
1898 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1899 ASSERT_EQ(EINVAL, errno);
1900#else
Elliott Hughes9677fab2016-01-25 15:50:59 -08001901 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001902#endif
1903}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001904
Christopher Ferris13f26a72016-01-13 13:47:58 -08001905TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001906 int fd = open("/proc/version", O_RDONLY);
1907 ASSERT_TRUE(fd != -1);
1908
1909 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001910 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001911
1912 FILE* fp = fdopen(fd, "re");
1913 ASSERT_TRUE(fp != NULL);
1914
1915 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001916 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001917
1918 fclose(fp);
1919 close(fd);
1920}
1921
Christopher Ferris13f26a72016-01-13 13:47:58 -08001922TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001923 FILE* fp = fopen("/proc/version", "r");
1924 ASSERT_TRUE(fp != NULL);
1925
1926 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001927 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001928
1929 fp = freopen("/proc/version", "re", fp);
1930
1931 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001932 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001933
1934 fclose(fp);
1935}
Elliott Hughes20841a12014-12-01 16:13:30 -08001936
Elliott Hughesf226ee52016-02-03 11:24:28 -08001937TEST(STDIO_TEST, fopen64_freopen64) {
1938 FILE* fp = fopen64("/proc/version", "r");
1939 ASSERT_TRUE(fp != nullptr);
1940 fp = freopen64("/proc/version", "re", fp);
1941 ASSERT_TRUE(fp != nullptr);
1942 fclose(fp);
1943}
1944
Elliott Hughes20841a12014-12-01 16:13:30 -08001945// https://code.google.com/p/android/issues/detail?id=81155
1946// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001947TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001948 FILE* fp = fopen("/dev/zero", "r");
1949 ASSERT_TRUE(fp != NULL);
1950
1951 // Make this stream unbuffered.
1952 setvbuf(fp, 0, _IONBF, 0);
1953
1954 char buf[65*1024];
1955 memset(buf, 0xff, sizeof(buf));
1956
1957 time_t t0 = time(NULL);
1958 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001959 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001960 }
1961 time_t t1 = time(NULL);
1962
1963 fclose(fp);
1964
1965 // 1024 64KiB reads should have been very quick.
1966 ASSERT_LE(t1 - t0, 1);
1967
1968 for (size_t i = 0; i < 64*1024; ++i) {
1969 ASSERT_EQ('\0', buf[i]);
1970 }
1971 for (size_t i = 64*1024; i < 65*1024; ++i) {
1972 ASSERT_EQ('\xff', buf[i]);
1973 }
1974}
Elliott Hughes75b99382015-01-20 11:23:50 -08001975
Christopher Ferris13f26a72016-01-13 13:47:58 -08001976TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001977 std::string digits("0123456789");
1978 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001979
1980 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1981 char buf1[4 * 4];
1982 memset(buf1, 0, sizeof(buf1));
1983 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001984 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001985 ASSERT_TRUE(feof(fp));
1986
1987 rewind(fp);
1988
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001989 // Try to read way too much so stdio tries to read more direct from the stream.
1990 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001991 memset(buf2, 0, sizeof(buf2));
1992 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001993 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001994 ASSERT_TRUE(feof(fp));
1995
1996 fclose(fp);
1997}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001998
1999static void test_fread_from_write_only_stream(size_t n) {
2000 FILE* fp = fopen("/dev/null", "w");
2001 std::vector<char> buf(n, 0);
2002 errno = 0;
2003 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2004 ASSERT_EQ(EBADF, errno);
2005 ASSERT_TRUE(ferror(fp));
2006 ASSERT_FALSE(feof(fp));
2007 fclose(fp);
2008}
2009
Christopher Ferris13f26a72016-01-13 13:47:58 -08002010TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002011 test_fread_from_write_only_stream(1);
2012}
2013
Christopher Ferris13f26a72016-01-13 13:47:58 -08002014TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002015 test_fread_from_write_only_stream(64*1024);
2016}
2017
2018static void test_fwrite_after_fread(size_t n) {
2019 TemporaryFile tf;
2020
2021 FILE* fp = fdopen(tf.fd, "w+");
2022 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2023 fflush(fp);
2024
2025 // We've flushed but not rewound, so there's nothing to read.
2026 std::vector<char> buf(n, 0);
2027 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2028 ASSERT_TRUE(feof(fp));
2029
2030 // But hitting EOF doesn't prevent us from writing...
2031 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002032 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002033
2034 // And if we rewind, everything's there.
2035 rewind(fp);
2036 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2037 ASSERT_EQ('1', buf[0]);
2038 ASSERT_EQ('2', buf[1]);
2039
2040 fclose(fp);
2041}
2042
Christopher Ferris13f26a72016-01-13 13:47:58 -08002043TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002044 test_fwrite_after_fread(16);
2045}
2046
Christopher Ferris13f26a72016-01-13 13:47:58 -08002047TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002048 test_fwrite_after_fread(64*1024);
2049}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002050
2051// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002052TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002053 TemporaryFile tf;
2054
2055 FILE* fp = fopen(tf.filename, "w+");
2056 ASSERT_TRUE(fp != nullptr);
2057
2058 char file_data[12288];
2059 for (size_t i = 0; i < 12288; i++) {
2060 file_data[i] = i;
2061 }
2062 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2063 fclose(fp);
2064
2065 fp = fopen(tf.filename, "r");
2066 ASSERT_TRUE(fp != nullptr);
2067
2068 char buffer[8192];
2069 size_t cur_location = 0;
2070 // Small read to populate internal buffer.
2071 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2072 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2073
2074 cur_location = static_cast<size_t>(ftell(fp));
2075 // Large read to force reading into the user supplied buffer and bypassing
2076 // the internal buffer.
2077 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2078 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2079
2080 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002081 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002082 cur_location = static_cast<size_t>(ftell(fp));
2083 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2084 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2085
2086 fclose(fp);
2087}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002088
2089// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002090TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002091 TemporaryFile tf;
2092 char buf[6] = {0};
2093
2094 FILE* fw = fopen(tf.filename, "w");
2095 ASSERT_TRUE(fw != nullptr);
2096
2097 FILE* fr = fopen(tf.filename, "r");
2098 ASSERT_TRUE(fr != nullptr);
2099
2100 fwrite("a", 1, 1, fw);
2101 fflush(fw);
2102 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2103 ASSERT_STREQ("a", buf);
2104
2105 // 'fr' is now at EOF.
2106 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2107 ASSERT_TRUE(feof(fr));
2108
2109 // Write some more...
2110 fwrite("z", 1, 1, fw);
2111 fflush(fw);
2112
2113 // ...and check that we can read it back.
2114 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2115 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2116 ASSERT_STREQ("z", buf);
2117
2118 // But now we're done.
2119 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2120
2121 fclose(fr);
2122 fclose(fw);
2123}
Elliott Hughes923f1652016-01-19 15:46:05 -08002124
2125TEST(STDIO_TEST, fclose_invalidates_fd) {
2126 // The typical error we're trying to help people catch involves accessing
2127 // memory after it's been freed. But we know that stdin/stdout/stderr are
2128 // special and don't get deallocated, so this test uses stdin.
2129 ASSERT_EQ(0, fclose(stdin));
2130
2131 // Even though using a FILE* after close is undefined behavior, I've closed
2132 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2133 // especially because they might actually correspond to a real stream.
2134 errno = 0;
2135 ASSERT_EQ(-1, fileno(stdin));
2136 ASSERT_EQ(EBADF, errno);
2137}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002138
2139TEST(STDIO_TEST, fseek_ftell_unseekable) {
2140#if defined(__BIONIC__) // glibc has fopencookie instead.
2141 auto read_fn = [](void*, char*, int) { return -1; };
2142 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2143 ASSERT_TRUE(fp != nullptr);
2144
2145 // Check that ftell balks on an unseekable FILE*.
2146 errno = 0;
2147 ASSERT_EQ(-1, ftell(fp));
2148 ASSERT_EQ(ESPIPE, errno);
2149
2150 // SEEK_CUR is rewritten as SEEK_SET internally...
2151 errno = 0;
2152 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2153 ASSERT_EQ(ESPIPE, errno);
2154
2155 // ...so it's worth testing the direct seek path too.
2156 errno = 0;
2157 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2158 ASSERT_EQ(ESPIPE, errno);
2159
2160 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002161#else
2162 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
2163#endif
2164}
2165
2166TEST(STDIO_TEST, funopen_EINVAL) {
2167#if defined(__BIONIC__)
2168 errno = 0;
2169 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2170 ASSERT_EQ(EINVAL, errno);
2171#else
2172 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
2173#endif
2174}
2175
2176TEST(STDIO_TEST, funopen_seek) {
2177#if defined(__BIONIC__)
2178 auto read_fn = [](void*, char*, int) { return -1; };
2179
2180 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2181 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2182
2183 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2184 ASSERT_TRUE(fp != nullptr);
2185 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002186#if defined(__LP64__)
2187 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2188 EXPECT_EQ(0xfedcba12LL, pos);
2189#else
2190 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2191 EXPECT_EQ(EOVERFLOW, errno);
2192#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002193
2194 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2195 ASSERT_TRUE(fp64 != nullptr);
2196 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002197 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2198 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002199#else
2200 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002201#endif
2202}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002203
2204TEST(STDIO_TEST, lots_of_concurrent_files) {
2205 std::vector<TemporaryFile*> tfs;
2206 std::vector<FILE*> fps;
2207
2208 for (size_t i = 0; i < 256; ++i) {
2209 TemporaryFile* tf = new TemporaryFile;
2210 tfs.push_back(tf);
2211 FILE* fp = fopen(tf->filename, "w+");
2212 fps.push_back(fp);
2213 fprintf(fp, "hello %zu!\n", i);
2214 fflush(fp);
2215 }
2216
2217 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002218 char expected[BUFSIZ];
2219 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002220
Elliott Hughes70715da2016-08-01 16:35:17 -07002221 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002222 fclose(fps[i]);
2223 delete tfs[i];
2224 }
2225}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002226
2227static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2228 EXPECT_EQ(offset, ftell(fp));
2229 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002230 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002231 fpos_t pos;
2232 fpos64_t pos64;
2233 EXPECT_EQ(0, fgetpos(fp, &pos));
2234 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2235#if defined(__BIONIC__)
2236 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2237 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2238#else
2239 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
2240#endif
2241}
2242
2243TEST(STDIO_TEST, seek_tell_family_smoke) {
2244 TemporaryFile tf;
2245 FILE* fp = fdopen(tf.fd, "w+");
2246
2247 // Initially we should be at 0.
2248 AssertFileOffsetAt(fp, 0);
2249
2250 // Seek to offset 8192.
2251 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2252 AssertFileOffsetAt(fp, 8192);
2253 fpos_t eight_k_pos;
2254 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2255
2256 // Seek forward another 8192...
2257 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2258 AssertFileOffsetAt(fp, 8192 + 8192);
2259 fpos64_t sixteen_k_pos64;
2260 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2261
2262 // Seek back 8192...
2263 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2264 AssertFileOffsetAt(fp, 8192);
2265
2266 // Since we haven't written anything, the end is also at 0.
2267 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2268 AssertFileOffsetAt(fp, 0);
2269
2270 // Check that our fpos64_t from 16KiB works...
2271 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2272 AssertFileOffsetAt(fp, 8192 + 8192);
2273 // ...as does our fpos_t from 8192.
2274 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2275 AssertFileOffsetAt(fp, 8192);
2276
2277 // Do fseeko and fseeko64 work too?
2278 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2279 AssertFileOffsetAt(fp, 1234);
2280 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2281 AssertFileOffsetAt(fp, 5678);
2282
2283 fclose(fp);
2284}
2285
2286TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2287 TemporaryFile tf;
2288 FILE* fp = fdopen(tf.fd, "w+");
2289
2290 // Bad whence.
2291 errno = 0;
2292 ASSERT_EQ(-1, fseek(fp, 0, 123));
2293 ASSERT_EQ(EINVAL, errno);
2294 errno = 0;
2295 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2296 ASSERT_EQ(EINVAL, errno);
2297 errno = 0;
2298 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2299 ASSERT_EQ(EINVAL, errno);
2300
2301 // Bad offset.
2302 errno = 0;
2303 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2304 ASSERT_EQ(EINVAL, errno);
2305 errno = 0;
2306 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2307 ASSERT_EQ(EINVAL, errno);
2308 errno = 0;
2309 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2310 ASSERT_EQ(EINVAL, errno);
2311
2312 fclose(fp);
2313}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002314
2315TEST(STDIO_TEST, ctermid) {
2316 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2317
2318 char buf[L_ctermid] = {};
2319 ASSERT_EQ(buf, ctermid(buf));
2320 ASSERT_STREQ("/dev/tty", buf);
2321}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002322
2323TEST(STDIO_TEST, remove) {
2324 struct stat sb;
2325
2326 TemporaryFile tf;
2327 ASSERT_EQ(0, remove(tf.filename));
2328 ASSERT_EQ(-1, lstat(tf.filename, &sb));
2329 ASSERT_EQ(ENOENT, errno);
2330
2331 TemporaryDir td;
2332 ASSERT_EQ(0, remove(td.dirname));
2333 ASSERT_EQ(-1, lstat(td.dirname, &sb));
2334 ASSERT_EQ(ENOENT, errno);
2335
2336 errno = 0;
2337 ASSERT_EQ(-1, remove(tf.filename));
2338 ASSERT_EQ(ENOENT, errno);
2339
2340 errno = 0;
2341 ASSERT_EQ(-1, remove(td.dirname));
2342 ASSERT_EQ(ENOENT, errno);
2343}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002344
2345TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2346 char buf[16];
2347 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2348 testing::KilledBySignal(SIGABRT),
2349#if defined(NOFORTIFY)
2350 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2351#else
2352 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2353#endif
2354 );
2355}
2356
2357TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2358 std::string buf = "world";
2359 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2360 testing::KilledBySignal(SIGABRT),
2361 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2362}
2363
2364TEST(STDIO_TEST, sprintf_30445072) {
2365 std::string buf = "world";
2366 sprintf(&buf[0], "hello");
2367 ASSERT_EQ(buf, "hello");
2368}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002369
2370TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2371 TemporaryFile tf;
2372 SetFileTo(tf.filename, "0123456789");
2373 FILE* fp = fopen(tf.filename, "a");
2374 EXPECT_EQ(10, ftell(fp));
2375 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2376 EXPECT_EQ(2, ftell(fp));
2377 ASSERT_NE(EOF, fputs("xxx", fp));
2378 ASSERT_EQ(0, fflush(fp));
2379 EXPECT_EQ(13, ftell(fp));
2380 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2381 EXPECT_EQ(13, ftell(fp));
2382 ASSERT_EQ(0, fclose(fp));
2383 AssertFileIs(tf.filename, "0123456789xxx");
2384}
2385
2386TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2387 TemporaryFile tf;
2388 SetFileTo(tf.filename, "0123456789");
2389 int fd = open(tf.filename, O_RDWR);
2390 ASSERT_NE(-1, fd);
2391 // POSIX: "The file position indicator associated with the new stream is set to the position
2392 // indicated by the file offset associated with the file descriptor."
2393 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2394 FILE* fp = fdopen(fd, "a");
2395 EXPECT_EQ(4, ftell(fp));
2396 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2397 EXPECT_EQ(2, ftell(fp));
2398 ASSERT_NE(EOF, fputs("xxx", fp));
2399 ASSERT_EQ(0, fflush(fp));
2400 EXPECT_EQ(13, ftell(fp));
2401 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2402 EXPECT_EQ(13, ftell(fp));
2403 ASSERT_EQ(0, fclose(fp));
2404 AssertFileIs(tf.filename, "0123456789xxx");
2405}
2406
2407TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2408 TemporaryFile tf;
2409 SetFileTo(tf.filename, "0123456789");
2410 FILE* other_fp = fopen("/proc/version", "r");
2411 FILE* fp = freopen(tf.filename, "a", other_fp);
2412 EXPECT_EQ(10, ftell(fp));
2413 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2414 EXPECT_EQ(2, ftell(fp));
2415 ASSERT_NE(EOF, fputs("xxx", fp));
2416 ASSERT_EQ(0, fflush(fp));
2417 EXPECT_EQ(13, ftell(fp));
2418 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2419 EXPECT_EQ(13, ftell(fp));
2420 ASSERT_EQ(0, fclose(fp));
2421 AssertFileIs(tf.filename, "0123456789xxx");
2422}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002423
2424TEST(STDIO_TEST, constants) {
2425 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2426 ASSERT_EQ(L_tmpnam, PATH_MAX);
2427}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002428
2429TEST(STDIO_TEST, perror) {
2430 ExecTestHelper eth;
2431 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2432 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2433 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2434}
2435
2436TEST(STDIO_TEST, puts) {
2437 ExecTestHelper eth;
2438 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2439}
2440
2441TEST(STDIO_TEST, unlocked) {
2442 TemporaryFile tf;
2443
2444 FILE* fp = fopen(tf.filename, "w+");
2445 ASSERT_TRUE(fp != nullptr);
2446
2447 clearerr_unlocked(fp);
2448 ASSERT_FALSE(feof_unlocked(fp));
2449 ASSERT_FALSE(ferror_unlocked(fp));
2450
2451 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2452
2453 ASSERT_NE(EOF, putc_unlocked('a', fp));
2454 ASSERT_NE(EOF, putc('b', fp));
2455 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2456 ASSERT_NE(EOF, fputc('d', fp));
2457
2458 rewind(fp);
2459 ASSERT_EQ('a', getc_unlocked(fp));
2460 ASSERT_EQ('b', getc(fp));
2461 ASSERT_EQ('c', fgetc_unlocked(fp));
2462 ASSERT_EQ('d', fgetc(fp));
2463
2464 rewind(fp);
2465 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2466 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2467 ASSERT_EQ(0, fflush_unlocked(fp));
2468
2469 rewind(fp);
2470 char buf[BUFSIZ] = {};
2471 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2472 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2473 ASSERT_STREQ("ABCD", buf);
2474
2475 rewind(fp);
2476 ASSERT_NE(EOF, fputs("hello ", fp));
2477 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2478 ASSERT_NE(EOF, fputc('\n', fp));
2479
2480 rewind(fp);
2481 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2482 ASSERT_STREQ("hello world\n", buf);
2483
2484 ASSERT_EQ(0, fclose(fp));
2485}
Ryan Prichardbf549862017-11-07 15:30:32 -08002486
2487TEST(STDIO_TEST, fseek_64bit) {
2488 TemporaryFile tf;
2489 FILE* fp = fopen64(tf.filename, "w+");
2490 ASSERT_TRUE(fp != nullptr);
2491 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2492 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2493 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2494 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2495 ASSERT_EQ(0, fclose(fp));
2496}
2497
2498// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2499// isn't representable in long/off_t.
2500TEST(STDIO_TEST, fseek_overflow_32bit) {
2501 TemporaryFile tf;
2502 FILE* fp = fopen64(tf.filename, "w+");
2503 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2504
2505 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2506#if defined(__BIONIC__) && !defined(__LP64__)
2507 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2508 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2509 ASSERT_EQ(EOVERFLOW, errno);
2510#endif
2511
2512 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2513 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2514 // and SEEK_END -- many C libraries check neither.)
2515 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2516 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2517
2518 fclose(fp);
2519}