blob: 33514d4ffb5db1139185eaa34bb4761998ad65ba [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>
25#include <sys/stat.h>
26#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070027#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010028#include <locale.h>
29
Elliott Hughes3a4c4542017-07-19 17:20:24 -070030#include <string>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080031#include <vector>
32
Elliott Hughesfb3873d2016-08-10 11:07:54 -070033#include "BionicDeathTest.h"
Calin Juravle03e4ebe2014-05-08 14:42:06 +010034#include "TemporaryFile.h"
Josh Gao2f06e102017-01-10 13:00:37 -080035#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070036
Christopher Ferris13f26a72016-01-13 13:47:58 -080037#if defined(NOFORTIFY)
38#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070039#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080040#else
41#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070042#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080043#endif
44
Elliott Hughes3a4c4542017-07-19 17:20:24 -070045using namespace std::string_literals;
46
Elliott Hughesfb3873d2016-08-10 11:07:54 -070047class stdio_DeathTest : public BionicDeathTest {};
48class stdio_nofortify_DeathTest : public BionicDeathTest {};
49
Elliott Hughes33a8cb12017-07-25 18:06:46 -070050static void SetFileTo(const char* path, const char* content) {
51 FILE* fp;
52 ASSERT_NE(nullptr, fp = fopen(path, "w"));
53 ASSERT_NE(EOF, fputs(content, fp));
54 ASSERT_EQ(0, fclose(fp));
55}
56
57static void AssertFileIs(const char* path, const char* expected) {
58 FILE* fp;
59 ASSERT_NE(nullptr, fp = fopen(path, "r"));
60 char* line = nullptr;
61 size_t length;
62 ASSERT_NE(EOF, getline(&line, &length, fp));
63 ASSERT_EQ(0, fclose(fp));
64 ASSERT_STREQ(expected, line);
65 free(line);
66}
67
Elliott Hughes70715da2016-08-01 16:35:17 -070068static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
69 rewind(fp);
70
71 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080072 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070073 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
74 ASSERT_STREQ(expected, line);
75
76 if (is_fmemopen) {
77 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
78 // extra empty line, but does on every C library I tested...
79 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
80 ASSERT_STREQ("", line);
81 }
82
83 // Make sure there isn't anything else in the file.
84 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
85}
86
Christopher Ferris13f26a72016-01-13 13:47:58 -080087TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080088 // Check that we have a _recursive_ mutex for flockfile.
89 flockfile(stderr);
90 feof(stderr); // We don't care about the result, but this needs to take the lock.
91 funlockfile(stderr);
92}
93
Christopher Ferris13f26a72016-01-13 13:47:58 -080094TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080095 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
96 FILE* fp = fopen("/dev/null", "w");
97 ASSERT_TRUE(fp != NULL);
98 flockfile(fp);
99 feof(fp);
100 funlockfile(fp);
101 fclose(fp);
102}
103
Christopher Ferris13f26a72016-01-13 13:47:58 -0800104TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700105 FILE* fp = tmpfile();
106 ASSERT_TRUE(fp != NULL);
107
108 int fd = fileno(fp);
109 ASSERT_NE(fd, -1);
110
111 struct stat sb;
112 int rc = fstat(fd, &sb);
113 ASSERT_NE(rc, -1);
114 ASSERT_EQ(sb.st_mode & 0777, 0600U);
115
116 rc = fprintf(fp, "hello\n");
117 ASSERT_EQ(rc, 6);
118
Elliott Hughes70715da2016-08-01 16:35:17 -0700119 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700120 fclose(fp);
121}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300122
Elliott Hughesf226ee52016-02-03 11:24:28 -0800123TEST(STDIO_TEST, tmpfile64) {
124 FILE* fp = tmpfile64();
125 ASSERT_TRUE(fp != nullptr);
126 fclose(fp);
127}
128
Christopher Ferris13f26a72016-01-13 13:47:58 -0800129TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100130 TemporaryFile tf;
131
132 int rc = dprintf(tf.fd, "hello\n");
133 ASSERT_EQ(rc, 6);
134
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800135 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700136 FILE* tfile = fdopen(tf.fd, "r");
137 ASSERT_TRUE(tfile != NULL);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100138
Elliott Hughes70715da2016-08-01 16:35:17 -0700139 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700140 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100141}
142
Christopher Ferris13f26a72016-01-13 13:47:58 -0800143TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300144 FILE* fp = tmpfile();
145 ASSERT_TRUE(fp != NULL);
146
147 const char* line_written = "This is a test";
148 int rc = fprintf(fp, "%s", line_written);
149 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
150
151 rewind(fp);
152
153 char* word_read = NULL;
154 size_t allocated_length = 0;
155
156 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
157 for (size_t i = 0; i < 5; ++i) {
158 ASSERT_FALSE(feof(fp));
159 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
160 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800161 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300162 }
163 // The last read should have set the end-of-file indicator for the stream.
164 ASSERT_TRUE(feof(fp));
165 clearerr(fp);
166
167 // getdelim returns -1 but doesn't set errno if we're already at EOF.
168 // It should set the end-of-file indicator for the stream, though.
169 errno = 0;
170 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800171 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300172 ASSERT_TRUE(feof(fp));
173
174 free(word_read);
175 fclose(fp);
176}
177
Christopher Ferris13f26a72016-01-13 13:47:58 -0800178TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300179 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800180 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300181
182 char* buffer = NULL;
183 size_t buffer_length = 0;
184
185 // The first argument can't be NULL.
186 errno = 0;
187 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800188 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300189
190 // The second argument can't be NULL.
191 errno = 0;
192 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800193 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300194
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700195 // The underlying fd can't be closed.
196 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300197 errno = 0;
198 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800199 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700200 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300201}
202
Christopher Ferris13f26a72016-01-13 13:47:58 -0800203TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700204 FILE* fp = fopen("/proc", "r");
205 ASSERT_TRUE(fp != NULL);
206 char* word_read;
207 size_t allocated_length;
208 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
209 fclose(fp);
210}
211
Christopher Ferris13f26a72016-01-13 13:47:58 -0800212TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300213 FILE* fp = tmpfile();
214 ASSERT_TRUE(fp != NULL);
215
216 const char* line_written = "This is a test for getline\n";
217 const size_t line_count = 5;
218
219 for (size_t i = 0; i < line_count; ++i) {
220 int rc = fprintf(fp, "%s", line_written);
221 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
222 }
223
224 rewind(fp);
225
226 char* line_read = NULL;
227 size_t allocated_length = 0;
228
229 size_t read_line_count = 0;
230 ssize_t read_char_count;
231 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
232 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
233 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800234 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300235 ++read_line_count;
236 }
237 ASSERT_EQ(read_line_count, line_count);
238
239 // The last read should have set the end-of-file indicator for the stream.
240 ASSERT_TRUE(feof(fp));
241 clearerr(fp);
242
243 // getline returns -1 but doesn't set errno if we're already at EOF.
244 // It should set the end-of-file indicator for the stream, though.
245 errno = 0;
246 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800247 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300248 ASSERT_TRUE(feof(fp));
249
250 free(line_read);
251 fclose(fp);
252}
253
Christopher Ferris13f26a72016-01-13 13:47:58 -0800254TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300255 FILE* fp = tmpfile();
Elliott Hughes6ad8f762013-12-19 14:56:17 -0800256 ASSERT_TRUE(fp != NULL);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300257
258 char* buffer = NULL;
259 size_t buffer_length = 0;
260
261 // The first argument can't be NULL.
262 errno = 0;
263 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800264 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300265
266 // The second argument can't be NULL.
267 errno = 0;
268 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800269 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300270
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700271 // The underlying fd can't be closed.
272 ASSERT_EQ(0, close(fileno(fp)));
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300273 errno = 0;
274 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800275 ASSERT_EQ(EBADF, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700276 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300277}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000278
Christopher Ferris13f26a72016-01-13 13:47:58 -0800279TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800280 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800281 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800282 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
283 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000284 // error: format '%zd' expects argument of type 'signed size_t',
285 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
286 ssize_t v = 1;
287 char buf[32];
288 snprintf(buf, sizeof(buf), "%zd", v);
289}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800290
Elliott Hughes05493712014-04-17 17:30:03 -0700291// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800292TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700293 char buf[BUFSIZ];
294 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
295 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
296}
297
Christopher Ferris13f26a72016-01-13 13:47:58 -0800298TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700299 char buf[BUFSIZ];
300 wint_t wc = L'a';
301 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
302 EXPECT_STREQ("<a>", buf);
303}
304
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700305TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
306 char buf[BUFSIZ];
307 wchar_t wc = L'a';
308 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
309 EXPECT_STREQ("<a>", buf);
310}
311
Christopher Ferris13f26a72016-01-13 13:47:58 -0800312TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700313 char buf[BUFSIZ];
314 wchar_t* ws = NULL;
315 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
316 EXPECT_STREQ("<(null)>", buf);
317
318 wchar_t chars[] = { L'h', L'i', 0 };
319 ws = chars;
320 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
321 EXPECT_STREQ("<hi>", buf);
322}
323
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700324TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
325 char buf[BUFSIZ];
326 wchar_t* ws = NULL;
327 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
328 EXPECT_STREQ("<(null)>", buf);
329
330 wchar_t chars[] = { L'h', L'i', 0 };
331 ws = chars;
332 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
333 EXPECT_STREQ("<hi>", buf);
334}
335
Christopher Ferris13f26a72016-01-13 13:47:58 -0800336TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700337#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800338 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700339 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700340 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800341 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700342#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800343 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700344#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700345}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700346
Christopher Ferris13f26a72016-01-13 13:47:58 -0800347TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700348 char buf[BUFSIZ];
349
350 snprintf(buf, sizeof(buf), "a");
351 EXPECT_STREQ("a", buf);
352
353 snprintf(buf, sizeof(buf), "%%");
354 EXPECT_STREQ("%", buf);
355
356 snprintf(buf, sizeof(buf), "01234");
357 EXPECT_STREQ("01234", buf);
358
359 snprintf(buf, sizeof(buf), "a%sb", "01234");
360 EXPECT_STREQ("a01234b", buf);
361
362 char* s = NULL;
363 snprintf(buf, sizeof(buf), "a%sb", s);
364 EXPECT_STREQ("a(null)b", buf);
365
366 snprintf(buf, sizeof(buf), "aa%scc", "bb");
367 EXPECT_STREQ("aabbcc", buf);
368
369 snprintf(buf, sizeof(buf), "a%cc", 'b');
370 EXPECT_STREQ("abc", buf);
371
372 snprintf(buf, sizeof(buf), "a%db", 1234);
373 EXPECT_STREQ("a1234b", buf);
374
375 snprintf(buf, sizeof(buf), "a%db", -8123);
376 EXPECT_STREQ("a-8123b", buf);
377
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700378 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700379 EXPECT_STREQ("a16b", buf);
380
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700381 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700382 EXPECT_STREQ("a16b", buf);
383
384 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
385 EXPECT_STREQ("a68719476736b", buf);
386
387 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
388 EXPECT_STREQ("a70000b", buf);
389
390 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
391 EXPECT_STREQ("a0xb0001234b", buf);
392
393 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
394 EXPECT_STREQ("a12abz", buf);
395
396 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
397 EXPECT_STREQ("a12ABz", buf);
398
399 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
400 EXPECT_STREQ("a00123456z", buf);
401
402 snprintf(buf, sizeof(buf), "a%5dz", 1234);
403 EXPECT_STREQ("a 1234z", buf);
404
405 snprintf(buf, sizeof(buf), "a%05dz", 1234);
406 EXPECT_STREQ("a01234z", buf);
407
408 snprintf(buf, sizeof(buf), "a%8dz", 1234);
409 EXPECT_STREQ("a 1234z", buf);
410
411 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
412 EXPECT_STREQ("a1234 z", buf);
413
414 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
415 EXPECT_STREQ("Aabcdef Z", buf);
416
417 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
418 EXPECT_STREQ("Ahello:1234Z", buf);
419
420 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
421 EXPECT_STREQ("a005:5:05z", buf);
422
423 void* p = NULL;
424 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700425#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700426 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800427#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700428 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800429#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700430
431 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
432 EXPECT_STREQ("a68719476736,6,7,8z", buf);
433
434 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
435 EXPECT_STREQ("a_1.230000_b", buf);
436
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700437 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700438 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400439
440 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
441 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700442}
443
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800444template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700445static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
446 int sscanf_fn(const T*, const T*, ...),
447 const T* fmt_string, const T* fmt, const T* fmt_plus,
448 const T* minus_inf, const T* inf_, const T* plus_inf,
449 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800450 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700451 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700452
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700453 // NaN.
454
455 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800456 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700457 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
458 EXPECT_TRUE(isnan(f));
459
460 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800461 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700462 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
463 EXPECT_TRUE(isnan(f));
464
465 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800466 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700467 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
468 EXPECT_TRUE(isnan(f));
469
470 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800471 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700472 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
473 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800474
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700475 // Inf.
476
477 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800478 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700479 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
480 EXPECT_EQ(HUGE_VALF, f);
481
482 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800483 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700484 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
485 EXPECT_EQ(-HUGE_VALF, f);
486
487 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800488 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700489 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
490 EXPECT_EQ(HUGE_VALF, f);
491
492 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800493 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700494 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
495 EXPECT_EQ(-HUGE_VALF, f);
496
497 // Check case-insensitivity.
498 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
499 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
500 EXPECT_EQ(HUGE_VALF, f);
501 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
502 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
503 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700504}
505
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700506TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
507 CheckInfNan(snprintf, sscanf, "%s",
508 "[%a]", "[%+a]",
509 "[-inf]", "[inf]", "[+inf]",
510 "[-nan]", "[nan]", "[+nan]");
511 CheckInfNan(snprintf, sscanf, "%s",
512 "[%A]", "[%+A]",
513 "[-INF]", "[INF]", "[+INF]",
514 "[-NAN]", "[NAN]", "[+NAN]");
515 CheckInfNan(snprintf, sscanf, "%s",
516 "[%e]", "[%+e]",
517 "[-inf]", "[inf]", "[+inf]",
518 "[-nan]", "[nan]", "[+nan]");
519 CheckInfNan(snprintf, sscanf, "%s",
520 "[%E]", "[%+E]",
521 "[-INF]", "[INF]", "[+INF]",
522 "[-NAN]", "[NAN]", "[+NAN]");
523 CheckInfNan(snprintf, sscanf, "%s",
524 "[%f]", "[%+f]",
525 "[-inf]", "[inf]", "[+inf]",
526 "[-nan]", "[nan]", "[+nan]");
527 CheckInfNan(snprintf, sscanf, "%s",
528 "[%F]", "[%+F]",
529 "[-INF]", "[INF]", "[+INF]",
530 "[-NAN]", "[NAN]", "[+NAN]");
531 CheckInfNan(snprintf, sscanf, "%s",
532 "[%g]", "[%+g]",
533 "[-inf]", "[inf]", "[+inf]",
534 "[-nan]", "[nan]", "[+nan]");
535 CheckInfNan(snprintf, sscanf, "%s",
536 "[%G]", "[%+G]",
537 "[-INF]", "[INF]", "[+INF]",
538 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800539}
Elliott Hughes7823f322014-04-14 12:11:28 -0700540
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700541TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
542 CheckInfNan(swprintf, swscanf, L"%s",
543 L"[%a]", L"[%+a]",
544 L"[-inf]", L"[inf]", L"[+inf]",
545 L"[-nan]", L"[nan]", L"[+nan]");
546 CheckInfNan(swprintf, swscanf, L"%s",
547 L"[%A]", L"[%+A]",
548 L"[-INF]", L"[INF]", L"[+INF]",
549 L"[-NAN]", L"[NAN]", L"[+NAN]");
550 CheckInfNan(swprintf, swscanf, L"%s",
551 L"[%e]", L"[%+e]",
552 L"[-inf]", L"[inf]", L"[+inf]",
553 L"[-nan]", L"[nan]", L"[+nan]");
554 CheckInfNan(swprintf, swscanf, L"%s",
555 L"[%E]", L"[%+E]",
556 L"[-INF]", L"[INF]", L"[+INF]",
557 L"[-NAN]", L"[NAN]", L"[+NAN]");
558 CheckInfNan(swprintf, swscanf, L"%s",
559 L"[%f]", L"[%+f]",
560 L"[-inf]", L"[inf]", L"[+inf]",
561 L"[-nan]", L"[nan]", L"[+nan]");
562 CheckInfNan(swprintf, swscanf, L"%s",
563 L"[%F]", L"[%+F]",
564 L"[-INF]", L"[INF]", L"[+INF]",
565 L"[-NAN]", L"[NAN]", L"[+NAN]");
566 CheckInfNan(swprintf, swscanf, L"%s",
567 L"[%g]", L"[%+g]",
568 L"[-inf]", L"[inf]", L"[+inf]",
569 L"[-nan]", L"[nan]", L"[+nan]");
570 CheckInfNan(swprintf, swscanf, L"%s",
571 L"[%G]", L"[%+G]",
572 L"[-INF]", L"[INF]", L"[+INF]",
573 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700574}
575
Dan Albert9601f162017-08-09 14:59:06 -0700576TEST(STDIO_TEST, swprintf) {
577 constexpr size_t nchars = 32;
578 wchar_t buf[nchars];
579
580 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
581 ASSERT_EQ(std::wstring(L"ab"), buf);
582 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
583 ASSERT_EQ(std::wstring(L"abcde"), buf);
584
585 // Unlike swprintf(), swprintf() returns -1 in case of truncation
586 // and doesn't necessarily zero-terminate the output!
587 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
588
589 const char kString[] = "Hello, World";
590 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
591 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
592 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
593 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
594}
595
596TEST(STDIO_TEST, swprintf_a) {
597 constexpr size_t nchars = 32;
598 wchar_t buf[nchars];
599
600 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
601 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
602}
603
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700604TEST(STDIO_TEST, swprintf_lc) {
605 constexpr size_t nchars = 32;
606 wchar_t buf[nchars];
607
608 wint_t wc = L'a';
609 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
610 EXPECT_EQ(std::wstring(L"<a>"), buf);
611}
612
613TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
614 constexpr size_t nchars = 32;
615 wchar_t buf[nchars];
616
617 wint_t wc = L'a';
618 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
619 EXPECT_EQ(std::wstring(L"<a>"), buf);
620}
621
Elliott Hughes618303c2017-11-02 16:58:44 -0700622TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
623 constexpr size_t nchars = 32;
624 wchar_t buf[nchars];
625
626 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
627 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
628}
629
630TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
631 constexpr size_t nchars = 32;
632 wchar_t buf[nchars];
633
634 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
635 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
636}
637
638TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
639 constexpr size_t nchars = 32;
640 wchar_t buf[nchars];
641
642 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
643 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
644}
645
646TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
647 constexpr size_t nchars = 32;
648 wchar_t buf[nchars];
649
650 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
651 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
652}
653
Dan Albert9601f162017-08-09 14:59:06 -0700654TEST(STDIO_TEST, swprintf_ls) {
655 constexpr size_t nchars = 32;
656 wchar_t buf[nchars];
657
658 static const wchar_t kWideString[] = L"Hello\uff41 World";
659 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
660 ASSERT_EQ(std::wstring(kWideString), buf);
661 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
662 ASSERT_EQ(std::wstring(kWideString), buf);
663}
664
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700665TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
666 constexpr size_t nchars = 32;
667 wchar_t buf[nchars];
668
669 static const wchar_t kWideString[] = L"Hello\uff41 World";
670 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
671 ASSERT_EQ(std::wstring(kWideString), buf);
672 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
673 ASSERT_EQ(std::wstring(kWideString), buf);
674}
675
Christopher Ferris13f26a72016-01-13 13:47:58 -0800676TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700677 char buf[BUFSIZ];
678 snprintf(buf, sizeof(buf), "%d", INT_MAX);
679 EXPECT_STREQ("2147483647", buf);
680}
681
Christopher Ferris13f26a72016-01-13 13:47:58 -0800682TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700683 char buf[BUFSIZ];
684 snprintf(buf, sizeof(buf), "%d", INT_MIN);
685 EXPECT_STREQ("-2147483648", buf);
686}
687
Elliott Hughes618303c2017-11-02 16:58:44 -0700688TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
689 char buf[BUFSIZ];
690 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
691 EXPECT_STREQ("9223372036854775807", buf);
692}
693
694TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
695 char buf[BUFSIZ];
696 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
697 EXPECT_STREQ("-9223372036854775808", buf);
698}
699
700TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
701 char buf[BUFSIZ];
702 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
703 EXPECT_STREQ("18446744073709551615", buf);
704}
705
706TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
707 char buf[BUFSIZ];
708 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
709 EXPECT_STREQ("18446744073709551615", buf);
710}
711
Christopher Ferris13f26a72016-01-13 13:47:58 -0800712TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700713 char buf[BUFSIZ];
714 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700715#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700716 EXPECT_STREQ("9223372036854775807", buf);
717#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700718 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700719#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700720}
721
Christopher Ferris13f26a72016-01-13 13:47:58 -0800722TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700723 char buf[BUFSIZ];
724 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700725#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700726 EXPECT_STREQ("-9223372036854775808", buf);
727#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700728 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700729#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700730}
731
Christopher Ferris13f26a72016-01-13 13:47:58 -0800732TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700733 char buf[BUFSIZ];
734 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
735 EXPECT_STREQ("9223372036854775807", buf);
736}
737
Christopher Ferris13f26a72016-01-13 13:47:58 -0800738TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700739 char buf[BUFSIZ];
740 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
741 EXPECT_STREQ("-9223372036854775808", buf);
742}
743
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700744TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
745 char buf[BUFSIZ];
746 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
747 EXPECT_STREQ("37777777777", buf);
748}
749
750TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
751 char buf[BUFSIZ];
752 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
753 EXPECT_STREQ("4294967295", buf);
754}
755
756TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
757 char buf[BUFSIZ];
758 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
759 EXPECT_STREQ("ffffffff", buf);
760}
761
762TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
763 char buf[BUFSIZ];
764 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
765 EXPECT_STREQ("FFFFFFFF", buf);
766}
767
Christopher Ferris13f26a72016-01-13 13:47:58 -0800768TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700769 char buf[BUFSIZ];
770
771 snprintf(buf, sizeof(buf), "%e", 1.5);
772 EXPECT_STREQ("1.500000e+00", buf);
773
774 snprintf(buf, sizeof(buf), "%Le", 1.5l);
775 EXPECT_STREQ("1.500000e+00", buf);
776}
777
Christopher Ferris13f26a72016-01-13 13:47:58 -0800778TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700779 char buf[BUFSIZ];
780
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800781 snprintf(buf, sizeof(buf), "%e", -0.0);
782 EXPECT_STREQ("-0.000000e+00", buf);
783 snprintf(buf, sizeof(buf), "%E", -0.0);
784 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700785 snprintf(buf, sizeof(buf), "%f", -0.0);
786 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800787 snprintf(buf, sizeof(buf), "%F", -0.0);
788 EXPECT_STREQ("-0.000000", buf);
789 snprintf(buf, sizeof(buf), "%g", -0.0);
790 EXPECT_STREQ("-0", buf);
791 snprintf(buf, sizeof(buf), "%G", -0.0);
792 EXPECT_STREQ("-0", buf);
793 snprintf(buf, sizeof(buf), "%a", -0.0);
794 EXPECT_STREQ("-0x0p+0", buf);
795 snprintf(buf, sizeof(buf), "%A", -0.0);
796 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700797}
798
Christopher Ferris13f26a72016-01-13 13:47:58 -0800799TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700800 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700801 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700802
Elliott Hughes69f05d22014-06-05 20:10:09 -0700803 // http://b/15439554
804 char buf[BUFSIZ];
805
806 // 1-byte character.
807 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
808 EXPECT_STREQ("1x2", buf);
809 // 2-byte character.
810 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
811 EXPECT_STREQ("1¢2", buf);
812 // 3-byte character.
813 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
814 EXPECT_STREQ("1€2", buf);
815 // 4-byte character.
816 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
817 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700818
Wally Yaua40fdbd2014-08-26 09:47:23 -0700819 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700820 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700821}
822
Elliott Hughes43f7c872016-02-05 11:18:41 -0800823static void* snprintf_small_stack_fn(void*) {
824 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
825 char buf[PATH_MAX];
826 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
827 return nullptr;
828}
829
830TEST(STDIO_TEST, snprintf_small_stack) {
831 // Is it safe to call snprintf on a thread with a small stack?
832 // (The snprintf implementation puts some pretty large buffers on the stack.)
833 pthread_attr_t a;
834 ASSERT_EQ(0, pthread_attr_init(&a));
835 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
836
837 pthread_t t;
838 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
839 ASSERT_EQ(0, pthread_join(t, nullptr));
840}
841
Elliott Hughes8200e552016-02-05 21:57:37 -0800842TEST(STDIO_TEST, snprintf_asterisk_overflow) {
843 char buf[128];
844 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
845 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
846 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
847 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
848 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
849
850 // INT_MAX-1, INT_MAX, INT_MAX+1.
851 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
852 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
853 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
854 ASSERT_EQ(ENOMEM, errno);
855}
856
Elliott Hughes70715da2016-08-01 16:35:17 -0700857TEST(STDIO_TEST, fprintf) {
858 TemporaryFile tf;
859
860 FILE* tfile = fdopen(tf.fd, "r+");
861 ASSERT_TRUE(tfile != nullptr);
862
863 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
864 AssertFileIs(tfile, "123 abc");
865 fclose(tfile);
866}
867
Christopher Ferris13f26a72016-01-13 13:47:58 -0800868TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700869 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700870 FILE* fp;
871
872 // Unbuffered case where the fprintf(3) itself fails.
873 ASSERT_NE(nullptr, fp = tmpfile());
874 setbuf(fp, NULL);
875 ASSERT_EQ(4, fprintf(fp, "epic"));
876 ASSERT_EQ(0, close(fileno(fp)));
877 ASSERT_EQ(-1, fprintf(fp, "fail"));
878 ASSERT_EQ(-1, fclose(fp));
879
880 // Buffered case where we won't notice until the fclose(3).
881 // It's likely this is what was actually seen in http://b/7229520,
882 // and that expecting fprintf to fail is setting yourself up for
883 // disappointment. Remember to check fclose(3)'s return value, kids!
884 ASSERT_NE(nullptr, fp = tmpfile());
885 ASSERT_EQ(4, fprintf(fp, "epic"));
886 ASSERT_EQ(0, close(fileno(fp)));
887 ASSERT_EQ(4, fprintf(fp, "fail"));
888 ASSERT_EQ(-1, fclose(fp));
889}
890
Christopher Ferris13f26a72016-01-13 13:47:58 -0800891TEST(STDIO_TEST, popen) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800892 FILE* fp = popen("cat /proc/version", "r");
893 ASSERT_TRUE(fp != NULL);
894
895 char buf[16];
896 char* s = fgets(buf, sizeof(buf), fp);
897 buf[13] = '\0';
898 ASSERT_STREQ("Linux version", s);
899
900 ASSERT_EQ(0, pclose(fp));
901}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700902
Christopher Ferris13f26a72016-01-13 13:47:58 -0800903TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700904 FILE* fp = fopen("/proc/version", "r");
905 ASSERT_TRUE(fp != NULL);
906 ASSERT_EQ('L', getc(fp));
907 ASSERT_EQ('i', getc(fp));
908 ASSERT_EQ('n', getc(fp));
909 ASSERT_EQ('u', getc(fp));
910 ASSERT_EQ('x', getc(fp));
911 fclose(fp);
912}
913
Christopher Ferris13f26a72016-01-13 13:47:58 -0800914TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700915 FILE* fp = fopen("/proc/version", "r");
916 ASSERT_TRUE(fp != NULL);
917 ASSERT_EQ(EOF, putc('x', fp));
918 fclose(fp);
919}
Elliott Hughes603332f2014-03-12 17:10:41 -0700920
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700921TEST(STDIO_TEST, sscanf_swscanf) {
922 struct stuff {
923 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800924 int i1, i2;
925 char cs1[3];
926 char s2[3];
927 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700928 double d1;
929 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800930 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700931
932 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800933 EXPECT_STREQ("hello", s1);
934 EXPECT_EQ(123, i1);
935 EXPECT_EQ(456, i2);
936 EXPECT_EQ('a', cs1[0]);
937 EXPECT_EQ('b', cs1[1]);
938 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
939 EXPECT_STREQ("AB", s2); // Terminating NUL.
940 EXPECT_EQ('!', c1);
941 EXPECT_DOUBLE_EQ(1.23, d1);
942 EXPECT_FLOAT_EQ(9.0f, f1);
943 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700944 }
945 } s;
946
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800947 memset(&s, 'x', sizeof(s));
948 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
949 "%s %i%i%2c%[A-Z]%c %lf %f %s",
950 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 -0700951 s.Check();
952
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800953 memset(&s, 'x', sizeof(s));
954 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
955 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
956 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 -0700957 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -0700958}
Elliott Hughes53b24382014-05-02 18:29:25 -0700959
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800960template <typename T>
961static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
962 const T* input, const T* fmt,
963 int expected_count, const char* expected_string) {
964 char buf[256] = {};
965 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
966 ASSERT_STREQ(expected_string, buf) << fmt;
967}
968
969TEST(STDIO_TEST, sscanf_ccl) {
970 // `abc` is just those characters.
971 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
972 // `a-c` is the range 'a' .. 'c'.
973 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
974 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
975 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
976 // `a-c-e` is equivalent to `a-e`.
977 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
978 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
979 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
980 // An initial '^' negates the set.
981 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
982 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
983 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
984 // The first character may be ']' or '-' without being special.
985 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
986 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
987 // The last character may be '-' without being special.
988 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
989 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
990 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
991}
992
993TEST(STDIO_TEST, swscanf_ccl) {
994 // `abc` is just those characters.
995 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
996 // `a-c` is the range 'a' .. 'c'.
997 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
998 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
999 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1000 // `a-c-e` is equivalent to `a-e`.
1001 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1002 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1003 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1004 // An initial '^' negates the set.
1005 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1006 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1007 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1008 // The first character may be ']' or '-' without being special.
1009 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1010 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1011 // The last character may be '-' without being special.
1012 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1013 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1014 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1015}
1016
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001017template <typename T1, typename T2>
1018static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1019 const T1* input, const T1* fmt,
1020 int expected_count, const T2* expected_string) {
1021 T2* result = nullptr;
1022 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1023 if (expected_string == nullptr) {
1024 ASSERT_EQ(nullptr, result);
1025 } else {
1026 ASSERT_STREQ(expected_string, result) << fmt;
1027 }
1028 free(result);
1029}
1030
1031TEST(STDIO_TEST, sscanf_mc) {
1032 char* p1 = nullptr;
1033 char* p2 = nullptr;
1034 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1035 ASSERT_EQ('h', *p1);
1036 ASSERT_EQ('e', *p2);
1037 free(p1);
1038 free(p2);
1039
1040 p1 = nullptr;
1041 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1042 ASSERT_EQ('h', p1[0]);
1043 ASSERT_EQ('e', p1[1]);
1044 ASSERT_EQ('l', p1[2]);
1045 ASSERT_EQ('l', p1[3]);
1046 free(p1);
1047
1048 p1 = nullptr;
1049 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1050 ASSERT_EQ('h', p1[0]);
1051 ASSERT_EQ('e', p1[1]);
1052 ASSERT_EQ('l', p1[2]);
1053 ASSERT_EQ('l', p1[3]);
1054 ASSERT_EQ('o', p1[4]);
1055 free(p1);
1056}
1057
1058
1059TEST(STDIO_TEST, sscanf_mlc) {
1060 // This is so useless that clang doesn't even believe it exists...
1061#pragma clang diagnostic push
1062#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1063#pragma clang diagnostic ignored "-Wformat-extra-args"
1064
1065 wchar_t* p1 = nullptr;
1066 wchar_t* p2 = nullptr;
1067 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1068 ASSERT_EQ(L'h', *p1);
1069 ASSERT_EQ(L'e', *p2);
1070 free(p1);
1071 free(p2);
1072
1073 p1 = nullptr;
1074 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1075 ASSERT_EQ(L'h', p1[0]);
1076 ASSERT_EQ(L'e', p1[1]);
1077 ASSERT_EQ(L'l', p1[2]);
1078 ASSERT_EQ(L'l', p1[3]);
1079 free(p1);
1080
1081 p1 = nullptr;
1082 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1083 ASSERT_EQ(L'h', p1[0]);
1084 ASSERT_EQ(L'e', p1[1]);
1085 ASSERT_EQ(L'l', p1[2]);
1086 ASSERT_EQ(L'l', p1[3]);
1087 ASSERT_EQ(L'o', p1[4]);
1088 free(p1);
1089#pragma clang diagnostic pop
1090}
1091
1092
1093TEST(STDIO_TEST, sscanf_ms) {
1094 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1095 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1096 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1097}
1098
1099TEST(STDIO_TEST, sscanf_mls) {
1100 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1101 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1102 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1103}
1104
1105TEST(STDIO_TEST, sscanf_m_ccl) {
1106 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1107 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1108 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1109}
1110
1111TEST(STDIO_TEST, sscanf_ml_ccl) {
1112 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1113 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1114 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1115}
1116
1117TEST(STDIO_TEST, sscanf_ls) {
1118 wchar_t w[32] = {};
1119 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1120 ASSERT_EQ(L"hello", std::wstring(w));
1121}
1122
1123TEST(STDIO_TEST, sscanf_ls_suppress) {
1124 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1125}
1126
1127TEST(STDIO_TEST, sscanf_ls_n) {
1128 setlocale(LC_ALL, "C.UTF-8");
1129 wchar_t w[32] = {};
1130 int pos = 0;
1131 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1132 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1133 ASSERT_EQ(2, pos);
1134}
1135
1136TEST(STDIO_TEST, sscanf_ls_realloc) {
1137 // This is so useless that clang doesn't even believe it exists...
1138#pragma clang diagnostic push
1139#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1140#pragma clang diagnostic ignored "-Wformat-extra-args"
1141 wchar_t* p1 = nullptr;
1142 wchar_t* p2 = nullptr;
1143 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1144 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1145 ASSERT_EQ(L"world", std::wstring(p2));
1146#pragma clang diagnostic pop
1147}
1148
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001149// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1150TEST(STDIO_TEST, scanf_wscanf_EOF) {
1151 EXPECT_EQ(0, sscanf("b", "ab"));
1152 EXPECT_EQ(EOF, sscanf("", "a"));
1153 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1154 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1155}
1156
1157TEST(STDIO_TEST, scanf_invalid_UTF8) {
1158#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1159 char buf[BUFSIZ];
1160 wchar_t wbuf[BUFSIZ];
1161
1162 memset(buf, 0, sizeof(buf));
1163 memset(wbuf, 0, sizeof(wbuf));
1164 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1165#endif
1166}
1167
1168TEST(STDIO_TEST, scanf_no_match_no_termination) {
1169 char buf[4] = "x";
1170 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1171 EXPECT_EQ('x', buf[0]);
1172 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1173 EXPECT_EQ('x', buf[0]);
1174
1175 wchar_t wbuf[4] = L"x";
1176 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1177 EXPECT_EQ(L'x', wbuf[0]);
1178
1179 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1180 EXPECT_EQ('x', buf[0]);
1181
1182 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1183 EXPECT_EQ(L'x', wbuf[0]);
1184}
1185
1186TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1187#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1188 wchar_t buf[BUFSIZ];
1189
1190 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1191 memset(buf, 0, sizeof(buf));
1192 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1193 EXPECT_EQ(L"x"s, std::wstring(buf));
1194 memset(buf, 0, sizeof(buf));
1195 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1196 EXPECT_EQ(L"x"s, std::wstring(buf));
1197
1198 // Even if scanf has wide characters in a class, they won't match...
1199 // TODO: is that a bug?
1200 memset(buf, 0, sizeof(buf));
1201 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1202 EXPECT_EQ(L"x"s, std::wstring(buf));
1203 // ...unless you use wscanf.
1204 memset(buf, 0, sizeof(buf));
1205 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1206 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1207
1208 // Negation only covers ASCII for scanf...
1209 memset(buf, 0, sizeof(buf));
1210 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1211 EXPECT_EQ(L"x"s, std::wstring(buf));
1212 // ...but covers wide characters for wscanf.
1213 memset(buf, 0, sizeof(buf));
1214 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1215 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1216
1217 // We already determined that non-ASCII characters are ignored in scanf classes.
1218 memset(buf, 0, sizeof(buf));
1219 EXPECT_EQ(1, sscanf("x"
1220 "\xc4\x80" // Matches a byte from each wide char in the class.
1221 "\xc6\x82" // Neither byte is in the class.
1222 "yz",
1223 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1224 EXPECT_EQ(L"x", std::wstring(buf));
1225 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1226 memset(buf, 0, sizeof(buf));
1227 EXPECT_EQ(1, swscanf(L"x"
1228 L"\xc4\x80"
1229 L"\xc6\x82"
1230 L"yz",
1231 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1232 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1233 // not put back together as a wide character.
1234 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1235#endif
1236}
1237
Christopher Ferris13f26a72016-01-13 13:47:58 -08001238TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001239 // If we open a file read-only...
1240 FILE* fp = fopen("/proc/version", "r");
1241
1242 // ...all attempts to write to that file should return failure.
1243
1244 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1245 // glibc gets the wide-character functions wrong.
1246
1247 errno = 0;
1248 EXPECT_EQ(EOF, putc('x', fp));
1249 EXPECT_EQ(EBADF, errno);
1250
1251 errno = 0;
1252 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1253 EXPECT_EQ(EBADF, errno);
1254
1255 errno = 0;
1256 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001257#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001258 EXPECT_EQ(EBADF, errno);
1259#endif
1260
1261 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001262 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1263 EXPECT_EQ(EBADF, errno);
1264
1265 errno = 0;
1266 EXPECT_EQ(EOF, fputs("hello", fp));
1267 EXPECT_EQ(EBADF, errno);
1268
1269 errno = 0;
1270 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001271#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001272 EXPECT_EQ(EBADF, errno);
1273#endif
1274}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001275
1276// Tests that we can only have a consistent and correct fpos_t when using
1277// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001278TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001279 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1280 uselocale(LC_GLOBAL_LOCALE);
1281
1282 FILE* fp = tmpfile();
1283 ASSERT_TRUE(fp != NULL);
1284
1285 wchar_t mb_one_bytes = L'h';
1286 wchar_t mb_two_bytes = 0x00a2;
1287 wchar_t mb_three_bytes = 0x20ac;
1288 wchar_t mb_four_bytes = 0x24b62;
1289
1290 // Write to file.
1291 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1292 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1293 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1294 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1295
1296 rewind(fp);
1297
1298 // Record each character position.
1299 fpos_t pos1;
1300 fpos_t pos2;
1301 fpos_t pos3;
1302 fpos_t pos4;
1303 fpos_t pos5;
1304 EXPECT_EQ(0, fgetpos(fp, &pos1));
1305 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1306 EXPECT_EQ(0, fgetpos(fp, &pos2));
1307 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1308 EXPECT_EQ(0, fgetpos(fp, &pos3));
1309 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1310 EXPECT_EQ(0, fgetpos(fp, &pos4));
1311 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1312 EXPECT_EQ(0, fgetpos(fp, &pos5));
1313
Elliott Hughes063525c2014-05-13 11:19:57 -07001314#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001315 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1316 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1317 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1318 // structure.
1319 ASSERT_EQ(0, static_cast<off_t>(pos1));
1320 ASSERT_EQ(1, static_cast<off_t>(pos2));
1321 ASSERT_EQ(3, static_cast<off_t>(pos3));
1322 ASSERT_EQ(6, static_cast<off_t>(pos4));
1323 ASSERT_EQ(10, static_cast<off_t>(pos5));
1324#endif
1325
1326 // Exercise back and forth movements of the position.
1327 ASSERT_EQ(0, fsetpos(fp, &pos2));
1328 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1329 ASSERT_EQ(0, fsetpos(fp, &pos1));
1330 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1331 ASSERT_EQ(0, fsetpos(fp, &pos4));
1332 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1333 ASSERT_EQ(0, fsetpos(fp, &pos3));
1334 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1335 ASSERT_EQ(0, fsetpos(fp, &pos5));
1336 ASSERT_EQ(WEOF, fgetwc(fp));
1337
1338 fclose(fp);
1339}
1340
1341// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001342TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001343 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1344 uselocale(LC_GLOBAL_LOCALE);
1345
Calin Juravle9b95ea92014-05-14 17:07:10 +01001346 // In glibc-2.16 fseek doesn't work properly in wide mode
1347 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1348 // to close and re-open the file. We do it in order to make the test pass
1349 // with all glibcs.
1350
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001351 TemporaryFile tf;
1352 FILE* fp = fdopen(tf.fd, "w+");
1353 ASSERT_TRUE(fp != NULL);
1354
1355 wchar_t mb_two_bytes = 0x00a2;
1356 wchar_t mb_three_bytes = 0x20ac;
1357 wchar_t mb_four_bytes = 0x24b62;
1358
1359 // Write to file.
1360 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1361 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1362 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1363
1364 fflush(fp);
1365 fclose(fp);
1366
1367 fp = fopen(tf.filename, "r");
1368 ASSERT_TRUE(fp != NULL);
1369
1370 // Store a valid position.
1371 fpos_t mb_two_bytes_pos;
1372 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1373
1374 // Move inside mb_four_bytes with fseek.
1375 long offset_inside_mb = 6;
1376 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1377
1378 // Store the "inside multi byte" position.
1379 fpos_t pos_inside_mb;
1380 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001381#if defined(__BIONIC__)
1382 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1383#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001384
1385 // Reading from within a byte should produce an error.
1386 ASSERT_EQ(WEOF, fgetwc(fp));
1387 ASSERT_EQ(EILSEQ, errno);
1388
1389 // Reverting to a valid position should work.
1390 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1391 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1392
1393 // Moving withing a multi byte with fsetpos should work but reading should
1394 // produce an error.
1395 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1396 ASSERT_EQ(WEOF, fgetwc(fp));
1397 ASSERT_EQ(EILSEQ, errno);
1398
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001399 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001400}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001401
Christopher Ferris13f26a72016-01-13 13:47:58 -08001402TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001403 char buf[16];
1404 memset(buf, 0, sizeof(buf));
1405 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1406 ASSERT_EQ('<', fputc('<', fp));
1407 ASSERT_NE(EOF, fputs("abc>\n", fp));
1408 fflush(fp);
1409
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001410 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001411 ASSERT_STREQ("<abc>\n", buf);
1412
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001413 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001414 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001415 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001416}
1417
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001418TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001419 FILE* fp = fmemopen(nullptr, 128, "r+");
1420 ASSERT_NE(EOF, fputs("xyz\n", fp));
1421
Elliott Hughes70715da2016-08-01 16:35:17 -07001422 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001423 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001424}
1425
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001426TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1427 FILE* fp;
1428 char buf[8];
1429
1430 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1431 // shall be written at the current position or at the end of the buffer,
1432 // depending on the size of the contents."
1433 memset(buf, 'x', sizeof(buf));
1434 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1435 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1436 ASSERT_EQ(0, fflush(fp));
1437 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1438 // Now write and check that the NUL moves along with our writes...
1439 ASSERT_NE(EOF, fputs("hello", fp));
1440 ASSERT_EQ(0, fflush(fp));
1441 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1442 ASSERT_NE(EOF, fputs("wo", fp));
1443 ASSERT_EQ(0, fflush(fp));
1444 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1445 ASSERT_EQ(0, fclose(fp));
1446
1447 // "If a stream open for update is flushed or closed and the last write has
1448 // advanced the current buffer size, a null byte shall be written at the end
1449 // of the buffer if it fits."
1450 memset(buf, 'x', sizeof(buf));
1451 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1452 // Nothing written yet, so no advance...
1453 ASSERT_EQ(0, fflush(fp));
1454 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1455 ASSERT_NE(EOF, fputs("hello", fp));
1456 ASSERT_EQ(0, fclose(fp));
1457}
1458
1459TEST(STDIO_TEST, fmemopen_size) {
1460 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001461 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001462 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001463
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001464 // POSIX: "The stream shall also maintain the size of the current buffer
1465 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1466 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001467
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001468 // "For modes r and r+ the size shall be set to the value given by the size
1469 // argument."
1470 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1471 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1472 EXPECT_EQ(16, ftell(fp));
1473 EXPECT_EQ(16, ftello(fp));
1474 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1475 EXPECT_EQ(16, ftell(fp));
1476 EXPECT_EQ(16, ftello(fp));
1477 ASSERT_EQ(0, fclose(fp));
1478 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1479 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1480 EXPECT_EQ(16, ftell(fp));
1481 EXPECT_EQ(16, ftello(fp));
1482 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1483 EXPECT_EQ(16, ftell(fp));
1484 EXPECT_EQ(16, ftello(fp));
1485 ASSERT_EQ(0, fclose(fp));
1486
1487 // "For modes w and w+ the initial size shall be zero..."
1488 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1489 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1490 EXPECT_EQ(0, ftell(fp));
1491 EXPECT_EQ(0, ftello(fp));
1492 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1493 EXPECT_EQ(0, ftell(fp));
1494 EXPECT_EQ(0, ftello(fp));
1495 ASSERT_EQ(0, fclose(fp));
1496 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1497 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1498 EXPECT_EQ(0, ftell(fp));
1499 EXPECT_EQ(0, ftello(fp));
1500 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1501 EXPECT_EQ(0, ftell(fp));
1502 EXPECT_EQ(0, ftello(fp));
1503 ASSERT_EQ(0, fclose(fp));
1504
1505 // "...and for modes a and a+ the initial size shall be:
1506 // 1. Zero, if buf is a null pointer
1507 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1508 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1509 EXPECT_EQ(0, ftell(fp));
1510 EXPECT_EQ(0, ftello(fp));
1511 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1512 EXPECT_EQ(0, ftell(fp));
1513 EXPECT_EQ(0, ftello(fp));
1514 ASSERT_EQ(0, fclose(fp));
1515 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1516 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1517 EXPECT_EQ(0, ftell(fp));
1518 EXPECT_EQ(0, ftello(fp));
1519 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1520 EXPECT_EQ(0, ftell(fp));
1521 EXPECT_EQ(0, ftello(fp));
1522 ASSERT_EQ(0, fclose(fp));
1523
1524 // 2. The position of the first null byte in the buffer, if one is found
1525 memset(buf, 'x', sizeof(buf));
1526 buf[3] = '\0';
1527 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1528 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1529 EXPECT_EQ(3, ftell(fp));
1530 EXPECT_EQ(3, ftello(fp));
1531 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1532 EXPECT_EQ(3, ftell(fp));
1533 EXPECT_EQ(3, ftello(fp));
1534 ASSERT_EQ(0, fclose(fp));
1535 memset(buf, 'x', sizeof(buf));
1536 buf[3] = '\0';
1537 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1538 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1539 EXPECT_EQ(3, ftell(fp));
1540 EXPECT_EQ(3, ftello(fp));
1541 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1542 EXPECT_EQ(3, ftell(fp));
1543 EXPECT_EQ(3, ftello(fp));
1544 ASSERT_EQ(0, fclose(fp));
1545
1546 // 3. The value of the size argument, if buf is not a null pointer and no
1547 // null byte is found.
1548 memset(buf, 'x', sizeof(buf));
1549 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1550 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1551 EXPECT_EQ(16, ftell(fp));
1552 EXPECT_EQ(16, ftello(fp));
1553 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1554 EXPECT_EQ(16, ftell(fp));
1555 EXPECT_EQ(16, ftello(fp));
1556 ASSERT_EQ(0, fclose(fp));
1557 memset(buf, 'x', sizeof(buf));
1558 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1559 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1560 EXPECT_EQ(16, ftell(fp));
1561 EXPECT_EQ(16, ftello(fp));
1562 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1563 EXPECT_EQ(16, ftell(fp));
1564 EXPECT_EQ(16, ftello(fp));
1565 ASSERT_EQ(0, fclose(fp));
1566}
1567
1568TEST(STDIO_TEST, fmemopen_SEEK_END) {
1569 // fseek SEEK_END is relative to the current string length, not the buffer size.
1570 FILE* fp;
1571 char buf[8];
1572 memset(buf, 'x', sizeof(buf));
1573 strcpy(buf, "str");
1574 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1575 ASSERT_NE(EOF, fputs("string", fp));
1576 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1577 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1578 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1579 EXPECT_EQ(0, fclose(fp));
1580
1581 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1582 // than adding).
1583 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1584 ASSERT_NE(EOF, fputs("54321", fp));
1585 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1586 EXPECT_EQ('2', fgetc(fp));
1587 EXPECT_EQ(0, fclose(fp));
1588}
1589
1590TEST(STDIO_TEST, fmemopen_seek_invalid) {
1591 char buf[8];
1592 memset(buf, 'x', sizeof(buf));
1593 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1594 ASSERT_TRUE(fp != nullptr);
1595
1596 // POSIX: "An attempt to seek ... to a negative position or to a position
1597 // larger than the buffer size given in the size argument shall fail."
1598 // (There's no mention of what errno should be set to, and glibc doesn't
1599 // set errno in any of these cases.)
1600 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1601 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1602 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1603 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1604}
1605
1606TEST(STDIO_TEST, fmemopen_read_EOF) {
1607 // POSIX: "A read operation on the stream shall not advance the current
1608 // buffer position beyond the current buffer size."
1609 char buf[8];
1610 memset(buf, 'x', sizeof(buf));
1611 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1612 ASSERT_TRUE(fp != nullptr);
1613 char buf2[BUFSIZ];
1614 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1615 // POSIX: "Reaching the buffer size in a read operation shall count as
1616 // end-of-file.
1617 ASSERT_TRUE(feof(fp));
1618 ASSERT_EQ(EOF, fgetc(fp));
1619 ASSERT_EQ(0, fclose(fp));
1620}
1621
1622TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1623 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1624 char buf[] = "h\0e\0l\0l\0o";
1625 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1626 ASSERT_TRUE(fp != nullptr);
1627 ASSERT_EQ('h', fgetc(fp));
1628 ASSERT_EQ(0, fgetc(fp));
1629 ASSERT_EQ('e', fgetc(fp));
1630 ASSERT_EQ(0, fgetc(fp));
1631 ASSERT_EQ('l', fgetc(fp));
1632 ASSERT_EQ(0, fgetc(fp));
1633 // POSIX: "The read operation shall start at the current buffer position of
1634 // the stream."
1635 char buf2[8];
1636 memset(buf2, 'x', sizeof(buf2));
1637 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1638 ASSERT_EQ('l', buf2[0]);
1639 ASSERT_EQ(0, buf2[1]);
1640 ASSERT_EQ('o', buf2[2]);
1641 ASSERT_EQ(0, buf2[3]);
1642 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1643 ASSERT_TRUE(feof(fp));
1644 ASSERT_EQ(0, fclose(fp));
1645}
1646
1647TEST(STDIO_TEST, fmemopen_write) {
1648 FILE* fp;
1649 char buf[8];
1650
1651 // POSIX: "A write operation shall start either at the current position of
1652 // the stream (if mode has not specified 'a' as the first character)..."
1653 memset(buf, 'x', sizeof(buf));
1654 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1655 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1656 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1657 ASSERT_EQ(' ', fputc(' ', fp));
1658 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1659 ASSERT_EQ(0, fclose(fp));
1660
1661 // "...or at the current size of the stream (if mode had 'a' as the first
1662 // character)." (See the fmemopen_size test for what "size" means, but for
1663 // mode "a", it's the first NUL byte.)
1664 memset(buf, 'x', sizeof(buf));
1665 buf[3] = '\0';
1666 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1667 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1668 ASSERT_EQ(' ', fputc(' ', fp));
1669 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1670 ASSERT_EQ(0, fclose(fp));
1671
1672 // "If the current position at the end of the write is larger than the
1673 // current buffer size, the current buffer size shall be set to the current
1674 // position." (See the fmemopen_size test for what "size" means, but to
1675 // query it we SEEK_END with offset 0, and then ftell.)
1676 memset(buf, 'x', sizeof(buf));
1677 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1678 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1679 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1680 EXPECT_EQ(0, ftell(fp));
1681 ASSERT_EQ(' ', fputc(' ', fp));
1682 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1683 EXPECT_EQ(1, ftell(fp));
1684 ASSERT_NE(EOF, fputs("123", fp));
1685 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1686 EXPECT_EQ(4, ftell(fp));
1687 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1688 ASSERT_EQ(0, fclose(fp));
1689}
1690
1691TEST(STDIO_TEST, fmemopen_write_EOF) {
1692 // POSIX: "A write operation on the stream shall not advance the current
1693 // buffer size beyond the size given in the size argument."
1694 FILE* fp;
1695
1696 // Scalar writes...
1697 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1698 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1699 ASSERT_EQ('x', fputc('x', fp));
1700 ASSERT_EQ('x', fputc('x', fp));
1701 ASSERT_EQ('x', fputc('x', fp));
1702 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1703 ASSERT_EQ(0, fclose(fp));
1704
1705 // Vector writes...
1706 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1707 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1708 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1709 ASSERT_EQ(0, fclose(fp));
1710}
1711
1712TEST(STDIO_TEST, fmemopen_initial_position) {
1713 // POSIX: "The ... current position in the buffer ... shall be initially
1714 // set to either the beginning of the buffer (for r and w modes) ..."
1715 char buf[] = "hello\0world";
1716 FILE* fp;
1717 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1718 EXPECT_EQ(0L, ftell(fp));
1719 EXPECT_EQ(0, fclose(fp));
1720 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1721 EXPECT_EQ(0L, ftell(fp));
1722 EXPECT_EQ(0, fclose(fp));
1723 buf[0] = 'h'; // (Undo the effects of the above.)
1724
1725 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1726 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1727 EXPECT_EQ(5L, ftell(fp));
1728 EXPECT_EQ(0, fclose(fp));
1729
1730 // POSIX: "If no null byte is found in append mode, the initial position
1731 // shall be set to one byte after the end of the buffer."
1732 memset(buf, 'x', sizeof(buf));
1733 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1734 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1735 EXPECT_EQ(0, fclose(fp));
1736}
1737
1738TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1739 // POSIX: "If buf is a null pointer, the initial position shall always be
1740 // set to the beginning of the buffer."
1741 FILE* fp = fmemopen(nullptr, 128, "a+");
1742 ASSERT_TRUE(fp != nullptr);
1743 EXPECT_EQ(0L, ftell(fp));
1744 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1745 EXPECT_EQ(0, fclose(fp));
1746}
1747
1748TEST(STDIO_TEST, fmemopen_zero_length) {
1749 // POSIX says it's up to the implementation whether or not you can have a
1750 // zero-length buffer (but "A future version of this standard may require
1751 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1752 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1753 FILE* fp;
1754 char buf[16];
1755 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1756 ASSERT_EQ(EOF, fgetc(fp));
1757 ASSERT_TRUE(feof(fp));
1758 ASSERT_EQ(0, fclose(fp));
1759 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1760 ASSERT_EQ(EOF, fgetc(fp));
1761 ASSERT_TRUE(feof(fp));
1762 ASSERT_EQ(0, fclose(fp));
1763
1764 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1765 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1766 ASSERT_EQ(EOF, fputc('x', fp));
1767 ASSERT_EQ(0, fclose(fp));
1768 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1769 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1770 ASSERT_EQ(EOF, fputc('x', fp));
1771 ASSERT_EQ(0, fclose(fp));
1772}
1773
1774TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1775 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1776 // BSD fails, glibc doesn't. We side with the more lenient.
1777 FILE* fp;
1778 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1779 ASSERT_EQ(0, fclose(fp));
1780 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1781 ASSERT_EQ(0, fclose(fp));
1782}
1783
1784TEST(STDIO_TEST, fmemopen_fileno) {
1785 // There's no fd backing an fmemopen FILE*.
1786 FILE* fp = fmemopen(nullptr, 16, "r");
1787 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001788 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001789 ASSERT_EQ(-1, fileno(fp));
1790 ASSERT_EQ(EBADF, errno);
1791 ASSERT_EQ(0, fclose(fp));
1792}
1793
1794TEST(STDIO_TEST, fmemopen_append_after_seek) {
1795 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1796 // there had been an intervening seek.
1797
1798 FILE* fp;
1799 char buf[] = "hello\0world";
1800 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1801 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1802 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1803 ASSERT_NE(EOF, fputc('!', fp));
1804 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1805 ASSERT_EQ(0, fclose(fp));
1806
1807 memcpy(buf, "hello\0world", sizeof(buf));
1808 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1809 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1810 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1811 ASSERT_NE(EOF, fputc('!', fp));
1812 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1813 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001814}
1815
Christopher Ferris13f26a72016-01-13 13:47:58 -08001816TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001817 char* p = nullptr;
1818 size_t size = 0;
1819 FILE* fp = open_memstream(&p, &size);
1820 ASSERT_NE(EOF, fputs("hello, world!", fp));
1821 fclose(fp);
1822
1823 ASSERT_STREQ("hello, world!", p);
1824 ASSERT_EQ(strlen("hello, world!"), size);
1825 free(p);
1826}
1827
Christopher Ferris13f26a72016-01-13 13:47:58 -08001828TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001829#if defined(__BIONIC__)
1830 char* p;
1831 size_t size;
1832
1833 // Invalid buffer.
1834 errno = 0;
1835 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1836 ASSERT_EQ(EINVAL, errno);
1837
1838 // Invalid size.
1839 errno = 0;
1840 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1841 ASSERT_EQ(EINVAL, errno);
1842#else
Elliott Hughes9677fab2016-01-25 15:50:59 -08001843 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001844#endif
1845}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001846
Christopher Ferris13f26a72016-01-13 13:47:58 -08001847TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001848 int fd = open("/proc/version", O_RDONLY);
1849 ASSERT_TRUE(fd != -1);
1850
1851 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001852 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001853
1854 FILE* fp = fdopen(fd, "re");
1855 ASSERT_TRUE(fp != NULL);
1856
1857 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001858 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001859
1860 fclose(fp);
1861 close(fd);
1862}
1863
Christopher Ferris13f26a72016-01-13 13:47:58 -08001864TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001865 FILE* fp = fopen("/proc/version", "r");
1866 ASSERT_TRUE(fp != NULL);
1867
1868 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001869 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001870
1871 fp = freopen("/proc/version", "re", fp);
1872
1873 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001874 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001875
1876 fclose(fp);
1877}
Elliott Hughes20841a12014-12-01 16:13:30 -08001878
Elliott Hughesf226ee52016-02-03 11:24:28 -08001879TEST(STDIO_TEST, fopen64_freopen64) {
1880 FILE* fp = fopen64("/proc/version", "r");
1881 ASSERT_TRUE(fp != nullptr);
1882 fp = freopen64("/proc/version", "re", fp);
1883 ASSERT_TRUE(fp != nullptr);
1884 fclose(fp);
1885}
1886
Elliott Hughes20841a12014-12-01 16:13:30 -08001887// https://code.google.com/p/android/issues/detail?id=81155
1888// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001889TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001890 FILE* fp = fopen("/dev/zero", "r");
1891 ASSERT_TRUE(fp != NULL);
1892
1893 // Make this stream unbuffered.
1894 setvbuf(fp, 0, _IONBF, 0);
1895
1896 char buf[65*1024];
1897 memset(buf, 0xff, sizeof(buf));
1898
1899 time_t t0 = time(NULL);
1900 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001901 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001902 }
1903 time_t t1 = time(NULL);
1904
1905 fclose(fp);
1906
1907 // 1024 64KiB reads should have been very quick.
1908 ASSERT_LE(t1 - t0, 1);
1909
1910 for (size_t i = 0; i < 64*1024; ++i) {
1911 ASSERT_EQ('\0', buf[i]);
1912 }
1913 for (size_t i = 64*1024; i < 65*1024; ++i) {
1914 ASSERT_EQ('\xff', buf[i]);
1915 }
1916}
Elliott Hughes75b99382015-01-20 11:23:50 -08001917
Christopher Ferris13f26a72016-01-13 13:47:58 -08001918TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001919 std::string digits("0123456789");
1920 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001921
1922 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1923 char buf1[4 * 4];
1924 memset(buf1, 0, sizeof(buf1));
1925 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001926 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001927 ASSERT_TRUE(feof(fp));
1928
1929 rewind(fp);
1930
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001931 // Try to read way too much so stdio tries to read more direct from the stream.
1932 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001933 memset(buf2, 0, sizeof(buf2));
1934 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001935 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001936 ASSERT_TRUE(feof(fp));
1937
1938 fclose(fp);
1939}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001940
1941static void test_fread_from_write_only_stream(size_t n) {
1942 FILE* fp = fopen("/dev/null", "w");
1943 std::vector<char> buf(n, 0);
1944 errno = 0;
1945 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
1946 ASSERT_EQ(EBADF, errno);
1947 ASSERT_TRUE(ferror(fp));
1948 ASSERT_FALSE(feof(fp));
1949 fclose(fp);
1950}
1951
Christopher Ferris13f26a72016-01-13 13:47:58 -08001952TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001953 test_fread_from_write_only_stream(1);
1954}
1955
Christopher Ferris13f26a72016-01-13 13:47:58 -08001956TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001957 test_fread_from_write_only_stream(64*1024);
1958}
1959
1960static void test_fwrite_after_fread(size_t n) {
1961 TemporaryFile tf;
1962
1963 FILE* fp = fdopen(tf.fd, "w+");
1964 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1965 fflush(fp);
1966
1967 // We've flushed but not rewound, so there's nothing to read.
1968 std::vector<char> buf(n, 0);
1969 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1970 ASSERT_TRUE(feof(fp));
1971
1972 // But hitting EOF doesn't prevent us from writing...
1973 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08001974 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001975
1976 // And if we rewind, everything's there.
1977 rewind(fp);
1978 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1979 ASSERT_EQ('1', buf[0]);
1980 ASSERT_EQ('2', buf[1]);
1981
1982 fclose(fp);
1983}
1984
Christopher Ferris13f26a72016-01-13 13:47:58 -08001985TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001986 test_fwrite_after_fread(16);
1987}
1988
Christopher Ferris13f26a72016-01-13 13:47:58 -08001989TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001990 test_fwrite_after_fread(64*1024);
1991}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001992
1993// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08001994TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001995 TemporaryFile tf;
1996
1997 FILE* fp = fopen(tf.filename, "w+");
1998 ASSERT_TRUE(fp != nullptr);
1999
2000 char file_data[12288];
2001 for (size_t i = 0; i < 12288; i++) {
2002 file_data[i] = i;
2003 }
2004 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2005 fclose(fp);
2006
2007 fp = fopen(tf.filename, "r");
2008 ASSERT_TRUE(fp != nullptr);
2009
2010 char buffer[8192];
2011 size_t cur_location = 0;
2012 // Small read to populate internal buffer.
2013 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2014 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2015
2016 cur_location = static_cast<size_t>(ftell(fp));
2017 // Large read to force reading into the user supplied buffer and bypassing
2018 // the internal buffer.
2019 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2020 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2021
2022 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002023 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002024 cur_location = static_cast<size_t>(ftell(fp));
2025 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2026 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2027
2028 fclose(fp);
2029}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002030
2031// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002032TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002033 TemporaryFile tf;
2034 char buf[6] = {0};
2035
2036 FILE* fw = fopen(tf.filename, "w");
2037 ASSERT_TRUE(fw != nullptr);
2038
2039 FILE* fr = fopen(tf.filename, "r");
2040 ASSERT_TRUE(fr != nullptr);
2041
2042 fwrite("a", 1, 1, fw);
2043 fflush(fw);
2044 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2045 ASSERT_STREQ("a", buf);
2046
2047 // 'fr' is now at EOF.
2048 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2049 ASSERT_TRUE(feof(fr));
2050
2051 // Write some more...
2052 fwrite("z", 1, 1, fw);
2053 fflush(fw);
2054
2055 // ...and check that we can read it back.
2056 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2057 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2058 ASSERT_STREQ("z", buf);
2059
2060 // But now we're done.
2061 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2062
2063 fclose(fr);
2064 fclose(fw);
2065}
Elliott Hughes923f1652016-01-19 15:46:05 -08002066
2067TEST(STDIO_TEST, fclose_invalidates_fd) {
2068 // The typical error we're trying to help people catch involves accessing
2069 // memory after it's been freed. But we know that stdin/stdout/stderr are
2070 // special and don't get deallocated, so this test uses stdin.
2071 ASSERT_EQ(0, fclose(stdin));
2072
2073 // Even though using a FILE* after close is undefined behavior, I've closed
2074 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2075 // especially because they might actually correspond to a real stream.
2076 errno = 0;
2077 ASSERT_EQ(-1, fileno(stdin));
2078 ASSERT_EQ(EBADF, errno);
2079}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002080
2081TEST(STDIO_TEST, fseek_ftell_unseekable) {
2082#if defined(__BIONIC__) // glibc has fopencookie instead.
2083 auto read_fn = [](void*, char*, int) { return -1; };
2084 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2085 ASSERT_TRUE(fp != nullptr);
2086
2087 // Check that ftell balks on an unseekable FILE*.
2088 errno = 0;
2089 ASSERT_EQ(-1, ftell(fp));
2090 ASSERT_EQ(ESPIPE, errno);
2091
2092 // SEEK_CUR is rewritten as SEEK_SET internally...
2093 errno = 0;
2094 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2095 ASSERT_EQ(ESPIPE, errno);
2096
2097 // ...so it's worth testing the direct seek path too.
2098 errno = 0;
2099 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2100 ASSERT_EQ(ESPIPE, errno);
2101
2102 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002103#else
2104 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
2105#endif
2106}
2107
2108TEST(STDIO_TEST, funopen_EINVAL) {
2109#if defined(__BIONIC__)
2110 errno = 0;
2111 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2112 ASSERT_EQ(EINVAL, errno);
2113#else
2114 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
2115#endif
2116}
2117
2118TEST(STDIO_TEST, funopen_seek) {
2119#if defined(__BIONIC__)
2120 auto read_fn = [](void*, char*, int) { return -1; };
2121
2122 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2123 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2124
2125 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2126 ASSERT_TRUE(fp != nullptr);
2127 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002128#if defined(__LP64__)
2129 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2130 EXPECT_EQ(0xfedcba12LL, pos);
2131#else
2132 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2133 EXPECT_EQ(EOVERFLOW, errno);
2134#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002135
2136 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2137 ASSERT_TRUE(fp64 != nullptr);
2138 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002139 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2140 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002141#else
2142 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002143#endif
2144}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002145
2146TEST(STDIO_TEST, lots_of_concurrent_files) {
2147 std::vector<TemporaryFile*> tfs;
2148 std::vector<FILE*> fps;
2149
2150 for (size_t i = 0; i < 256; ++i) {
2151 TemporaryFile* tf = new TemporaryFile;
2152 tfs.push_back(tf);
2153 FILE* fp = fopen(tf->filename, "w+");
2154 fps.push_back(fp);
2155 fprintf(fp, "hello %zu!\n", i);
2156 fflush(fp);
2157 }
2158
2159 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002160 char expected[BUFSIZ];
2161 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002162
Elliott Hughes70715da2016-08-01 16:35:17 -07002163 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002164 fclose(fps[i]);
2165 delete tfs[i];
2166 }
2167}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002168
2169static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2170 EXPECT_EQ(offset, ftell(fp));
2171 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002172 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002173 fpos_t pos;
2174 fpos64_t pos64;
2175 EXPECT_EQ(0, fgetpos(fp, &pos));
2176 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2177#if defined(__BIONIC__)
2178 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2179 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2180#else
2181 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
2182#endif
2183}
2184
2185TEST(STDIO_TEST, seek_tell_family_smoke) {
2186 TemporaryFile tf;
2187 FILE* fp = fdopen(tf.fd, "w+");
2188
2189 // Initially we should be at 0.
2190 AssertFileOffsetAt(fp, 0);
2191
2192 // Seek to offset 8192.
2193 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2194 AssertFileOffsetAt(fp, 8192);
2195 fpos_t eight_k_pos;
2196 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2197
2198 // Seek forward another 8192...
2199 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2200 AssertFileOffsetAt(fp, 8192 + 8192);
2201 fpos64_t sixteen_k_pos64;
2202 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2203
2204 // Seek back 8192...
2205 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2206 AssertFileOffsetAt(fp, 8192);
2207
2208 // Since we haven't written anything, the end is also at 0.
2209 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2210 AssertFileOffsetAt(fp, 0);
2211
2212 // Check that our fpos64_t from 16KiB works...
2213 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2214 AssertFileOffsetAt(fp, 8192 + 8192);
2215 // ...as does our fpos_t from 8192.
2216 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2217 AssertFileOffsetAt(fp, 8192);
2218
2219 // Do fseeko and fseeko64 work too?
2220 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2221 AssertFileOffsetAt(fp, 1234);
2222 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2223 AssertFileOffsetAt(fp, 5678);
2224
2225 fclose(fp);
2226}
2227
2228TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2229 TemporaryFile tf;
2230 FILE* fp = fdopen(tf.fd, "w+");
2231
2232 // Bad whence.
2233 errno = 0;
2234 ASSERT_EQ(-1, fseek(fp, 0, 123));
2235 ASSERT_EQ(EINVAL, errno);
2236 errno = 0;
2237 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2238 ASSERT_EQ(EINVAL, errno);
2239 errno = 0;
2240 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2241 ASSERT_EQ(EINVAL, errno);
2242
2243 // Bad offset.
2244 errno = 0;
2245 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2246 ASSERT_EQ(EINVAL, errno);
2247 errno = 0;
2248 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2249 ASSERT_EQ(EINVAL, errno);
2250 errno = 0;
2251 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2252 ASSERT_EQ(EINVAL, errno);
2253
2254 fclose(fp);
2255}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002256
2257TEST(STDIO_TEST, ctermid) {
2258 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2259
2260 char buf[L_ctermid] = {};
2261 ASSERT_EQ(buf, ctermid(buf));
2262 ASSERT_STREQ("/dev/tty", buf);
2263}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002264
2265TEST(STDIO_TEST, remove) {
2266 struct stat sb;
2267
2268 TemporaryFile tf;
2269 ASSERT_EQ(0, remove(tf.filename));
2270 ASSERT_EQ(-1, lstat(tf.filename, &sb));
2271 ASSERT_EQ(ENOENT, errno);
2272
2273 TemporaryDir td;
2274 ASSERT_EQ(0, remove(td.dirname));
2275 ASSERT_EQ(-1, lstat(td.dirname, &sb));
2276 ASSERT_EQ(ENOENT, errno);
2277
2278 errno = 0;
2279 ASSERT_EQ(-1, remove(tf.filename));
2280 ASSERT_EQ(ENOENT, errno);
2281
2282 errno = 0;
2283 ASSERT_EQ(-1, remove(td.dirname));
2284 ASSERT_EQ(ENOENT, errno);
2285}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002286
2287TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2288 char buf[16];
2289 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2290 testing::KilledBySignal(SIGABRT),
2291#if defined(NOFORTIFY)
2292 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2293#else
2294 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2295#endif
2296 );
2297}
2298
2299TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2300 std::string buf = "world";
2301 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2302 testing::KilledBySignal(SIGABRT),
2303 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2304}
2305
2306TEST(STDIO_TEST, sprintf_30445072) {
2307 std::string buf = "world";
2308 sprintf(&buf[0], "hello");
2309 ASSERT_EQ(buf, "hello");
2310}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002311
2312TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2313 TemporaryFile tf;
2314 SetFileTo(tf.filename, "0123456789");
2315 FILE* fp = fopen(tf.filename, "a");
2316 EXPECT_EQ(10, ftell(fp));
2317 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2318 EXPECT_EQ(2, ftell(fp));
2319 ASSERT_NE(EOF, fputs("xxx", fp));
2320 ASSERT_EQ(0, fflush(fp));
2321 EXPECT_EQ(13, ftell(fp));
2322 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2323 EXPECT_EQ(13, ftell(fp));
2324 ASSERT_EQ(0, fclose(fp));
2325 AssertFileIs(tf.filename, "0123456789xxx");
2326}
2327
2328TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2329 TemporaryFile tf;
2330 SetFileTo(tf.filename, "0123456789");
2331 int fd = open(tf.filename, O_RDWR);
2332 ASSERT_NE(-1, fd);
2333 // POSIX: "The file position indicator associated with the new stream is set to the position
2334 // indicated by the file offset associated with the file descriptor."
2335 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2336 FILE* fp = fdopen(fd, "a");
2337 EXPECT_EQ(4, ftell(fp));
2338 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2339 EXPECT_EQ(2, ftell(fp));
2340 ASSERT_NE(EOF, fputs("xxx", fp));
2341 ASSERT_EQ(0, fflush(fp));
2342 EXPECT_EQ(13, ftell(fp));
2343 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2344 EXPECT_EQ(13, ftell(fp));
2345 ASSERT_EQ(0, fclose(fp));
2346 AssertFileIs(tf.filename, "0123456789xxx");
2347}
2348
2349TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2350 TemporaryFile tf;
2351 SetFileTo(tf.filename, "0123456789");
2352 FILE* other_fp = fopen("/proc/version", "r");
2353 FILE* fp = freopen(tf.filename, "a", other_fp);
2354 EXPECT_EQ(10, ftell(fp));
2355 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2356 EXPECT_EQ(2, ftell(fp));
2357 ASSERT_NE(EOF, fputs("xxx", fp));
2358 ASSERT_EQ(0, fflush(fp));
2359 EXPECT_EQ(13, ftell(fp));
2360 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2361 EXPECT_EQ(13, ftell(fp));
2362 ASSERT_EQ(0, fclose(fp));
2363 AssertFileIs(tf.filename, "0123456789xxx");
2364}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002365
2366TEST(STDIO_TEST, constants) {
2367 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2368 ASSERT_EQ(L_tmpnam, PATH_MAX);
2369}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002370
2371TEST(STDIO_TEST, perror) {
2372 ExecTestHelper eth;
2373 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2374 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2375 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2376}
2377
2378TEST(STDIO_TEST, puts) {
2379 ExecTestHelper eth;
2380 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2381}
2382
2383TEST(STDIO_TEST, unlocked) {
2384 TemporaryFile tf;
2385
2386 FILE* fp = fopen(tf.filename, "w+");
2387 ASSERT_TRUE(fp != nullptr);
2388
2389 clearerr_unlocked(fp);
2390 ASSERT_FALSE(feof_unlocked(fp));
2391 ASSERT_FALSE(ferror_unlocked(fp));
2392
2393 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2394
2395 ASSERT_NE(EOF, putc_unlocked('a', fp));
2396 ASSERT_NE(EOF, putc('b', fp));
2397 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2398 ASSERT_NE(EOF, fputc('d', fp));
2399
2400 rewind(fp);
2401 ASSERT_EQ('a', getc_unlocked(fp));
2402 ASSERT_EQ('b', getc(fp));
2403 ASSERT_EQ('c', fgetc_unlocked(fp));
2404 ASSERT_EQ('d', fgetc(fp));
2405
2406 rewind(fp);
2407 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2408 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2409 ASSERT_EQ(0, fflush_unlocked(fp));
2410
2411 rewind(fp);
2412 char buf[BUFSIZ] = {};
2413 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2414 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2415 ASSERT_STREQ("ABCD", buf);
2416
2417 rewind(fp);
2418 ASSERT_NE(EOF, fputs("hello ", fp));
2419 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2420 ASSERT_NE(EOF, fputc('\n', fp));
2421
2422 rewind(fp);
2423 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2424 ASSERT_STREQ("hello world\n", buf);
2425
2426 ASSERT_EQ(0, fclose(fp));
2427}
Ryan Prichardbf549862017-11-07 15:30:32 -08002428
2429TEST(STDIO_TEST, fseek_64bit) {
2430 TemporaryFile tf;
2431 FILE* fp = fopen64(tf.filename, "w+");
2432 ASSERT_TRUE(fp != nullptr);
2433 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2434 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2435 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2436 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2437 ASSERT_EQ(0, fclose(fp));
2438}
2439
2440// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2441// isn't representable in long/off_t.
2442TEST(STDIO_TEST, fseek_overflow_32bit) {
2443 TemporaryFile tf;
2444 FILE* fp = fopen64(tf.filename, "w+");
2445 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2446
2447 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2448#if defined(__BIONIC__) && !defined(__LP64__)
2449 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2450 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2451 ASSERT_EQ(EOVERFLOW, errno);
2452#endif
2453
2454 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2455 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2456 // and SEEK_END -- many C libraries check neither.)
2457 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2458 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2459
2460 fclose(fp);
2461}