blob: d0d91309e088a4f76be95be2f9a0e64e1ce53fa0 [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 Hughese2341d02014-05-02 18:16:32 -0700338 // http://b/14492135
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700339 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700340 int i = 1234;
341 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
342 EXPECT_EQ(1234, i);
343 EXPECT_STREQ("a n b", buf);
344#else
Elliott Hughes9677fab2016-01-25 15:50:59 -0800345 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700346#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700347}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700348
Christopher Ferris13f26a72016-01-13 13:47:58 -0800349TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700350 char buf[BUFSIZ];
351
352 snprintf(buf, sizeof(buf), "a");
353 EXPECT_STREQ("a", buf);
354
355 snprintf(buf, sizeof(buf), "%%");
356 EXPECT_STREQ("%", buf);
357
358 snprintf(buf, sizeof(buf), "01234");
359 EXPECT_STREQ("01234", buf);
360
361 snprintf(buf, sizeof(buf), "a%sb", "01234");
362 EXPECT_STREQ("a01234b", buf);
363
364 char* s = NULL;
365 snprintf(buf, sizeof(buf), "a%sb", s);
366 EXPECT_STREQ("a(null)b", buf);
367
368 snprintf(buf, sizeof(buf), "aa%scc", "bb");
369 EXPECT_STREQ("aabbcc", buf);
370
371 snprintf(buf, sizeof(buf), "a%cc", 'b');
372 EXPECT_STREQ("abc", buf);
373
374 snprintf(buf, sizeof(buf), "a%db", 1234);
375 EXPECT_STREQ("a1234b", buf);
376
377 snprintf(buf, sizeof(buf), "a%db", -8123);
378 EXPECT_STREQ("a-8123b", buf);
379
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700380 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700381 EXPECT_STREQ("a16b", buf);
382
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700383 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700384 EXPECT_STREQ("a16b", buf);
385
386 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
387 EXPECT_STREQ("a68719476736b", buf);
388
389 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
390 EXPECT_STREQ("a70000b", buf);
391
392 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
393 EXPECT_STREQ("a0xb0001234b", buf);
394
395 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
396 EXPECT_STREQ("a12abz", buf);
397
398 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
399 EXPECT_STREQ("a12ABz", buf);
400
401 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
402 EXPECT_STREQ("a00123456z", buf);
403
404 snprintf(buf, sizeof(buf), "a%5dz", 1234);
405 EXPECT_STREQ("a 1234z", buf);
406
407 snprintf(buf, sizeof(buf), "a%05dz", 1234);
408 EXPECT_STREQ("a01234z", buf);
409
410 snprintf(buf, sizeof(buf), "a%8dz", 1234);
411 EXPECT_STREQ("a 1234z", buf);
412
413 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
414 EXPECT_STREQ("a1234 z", buf);
415
416 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
417 EXPECT_STREQ("Aabcdef Z", buf);
418
419 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
420 EXPECT_STREQ("Ahello:1234Z", buf);
421
422 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
423 EXPECT_STREQ("a005:5:05z", buf);
424
425 void* p = NULL;
426 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700427#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700428 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800429#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700430 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800431#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700432
433 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
434 EXPECT_STREQ("a68719476736,6,7,8z", buf);
435
436 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
437 EXPECT_STREQ("a_1.230000_b", buf);
438
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700439 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700440 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400441
442 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
443 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700444}
445
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800446template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700447static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
448 int sscanf_fn(const T*, const T*, ...),
449 const T* fmt_string, const T* fmt, const T* fmt_plus,
450 const T* minus_inf, const T* inf_, const T* plus_inf,
451 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800452 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700453 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700454
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700455 // NaN.
456
457 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800458 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700459 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
460 EXPECT_TRUE(isnan(f));
461
462 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800463 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700464 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
465 EXPECT_TRUE(isnan(f));
466
467 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800468 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700469 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
470 EXPECT_TRUE(isnan(f));
471
472 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800473 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700474 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
475 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800476
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700477 // Inf.
478
479 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800480 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700481 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
482 EXPECT_EQ(HUGE_VALF, f);
483
484 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800485 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700486 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
487 EXPECT_EQ(-HUGE_VALF, f);
488
489 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800490 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700491 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
492 EXPECT_EQ(HUGE_VALF, f);
493
494 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800495 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700496 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
497 EXPECT_EQ(-HUGE_VALF, f);
498
499 // Check case-insensitivity.
500 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
501 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
502 EXPECT_EQ(HUGE_VALF, f);
503 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
504 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
505 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700506}
507
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700508TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
509 CheckInfNan(snprintf, sscanf, "%s",
510 "[%a]", "[%+a]",
511 "[-inf]", "[inf]", "[+inf]",
512 "[-nan]", "[nan]", "[+nan]");
513 CheckInfNan(snprintf, sscanf, "%s",
514 "[%A]", "[%+A]",
515 "[-INF]", "[INF]", "[+INF]",
516 "[-NAN]", "[NAN]", "[+NAN]");
517 CheckInfNan(snprintf, sscanf, "%s",
518 "[%e]", "[%+e]",
519 "[-inf]", "[inf]", "[+inf]",
520 "[-nan]", "[nan]", "[+nan]");
521 CheckInfNan(snprintf, sscanf, "%s",
522 "[%E]", "[%+E]",
523 "[-INF]", "[INF]", "[+INF]",
524 "[-NAN]", "[NAN]", "[+NAN]");
525 CheckInfNan(snprintf, sscanf, "%s",
526 "[%f]", "[%+f]",
527 "[-inf]", "[inf]", "[+inf]",
528 "[-nan]", "[nan]", "[+nan]");
529 CheckInfNan(snprintf, sscanf, "%s",
530 "[%F]", "[%+F]",
531 "[-INF]", "[INF]", "[+INF]",
532 "[-NAN]", "[NAN]", "[+NAN]");
533 CheckInfNan(snprintf, sscanf, "%s",
534 "[%g]", "[%+g]",
535 "[-inf]", "[inf]", "[+inf]",
536 "[-nan]", "[nan]", "[+nan]");
537 CheckInfNan(snprintf, sscanf, "%s",
538 "[%G]", "[%+G]",
539 "[-INF]", "[INF]", "[+INF]",
540 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800541}
Elliott Hughes7823f322014-04-14 12:11:28 -0700542
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700543TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
544 CheckInfNan(swprintf, swscanf, L"%s",
545 L"[%a]", L"[%+a]",
546 L"[-inf]", L"[inf]", L"[+inf]",
547 L"[-nan]", L"[nan]", L"[+nan]");
548 CheckInfNan(swprintf, swscanf, L"%s",
549 L"[%A]", L"[%+A]",
550 L"[-INF]", L"[INF]", L"[+INF]",
551 L"[-NAN]", L"[NAN]", L"[+NAN]");
552 CheckInfNan(swprintf, swscanf, L"%s",
553 L"[%e]", L"[%+e]",
554 L"[-inf]", L"[inf]", L"[+inf]",
555 L"[-nan]", L"[nan]", L"[+nan]");
556 CheckInfNan(swprintf, swscanf, L"%s",
557 L"[%E]", L"[%+E]",
558 L"[-INF]", L"[INF]", L"[+INF]",
559 L"[-NAN]", L"[NAN]", L"[+NAN]");
560 CheckInfNan(swprintf, swscanf, L"%s",
561 L"[%f]", L"[%+f]",
562 L"[-inf]", L"[inf]", L"[+inf]",
563 L"[-nan]", L"[nan]", L"[+nan]");
564 CheckInfNan(swprintf, swscanf, L"%s",
565 L"[%F]", L"[%+F]",
566 L"[-INF]", L"[INF]", L"[+INF]",
567 L"[-NAN]", L"[NAN]", L"[+NAN]");
568 CheckInfNan(swprintf, swscanf, L"%s",
569 L"[%g]", L"[%+g]",
570 L"[-inf]", L"[inf]", L"[+inf]",
571 L"[-nan]", L"[nan]", L"[+nan]");
572 CheckInfNan(swprintf, swscanf, L"%s",
573 L"[%G]", L"[%+G]",
574 L"[-INF]", L"[INF]", L"[+INF]",
575 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700576}
577
Dan Albert9601f162017-08-09 14:59:06 -0700578TEST(STDIO_TEST, swprintf) {
579 constexpr size_t nchars = 32;
580 wchar_t buf[nchars];
581
582 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
583 ASSERT_EQ(std::wstring(L"ab"), buf);
584 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
585 ASSERT_EQ(std::wstring(L"abcde"), buf);
586
587 // Unlike swprintf(), swprintf() returns -1 in case of truncation
588 // and doesn't necessarily zero-terminate the output!
589 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
590
591 const char kString[] = "Hello, World";
592 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
593 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
594 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
595 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
596}
597
598TEST(STDIO_TEST, swprintf_a) {
599 constexpr size_t nchars = 32;
600 wchar_t buf[nchars];
601
602 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
603 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
604}
605
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700606TEST(STDIO_TEST, swprintf_lc) {
607 constexpr size_t nchars = 32;
608 wchar_t buf[nchars];
609
610 wint_t wc = L'a';
611 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
612 EXPECT_EQ(std::wstring(L"<a>"), buf);
613}
614
615TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
616 constexpr size_t nchars = 32;
617 wchar_t buf[nchars];
618
619 wint_t wc = L'a';
620 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
621 EXPECT_EQ(std::wstring(L"<a>"), buf);
622}
623
Elliott Hughes618303c2017-11-02 16:58:44 -0700624TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
625 constexpr size_t nchars = 32;
626 wchar_t buf[nchars];
627
628 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
629 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
630}
631
632TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
633 constexpr size_t nchars = 32;
634 wchar_t buf[nchars];
635
636 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
637 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
638}
639
640TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
641 constexpr size_t nchars = 32;
642 wchar_t buf[nchars];
643
644 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
645 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
646}
647
648TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
649 constexpr size_t nchars = 32;
650 wchar_t buf[nchars];
651
652 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
653 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
654}
655
Dan Albert9601f162017-08-09 14:59:06 -0700656TEST(STDIO_TEST, swprintf_ls) {
657 constexpr size_t nchars = 32;
658 wchar_t buf[nchars];
659
660 static const wchar_t kWideString[] = L"Hello\uff41 World";
661 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
662 ASSERT_EQ(std::wstring(kWideString), buf);
663 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
664 ASSERT_EQ(std::wstring(kWideString), buf);
665}
666
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700667TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
668 constexpr size_t nchars = 32;
669 wchar_t buf[nchars];
670
671 static const wchar_t kWideString[] = L"Hello\uff41 World";
672 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
673 ASSERT_EQ(std::wstring(kWideString), buf);
674 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
675 ASSERT_EQ(std::wstring(kWideString), buf);
676}
677
Christopher Ferris13f26a72016-01-13 13:47:58 -0800678TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700679 char buf[BUFSIZ];
680 snprintf(buf, sizeof(buf), "%d", INT_MAX);
681 EXPECT_STREQ("2147483647", buf);
682}
683
Christopher Ferris13f26a72016-01-13 13:47:58 -0800684TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700685 char buf[BUFSIZ];
686 snprintf(buf, sizeof(buf), "%d", INT_MIN);
687 EXPECT_STREQ("-2147483648", buf);
688}
689
Elliott Hughes618303c2017-11-02 16:58:44 -0700690TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
691 char buf[BUFSIZ];
692 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
693 EXPECT_STREQ("9223372036854775807", buf);
694}
695
696TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
697 char buf[BUFSIZ];
698 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
699 EXPECT_STREQ("-9223372036854775808", buf);
700}
701
702TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
703 char buf[BUFSIZ];
704 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
705 EXPECT_STREQ("18446744073709551615", buf);
706}
707
708TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
709 char buf[BUFSIZ];
710 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
711 EXPECT_STREQ("18446744073709551615", buf);
712}
713
Christopher Ferris13f26a72016-01-13 13:47:58 -0800714TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700715 char buf[BUFSIZ];
716 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700717#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700718 EXPECT_STREQ("9223372036854775807", buf);
719#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700720 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700721#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700722}
723
Christopher Ferris13f26a72016-01-13 13:47:58 -0800724TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700725 char buf[BUFSIZ];
726 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700727#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700728 EXPECT_STREQ("-9223372036854775808", buf);
729#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700730 EXPECT_STREQ("-2147483648", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700731#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700732}
733
Christopher Ferris13f26a72016-01-13 13:47:58 -0800734TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700735 char buf[BUFSIZ];
736 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
737 EXPECT_STREQ("9223372036854775807", buf);
738}
739
Christopher Ferris13f26a72016-01-13 13:47:58 -0800740TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700741 char buf[BUFSIZ];
742 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
743 EXPECT_STREQ("-9223372036854775808", buf);
744}
745
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700746TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
747 char buf[BUFSIZ];
748 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
749 EXPECT_STREQ("37777777777", buf);
750}
751
752TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
753 char buf[BUFSIZ];
754 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
755 EXPECT_STREQ("4294967295", buf);
756}
757
758TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
759 char buf[BUFSIZ];
760 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
761 EXPECT_STREQ("ffffffff", buf);
762}
763
764TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
765 char buf[BUFSIZ];
766 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
767 EXPECT_STREQ("FFFFFFFF", buf);
768}
769
Christopher Ferris13f26a72016-01-13 13:47:58 -0800770TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700771 char buf[BUFSIZ];
772
773 snprintf(buf, sizeof(buf), "%e", 1.5);
774 EXPECT_STREQ("1.500000e+00", buf);
775
776 snprintf(buf, sizeof(buf), "%Le", 1.5l);
777 EXPECT_STREQ("1.500000e+00", buf);
778}
779
Christopher Ferris13f26a72016-01-13 13:47:58 -0800780TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700781 char buf[BUFSIZ];
782
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800783 snprintf(buf, sizeof(buf), "%e", -0.0);
784 EXPECT_STREQ("-0.000000e+00", buf);
785 snprintf(buf, sizeof(buf), "%E", -0.0);
786 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700787 snprintf(buf, sizeof(buf), "%f", -0.0);
788 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800789 snprintf(buf, sizeof(buf), "%F", -0.0);
790 EXPECT_STREQ("-0.000000", buf);
791 snprintf(buf, sizeof(buf), "%g", -0.0);
792 EXPECT_STREQ("-0", buf);
793 snprintf(buf, sizeof(buf), "%G", -0.0);
794 EXPECT_STREQ("-0", buf);
795 snprintf(buf, sizeof(buf), "%a", -0.0);
796 EXPECT_STREQ("-0x0p+0", buf);
797 snprintf(buf, sizeof(buf), "%A", -0.0);
798 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700799}
800
Christopher Ferris13f26a72016-01-13 13:47:58 -0800801TEST(STDIO_TEST, snprintf_utf8_15439554) {
Dan Albert1aec7c12014-07-30 10:53:48 -0700802 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700803 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700804
Elliott Hughes69f05d22014-06-05 20:10:09 -0700805 // http://b/15439554
806 char buf[BUFSIZ];
807
808 // 1-byte character.
809 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
810 EXPECT_STREQ("1x2", buf);
811 // 2-byte character.
812 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
813 EXPECT_STREQ("1¢2", buf);
814 // 3-byte character.
815 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
816 EXPECT_STREQ("1€2", buf);
817 // 4-byte character.
818 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
819 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700820
Wally Yaua40fdbd2014-08-26 09:47:23 -0700821 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700822 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700823}
824
Elliott Hughes43f7c872016-02-05 11:18:41 -0800825static void* snprintf_small_stack_fn(void*) {
826 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
827 char buf[PATH_MAX];
828 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
829 return nullptr;
830}
831
832TEST(STDIO_TEST, snprintf_small_stack) {
833 // Is it safe to call snprintf on a thread with a small stack?
834 // (The snprintf implementation puts some pretty large buffers on the stack.)
835 pthread_attr_t a;
836 ASSERT_EQ(0, pthread_attr_init(&a));
837 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
838
839 pthread_t t;
840 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
841 ASSERT_EQ(0, pthread_join(t, nullptr));
842}
843
Elliott Hughes8200e552016-02-05 21:57:37 -0800844TEST(STDIO_TEST, snprintf_asterisk_overflow) {
845 char buf[128];
846 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
847 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
848 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
849 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
850 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
851
852 // INT_MAX-1, INT_MAX, INT_MAX+1.
853 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
854 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
855 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
856 ASSERT_EQ(ENOMEM, errno);
857}
858
Elliott Hughes70715da2016-08-01 16:35:17 -0700859TEST(STDIO_TEST, fprintf) {
860 TemporaryFile tf;
861
862 FILE* tfile = fdopen(tf.fd, "r+");
863 ASSERT_TRUE(tfile != nullptr);
864
865 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
866 AssertFileIs(tfile, "123 abc");
867 fclose(tfile);
868}
869
Christopher Ferris13f26a72016-01-13 13:47:58 -0800870TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700871 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700872 FILE* fp;
873
874 // Unbuffered case where the fprintf(3) itself fails.
875 ASSERT_NE(nullptr, fp = tmpfile());
876 setbuf(fp, NULL);
877 ASSERT_EQ(4, fprintf(fp, "epic"));
878 ASSERT_EQ(0, close(fileno(fp)));
879 ASSERT_EQ(-1, fprintf(fp, "fail"));
880 ASSERT_EQ(-1, fclose(fp));
881
882 // Buffered case where we won't notice until the fclose(3).
883 // It's likely this is what was actually seen in http://b/7229520,
884 // and that expecting fprintf to fail is setting yourself up for
885 // disappointment. Remember to check fclose(3)'s return value, kids!
886 ASSERT_NE(nullptr, fp = tmpfile());
887 ASSERT_EQ(4, fprintf(fp, "epic"));
888 ASSERT_EQ(0, close(fileno(fp)));
889 ASSERT_EQ(4, fprintf(fp, "fail"));
890 ASSERT_EQ(-1, fclose(fp));
891}
892
Christopher Ferris13f26a72016-01-13 13:47:58 -0800893TEST(STDIO_TEST, popen) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800894 FILE* fp = popen("cat /proc/version", "r");
895 ASSERT_TRUE(fp != NULL);
896
897 char buf[16];
898 char* s = fgets(buf, sizeof(buf), fp);
899 buf[13] = '\0';
900 ASSERT_STREQ("Linux version", s);
901
902 ASSERT_EQ(0, pclose(fp));
903}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700904
Christopher Ferris13f26a72016-01-13 13:47:58 -0800905TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700906 FILE* fp = fopen("/proc/version", "r");
907 ASSERT_TRUE(fp != NULL);
908 ASSERT_EQ('L', getc(fp));
909 ASSERT_EQ('i', getc(fp));
910 ASSERT_EQ('n', getc(fp));
911 ASSERT_EQ('u', getc(fp));
912 ASSERT_EQ('x', getc(fp));
913 fclose(fp);
914}
915
Christopher Ferris13f26a72016-01-13 13:47:58 -0800916TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700917 FILE* fp = fopen("/proc/version", "r");
918 ASSERT_TRUE(fp != NULL);
919 ASSERT_EQ(EOF, putc('x', fp));
920 fclose(fp);
921}
Elliott Hughes603332f2014-03-12 17:10:41 -0700922
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700923TEST(STDIO_TEST, sscanf_swscanf) {
924 struct stuff {
925 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800926 int i1, i2;
927 char cs1[3];
928 char s2[3];
929 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700930 double d1;
931 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800932 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700933
934 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800935 EXPECT_STREQ("hello", s1);
936 EXPECT_EQ(123, i1);
937 EXPECT_EQ(456, i2);
938 EXPECT_EQ('a', cs1[0]);
939 EXPECT_EQ('b', cs1[1]);
940 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
941 EXPECT_STREQ("AB", s2); // Terminating NUL.
942 EXPECT_EQ('!', c1);
943 EXPECT_DOUBLE_EQ(1.23, d1);
944 EXPECT_FLOAT_EQ(9.0f, f1);
945 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700946 }
947 } s;
948
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800949 memset(&s, 'x', sizeof(s));
950 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
951 "%s %i%i%2c%[A-Z]%c %lf %f %s",
952 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 -0700953 s.Check();
954
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800955 memset(&s, 'x', sizeof(s));
956 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
957 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
958 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 -0700959 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -0700960}
Elliott Hughes53b24382014-05-02 18:29:25 -0700961
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800962template <typename T>
963static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
964 const T* input, const T* fmt,
965 int expected_count, const char* expected_string) {
966 char buf[256] = {};
967 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
968 ASSERT_STREQ(expected_string, buf) << fmt;
969}
970
971TEST(STDIO_TEST, sscanf_ccl) {
972 // `abc` is just those characters.
973 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
974 // `a-c` is the range 'a' .. 'c'.
975 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
976 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
977 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
978 // `a-c-e` is equivalent to `a-e`.
979 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
980 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
981 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
982 // An initial '^' negates the set.
983 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
984 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
985 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
986 // The first character may be ']' or '-' without being special.
987 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
988 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
989 // The last character may be '-' without being special.
990 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
991 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
992 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
993}
994
995TEST(STDIO_TEST, swscanf_ccl) {
996 // `abc` is just those characters.
997 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
998 // `a-c` is the range 'a' .. 'c'.
999 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1000 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1001 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1002 // `a-c-e` is equivalent to `a-e`.
1003 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1004 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1005 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1006 // An initial '^' negates the set.
1007 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1008 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1009 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1010 // The first character may be ']' or '-' without being special.
1011 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1012 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1013 // The last character may be '-' without being special.
1014 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1015 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1016 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1017}
1018
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001019// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1020TEST(STDIO_TEST, scanf_wscanf_EOF) {
1021 EXPECT_EQ(0, sscanf("b", "ab"));
1022 EXPECT_EQ(EOF, sscanf("", "a"));
1023 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1024 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1025}
1026
1027TEST(STDIO_TEST, scanf_invalid_UTF8) {
1028#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1029 char buf[BUFSIZ];
1030 wchar_t wbuf[BUFSIZ];
1031
1032 memset(buf, 0, sizeof(buf));
1033 memset(wbuf, 0, sizeof(wbuf));
1034 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1035#endif
1036}
1037
1038TEST(STDIO_TEST, scanf_no_match_no_termination) {
1039 char buf[4] = "x";
1040 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1041 EXPECT_EQ('x', buf[0]);
1042 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1043 EXPECT_EQ('x', buf[0]);
1044
1045 wchar_t wbuf[4] = L"x";
1046 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1047 EXPECT_EQ(L'x', wbuf[0]);
1048
1049 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1050 EXPECT_EQ('x', buf[0]);
1051
1052 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1053 EXPECT_EQ(L'x', wbuf[0]);
1054}
1055
1056TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1057#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1058 wchar_t buf[BUFSIZ];
1059
1060 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1061 memset(buf, 0, sizeof(buf));
1062 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1063 EXPECT_EQ(L"x"s, std::wstring(buf));
1064 memset(buf, 0, sizeof(buf));
1065 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1066 EXPECT_EQ(L"x"s, std::wstring(buf));
1067
1068 // Even if scanf has wide characters in a class, they won't match...
1069 // TODO: is that a bug?
1070 memset(buf, 0, sizeof(buf));
1071 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1072 EXPECT_EQ(L"x"s, std::wstring(buf));
1073 // ...unless you use wscanf.
1074 memset(buf, 0, sizeof(buf));
1075 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1076 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1077
1078 // Negation only covers ASCII for scanf...
1079 memset(buf, 0, sizeof(buf));
1080 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1081 EXPECT_EQ(L"x"s, std::wstring(buf));
1082 // ...but covers wide characters for wscanf.
1083 memset(buf, 0, sizeof(buf));
1084 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1085 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1086
1087 // We already determined that non-ASCII characters are ignored in scanf classes.
1088 memset(buf, 0, sizeof(buf));
1089 EXPECT_EQ(1, sscanf("x"
1090 "\xc4\x80" // Matches a byte from each wide char in the class.
1091 "\xc6\x82" // Neither byte is in the class.
1092 "yz",
1093 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1094 EXPECT_EQ(L"x", std::wstring(buf));
1095 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1096 memset(buf, 0, sizeof(buf));
1097 EXPECT_EQ(1, swscanf(L"x"
1098 L"\xc4\x80"
1099 L"\xc6\x82"
1100 L"yz",
1101 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1102 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1103 // not put back together as a wide character.
1104 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1105#endif
1106}
1107
Christopher Ferris13f26a72016-01-13 13:47:58 -08001108TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001109 // If we open a file read-only...
1110 FILE* fp = fopen("/proc/version", "r");
1111
1112 // ...all attempts to write to that file should return failure.
1113
1114 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1115 // glibc gets the wide-character functions wrong.
1116
1117 errno = 0;
1118 EXPECT_EQ(EOF, putc('x', fp));
1119 EXPECT_EQ(EBADF, errno);
1120
1121 errno = 0;
1122 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1123 EXPECT_EQ(EBADF, errno);
1124
1125 errno = 0;
1126 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001127#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001128 EXPECT_EQ(EBADF, errno);
1129#endif
1130
1131 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001132 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1133 EXPECT_EQ(EBADF, errno);
1134
1135 errno = 0;
1136 EXPECT_EQ(EOF, fputs("hello", fp));
1137 EXPECT_EQ(EBADF, errno);
1138
1139 errno = 0;
1140 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001141#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001142 EXPECT_EQ(EBADF, errno);
1143#endif
1144}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001145
1146// Tests that we can only have a consistent and correct fpos_t when using
1147// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001148TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001149 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1150 uselocale(LC_GLOBAL_LOCALE);
1151
1152 FILE* fp = tmpfile();
1153 ASSERT_TRUE(fp != NULL);
1154
1155 wchar_t mb_one_bytes = L'h';
1156 wchar_t mb_two_bytes = 0x00a2;
1157 wchar_t mb_three_bytes = 0x20ac;
1158 wchar_t mb_four_bytes = 0x24b62;
1159
1160 // Write to file.
1161 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1162 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1163 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1164 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1165
1166 rewind(fp);
1167
1168 // Record each character position.
1169 fpos_t pos1;
1170 fpos_t pos2;
1171 fpos_t pos3;
1172 fpos_t pos4;
1173 fpos_t pos5;
1174 EXPECT_EQ(0, fgetpos(fp, &pos1));
1175 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1176 EXPECT_EQ(0, fgetpos(fp, &pos2));
1177 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1178 EXPECT_EQ(0, fgetpos(fp, &pos3));
1179 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1180 EXPECT_EQ(0, fgetpos(fp, &pos4));
1181 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1182 EXPECT_EQ(0, fgetpos(fp, &pos5));
1183
Elliott Hughes063525c2014-05-13 11:19:57 -07001184#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001185 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1186 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1187 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1188 // structure.
1189 ASSERT_EQ(0, static_cast<off_t>(pos1));
1190 ASSERT_EQ(1, static_cast<off_t>(pos2));
1191 ASSERT_EQ(3, static_cast<off_t>(pos3));
1192 ASSERT_EQ(6, static_cast<off_t>(pos4));
1193 ASSERT_EQ(10, static_cast<off_t>(pos5));
1194#endif
1195
1196 // Exercise back and forth movements of the position.
1197 ASSERT_EQ(0, fsetpos(fp, &pos2));
1198 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1199 ASSERT_EQ(0, fsetpos(fp, &pos1));
1200 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1201 ASSERT_EQ(0, fsetpos(fp, &pos4));
1202 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1203 ASSERT_EQ(0, fsetpos(fp, &pos3));
1204 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1205 ASSERT_EQ(0, fsetpos(fp, &pos5));
1206 ASSERT_EQ(WEOF, fgetwc(fp));
1207
1208 fclose(fp);
1209}
1210
1211// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001212TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001213 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1214 uselocale(LC_GLOBAL_LOCALE);
1215
Calin Juravle9b95ea92014-05-14 17:07:10 +01001216 // In glibc-2.16 fseek doesn't work properly in wide mode
1217 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1218 // to close and re-open the file. We do it in order to make the test pass
1219 // with all glibcs.
1220
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001221 TemporaryFile tf;
1222 FILE* fp = fdopen(tf.fd, "w+");
1223 ASSERT_TRUE(fp != NULL);
1224
1225 wchar_t mb_two_bytes = 0x00a2;
1226 wchar_t mb_three_bytes = 0x20ac;
1227 wchar_t mb_four_bytes = 0x24b62;
1228
1229 // Write to file.
1230 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1231 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1232 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1233
1234 fflush(fp);
1235 fclose(fp);
1236
1237 fp = fopen(tf.filename, "r");
1238 ASSERT_TRUE(fp != NULL);
1239
1240 // Store a valid position.
1241 fpos_t mb_two_bytes_pos;
1242 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1243
1244 // Move inside mb_four_bytes with fseek.
1245 long offset_inside_mb = 6;
1246 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1247
1248 // Store the "inside multi byte" position.
1249 fpos_t pos_inside_mb;
1250 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001251#if defined(__BIONIC__)
1252 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1253#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001254
1255 // Reading from within a byte should produce an error.
1256 ASSERT_EQ(WEOF, fgetwc(fp));
1257 ASSERT_EQ(EILSEQ, errno);
1258
1259 // Reverting to a valid position should work.
1260 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1261 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1262
1263 // Moving withing a multi byte with fsetpos should work but reading should
1264 // produce an error.
1265 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1266 ASSERT_EQ(WEOF, fgetwc(fp));
1267 ASSERT_EQ(EILSEQ, errno);
1268
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001269 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001270}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001271
Christopher Ferris13f26a72016-01-13 13:47:58 -08001272TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001273 char buf[16];
1274 memset(buf, 0, sizeof(buf));
1275 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1276 ASSERT_EQ('<', fputc('<', fp));
1277 ASSERT_NE(EOF, fputs("abc>\n", fp));
1278 fflush(fp);
1279
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001280 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001281 ASSERT_STREQ("<abc>\n", buf);
1282
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001283 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001284 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001285 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001286}
1287
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001288TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001289 FILE* fp = fmemopen(nullptr, 128, "r+");
1290 ASSERT_NE(EOF, fputs("xyz\n", fp));
1291
Elliott Hughes70715da2016-08-01 16:35:17 -07001292 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001293 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001294}
1295
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001296TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1297 FILE* fp;
1298 char buf[8];
1299
1300 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1301 // shall be written at the current position or at the end of the buffer,
1302 // depending on the size of the contents."
1303 memset(buf, 'x', sizeof(buf));
1304 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1305 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1306 ASSERT_EQ(0, fflush(fp));
1307 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1308 // Now write and check that the NUL moves along with our writes...
1309 ASSERT_NE(EOF, fputs("hello", fp));
1310 ASSERT_EQ(0, fflush(fp));
1311 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1312 ASSERT_NE(EOF, fputs("wo", fp));
1313 ASSERT_EQ(0, fflush(fp));
1314 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1315 ASSERT_EQ(0, fclose(fp));
1316
1317 // "If a stream open for update is flushed or closed and the last write has
1318 // advanced the current buffer size, a null byte shall be written at the end
1319 // of the buffer if it fits."
1320 memset(buf, 'x', sizeof(buf));
1321 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1322 // Nothing written yet, so no advance...
1323 ASSERT_EQ(0, fflush(fp));
1324 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1325 ASSERT_NE(EOF, fputs("hello", fp));
1326 ASSERT_EQ(0, fclose(fp));
1327}
1328
1329TEST(STDIO_TEST, fmemopen_size) {
1330 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001331 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001332 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001333
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001334 // POSIX: "The stream shall also maintain the size of the current buffer
1335 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1336 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001337
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001338 // "For modes r and r+ the size shall be set to the value given by the size
1339 // argument."
1340 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1341 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1342 EXPECT_EQ(16, ftell(fp));
1343 EXPECT_EQ(16, ftello(fp));
1344 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1345 EXPECT_EQ(16, ftell(fp));
1346 EXPECT_EQ(16, ftello(fp));
1347 ASSERT_EQ(0, fclose(fp));
1348 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1349 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1350 EXPECT_EQ(16, ftell(fp));
1351 EXPECT_EQ(16, ftello(fp));
1352 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1353 EXPECT_EQ(16, ftell(fp));
1354 EXPECT_EQ(16, ftello(fp));
1355 ASSERT_EQ(0, fclose(fp));
1356
1357 // "For modes w and w+ the initial size shall be zero..."
1358 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1359 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1360 EXPECT_EQ(0, ftell(fp));
1361 EXPECT_EQ(0, ftello(fp));
1362 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1363 EXPECT_EQ(0, ftell(fp));
1364 EXPECT_EQ(0, ftello(fp));
1365 ASSERT_EQ(0, fclose(fp));
1366 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1367 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1368 EXPECT_EQ(0, ftell(fp));
1369 EXPECT_EQ(0, ftello(fp));
1370 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1371 EXPECT_EQ(0, ftell(fp));
1372 EXPECT_EQ(0, ftello(fp));
1373 ASSERT_EQ(0, fclose(fp));
1374
1375 // "...and for modes a and a+ the initial size shall be:
1376 // 1. Zero, if buf is a null pointer
1377 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1378 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1379 EXPECT_EQ(0, ftell(fp));
1380 EXPECT_EQ(0, ftello(fp));
1381 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1382 EXPECT_EQ(0, ftell(fp));
1383 EXPECT_EQ(0, ftello(fp));
1384 ASSERT_EQ(0, fclose(fp));
1385 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1386 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1387 EXPECT_EQ(0, ftell(fp));
1388 EXPECT_EQ(0, ftello(fp));
1389 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1390 EXPECT_EQ(0, ftell(fp));
1391 EXPECT_EQ(0, ftello(fp));
1392 ASSERT_EQ(0, fclose(fp));
1393
1394 // 2. The position of the first null byte in the buffer, if one is found
1395 memset(buf, 'x', sizeof(buf));
1396 buf[3] = '\0';
1397 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1398 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1399 EXPECT_EQ(3, ftell(fp));
1400 EXPECT_EQ(3, ftello(fp));
1401 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1402 EXPECT_EQ(3, ftell(fp));
1403 EXPECT_EQ(3, ftello(fp));
1404 ASSERT_EQ(0, fclose(fp));
1405 memset(buf, 'x', sizeof(buf));
1406 buf[3] = '\0';
1407 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1408 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1409 EXPECT_EQ(3, ftell(fp));
1410 EXPECT_EQ(3, ftello(fp));
1411 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1412 EXPECT_EQ(3, ftell(fp));
1413 EXPECT_EQ(3, ftello(fp));
1414 ASSERT_EQ(0, fclose(fp));
1415
1416 // 3. The value of the size argument, if buf is not a null pointer and no
1417 // null byte is found.
1418 memset(buf, 'x', sizeof(buf));
1419 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1420 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1421 EXPECT_EQ(16, ftell(fp));
1422 EXPECT_EQ(16, ftello(fp));
1423 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1424 EXPECT_EQ(16, ftell(fp));
1425 EXPECT_EQ(16, ftello(fp));
1426 ASSERT_EQ(0, fclose(fp));
1427 memset(buf, 'x', sizeof(buf));
1428 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1429 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1430 EXPECT_EQ(16, ftell(fp));
1431 EXPECT_EQ(16, ftello(fp));
1432 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1433 EXPECT_EQ(16, ftell(fp));
1434 EXPECT_EQ(16, ftello(fp));
1435 ASSERT_EQ(0, fclose(fp));
1436}
1437
1438TEST(STDIO_TEST, fmemopen_SEEK_END) {
1439 // fseek SEEK_END is relative to the current string length, not the buffer size.
1440 FILE* fp;
1441 char buf[8];
1442 memset(buf, 'x', sizeof(buf));
1443 strcpy(buf, "str");
1444 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1445 ASSERT_NE(EOF, fputs("string", fp));
1446 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1447 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1448 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1449 EXPECT_EQ(0, fclose(fp));
1450
1451 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1452 // than adding).
1453 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1454 ASSERT_NE(EOF, fputs("54321", fp));
1455 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1456 EXPECT_EQ('2', fgetc(fp));
1457 EXPECT_EQ(0, fclose(fp));
1458}
1459
1460TEST(STDIO_TEST, fmemopen_seek_invalid) {
1461 char buf[8];
1462 memset(buf, 'x', sizeof(buf));
1463 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1464 ASSERT_TRUE(fp != nullptr);
1465
1466 // POSIX: "An attempt to seek ... to a negative position or to a position
1467 // larger than the buffer size given in the size argument shall fail."
1468 // (There's no mention of what errno should be set to, and glibc doesn't
1469 // set errno in any of these cases.)
1470 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1471 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1472 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1473 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1474}
1475
1476TEST(STDIO_TEST, fmemopen_read_EOF) {
1477 // POSIX: "A read operation on the stream shall not advance the current
1478 // buffer position beyond the current buffer size."
1479 char buf[8];
1480 memset(buf, 'x', sizeof(buf));
1481 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1482 ASSERT_TRUE(fp != nullptr);
1483 char buf2[BUFSIZ];
1484 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1485 // POSIX: "Reaching the buffer size in a read operation shall count as
1486 // end-of-file.
1487 ASSERT_TRUE(feof(fp));
1488 ASSERT_EQ(EOF, fgetc(fp));
1489 ASSERT_EQ(0, fclose(fp));
1490}
1491
1492TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1493 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1494 char buf[] = "h\0e\0l\0l\0o";
1495 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1496 ASSERT_TRUE(fp != nullptr);
1497 ASSERT_EQ('h', fgetc(fp));
1498 ASSERT_EQ(0, fgetc(fp));
1499 ASSERT_EQ('e', fgetc(fp));
1500 ASSERT_EQ(0, fgetc(fp));
1501 ASSERT_EQ('l', fgetc(fp));
1502 ASSERT_EQ(0, fgetc(fp));
1503 // POSIX: "The read operation shall start at the current buffer position of
1504 // the stream."
1505 char buf2[8];
1506 memset(buf2, 'x', sizeof(buf2));
1507 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1508 ASSERT_EQ('l', buf2[0]);
1509 ASSERT_EQ(0, buf2[1]);
1510 ASSERT_EQ('o', buf2[2]);
1511 ASSERT_EQ(0, buf2[3]);
1512 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1513 ASSERT_TRUE(feof(fp));
1514 ASSERT_EQ(0, fclose(fp));
1515}
1516
1517TEST(STDIO_TEST, fmemopen_write) {
1518 FILE* fp;
1519 char buf[8];
1520
1521 // POSIX: "A write operation shall start either at the current position of
1522 // the stream (if mode has not specified 'a' as the first character)..."
1523 memset(buf, 'x', sizeof(buf));
1524 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1525 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1526 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1527 ASSERT_EQ(' ', fputc(' ', fp));
1528 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1529 ASSERT_EQ(0, fclose(fp));
1530
1531 // "...or at the current size of the stream (if mode had 'a' as the first
1532 // character)." (See the fmemopen_size test for what "size" means, but for
1533 // mode "a", it's the first NUL byte.)
1534 memset(buf, 'x', sizeof(buf));
1535 buf[3] = '\0';
1536 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1537 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1538 ASSERT_EQ(' ', fputc(' ', fp));
1539 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1540 ASSERT_EQ(0, fclose(fp));
1541
1542 // "If the current position at the end of the write is larger than the
1543 // current buffer size, the current buffer size shall be set to the current
1544 // position." (See the fmemopen_size test for what "size" means, but to
1545 // query it we SEEK_END with offset 0, and then ftell.)
1546 memset(buf, 'x', sizeof(buf));
1547 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1548 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1549 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1550 EXPECT_EQ(0, ftell(fp));
1551 ASSERT_EQ(' ', fputc(' ', fp));
1552 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1553 EXPECT_EQ(1, ftell(fp));
1554 ASSERT_NE(EOF, fputs("123", fp));
1555 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1556 EXPECT_EQ(4, ftell(fp));
1557 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1558 ASSERT_EQ(0, fclose(fp));
1559}
1560
1561TEST(STDIO_TEST, fmemopen_write_EOF) {
1562 // POSIX: "A write operation on the stream shall not advance the current
1563 // buffer size beyond the size given in the size argument."
1564 FILE* fp;
1565
1566 // Scalar writes...
1567 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1568 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1569 ASSERT_EQ('x', fputc('x', fp));
1570 ASSERT_EQ('x', fputc('x', fp));
1571 ASSERT_EQ('x', fputc('x', fp));
1572 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1573 ASSERT_EQ(0, fclose(fp));
1574
1575 // Vector writes...
1576 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1577 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1578 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1579 ASSERT_EQ(0, fclose(fp));
1580}
1581
1582TEST(STDIO_TEST, fmemopen_initial_position) {
1583 // POSIX: "The ... current position in the buffer ... shall be initially
1584 // set to either the beginning of the buffer (for r and w modes) ..."
1585 char buf[] = "hello\0world";
1586 FILE* fp;
1587 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1588 EXPECT_EQ(0L, ftell(fp));
1589 EXPECT_EQ(0, fclose(fp));
1590 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1591 EXPECT_EQ(0L, ftell(fp));
1592 EXPECT_EQ(0, fclose(fp));
1593 buf[0] = 'h'; // (Undo the effects of the above.)
1594
1595 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1596 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1597 EXPECT_EQ(5L, ftell(fp));
1598 EXPECT_EQ(0, fclose(fp));
1599
1600 // POSIX: "If no null byte is found in append mode, the initial position
1601 // shall be set to one byte after the end of the buffer."
1602 memset(buf, 'x', sizeof(buf));
1603 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1604 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1605 EXPECT_EQ(0, fclose(fp));
1606}
1607
1608TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1609 // POSIX: "If buf is a null pointer, the initial position shall always be
1610 // set to the beginning of the buffer."
1611 FILE* fp = fmemopen(nullptr, 128, "a+");
1612 ASSERT_TRUE(fp != nullptr);
1613 EXPECT_EQ(0L, ftell(fp));
1614 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1615 EXPECT_EQ(0, fclose(fp));
1616}
1617
1618TEST(STDIO_TEST, fmemopen_zero_length) {
1619 // POSIX says it's up to the implementation whether or not you can have a
1620 // zero-length buffer (but "A future version of this standard may require
1621 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1622 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1623 FILE* fp;
1624 char buf[16];
1625 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1626 ASSERT_EQ(EOF, fgetc(fp));
1627 ASSERT_TRUE(feof(fp));
1628 ASSERT_EQ(0, fclose(fp));
1629 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1630 ASSERT_EQ(EOF, fgetc(fp));
1631 ASSERT_TRUE(feof(fp));
1632 ASSERT_EQ(0, fclose(fp));
1633
1634 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1635 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1636 ASSERT_EQ(EOF, fputc('x', fp));
1637 ASSERT_EQ(0, fclose(fp));
1638 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1639 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1640 ASSERT_EQ(EOF, fputc('x', fp));
1641 ASSERT_EQ(0, fclose(fp));
1642}
1643
1644TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1645 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1646 // BSD fails, glibc doesn't. We side with the more lenient.
1647 FILE* fp;
1648 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1649 ASSERT_EQ(0, fclose(fp));
1650 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1651 ASSERT_EQ(0, fclose(fp));
1652}
1653
1654TEST(STDIO_TEST, fmemopen_fileno) {
1655 // There's no fd backing an fmemopen FILE*.
1656 FILE* fp = fmemopen(nullptr, 16, "r");
1657 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001658 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001659 ASSERT_EQ(-1, fileno(fp));
1660 ASSERT_EQ(EBADF, errno);
1661 ASSERT_EQ(0, fclose(fp));
1662}
1663
1664TEST(STDIO_TEST, fmemopen_append_after_seek) {
1665 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1666 // there had been an intervening seek.
1667
1668 FILE* fp;
1669 char buf[] = "hello\0world";
1670 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1671 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1672 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1673 ASSERT_NE(EOF, fputc('!', fp));
1674 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1675 ASSERT_EQ(0, fclose(fp));
1676
1677 memcpy(buf, "hello\0world", sizeof(buf));
1678 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1679 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1680 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1681 ASSERT_NE(EOF, fputc('!', fp));
1682 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1683 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001684}
1685
Christopher Ferris13f26a72016-01-13 13:47:58 -08001686TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001687 char* p = nullptr;
1688 size_t size = 0;
1689 FILE* fp = open_memstream(&p, &size);
1690 ASSERT_NE(EOF, fputs("hello, world!", fp));
1691 fclose(fp);
1692
1693 ASSERT_STREQ("hello, world!", p);
1694 ASSERT_EQ(strlen("hello, world!"), size);
1695 free(p);
1696}
1697
Christopher Ferris13f26a72016-01-13 13:47:58 -08001698TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001699#if defined(__BIONIC__)
1700 char* p;
1701 size_t size;
1702
1703 // Invalid buffer.
1704 errno = 0;
1705 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1706 ASSERT_EQ(EINVAL, errno);
1707
1708 // Invalid size.
1709 errno = 0;
1710 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1711 ASSERT_EQ(EINVAL, errno);
1712#else
Elliott Hughes9677fab2016-01-25 15:50:59 -08001713 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001714#endif
1715}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001716
Christopher Ferris13f26a72016-01-13 13:47:58 -08001717TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001718 int fd = open("/proc/version", O_RDONLY);
1719 ASSERT_TRUE(fd != -1);
1720
1721 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001722 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001723
1724 FILE* fp = fdopen(fd, "re");
1725 ASSERT_TRUE(fp != NULL);
1726
1727 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001728 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001729
1730 fclose(fp);
1731 close(fd);
1732}
1733
Christopher Ferris13f26a72016-01-13 13:47:58 -08001734TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001735 FILE* fp = fopen("/proc/version", "r");
1736 ASSERT_TRUE(fp != NULL);
1737
1738 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001739 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001740
1741 fp = freopen("/proc/version", "re", fp);
1742
1743 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001744 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001745
1746 fclose(fp);
1747}
Elliott Hughes20841a12014-12-01 16:13:30 -08001748
Elliott Hughesf226ee52016-02-03 11:24:28 -08001749TEST(STDIO_TEST, fopen64_freopen64) {
1750 FILE* fp = fopen64("/proc/version", "r");
1751 ASSERT_TRUE(fp != nullptr);
1752 fp = freopen64("/proc/version", "re", fp);
1753 ASSERT_TRUE(fp != nullptr);
1754 fclose(fp);
1755}
1756
Elliott Hughes20841a12014-12-01 16:13:30 -08001757// https://code.google.com/p/android/issues/detail?id=81155
1758// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001759TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001760 FILE* fp = fopen("/dev/zero", "r");
1761 ASSERT_TRUE(fp != NULL);
1762
1763 // Make this stream unbuffered.
1764 setvbuf(fp, 0, _IONBF, 0);
1765
1766 char buf[65*1024];
1767 memset(buf, 0xff, sizeof(buf));
1768
1769 time_t t0 = time(NULL);
1770 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001771 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001772 }
1773 time_t t1 = time(NULL);
1774
1775 fclose(fp);
1776
1777 // 1024 64KiB reads should have been very quick.
1778 ASSERT_LE(t1 - t0, 1);
1779
1780 for (size_t i = 0; i < 64*1024; ++i) {
1781 ASSERT_EQ('\0', buf[i]);
1782 }
1783 for (size_t i = 64*1024; i < 65*1024; ++i) {
1784 ASSERT_EQ('\xff', buf[i]);
1785 }
1786}
Elliott Hughes75b99382015-01-20 11:23:50 -08001787
Christopher Ferris13f26a72016-01-13 13:47:58 -08001788TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001789 std::string digits("0123456789");
1790 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001791
1792 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1793 char buf1[4 * 4];
1794 memset(buf1, 0, sizeof(buf1));
1795 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001796 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001797 ASSERT_TRUE(feof(fp));
1798
1799 rewind(fp);
1800
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001801 // Try to read way too much so stdio tries to read more direct from the stream.
1802 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001803 memset(buf2, 0, sizeof(buf2));
1804 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001805 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001806 ASSERT_TRUE(feof(fp));
1807
1808 fclose(fp);
1809}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001810
1811static void test_fread_from_write_only_stream(size_t n) {
1812 FILE* fp = fopen("/dev/null", "w");
1813 std::vector<char> buf(n, 0);
1814 errno = 0;
1815 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
1816 ASSERT_EQ(EBADF, errno);
1817 ASSERT_TRUE(ferror(fp));
1818 ASSERT_FALSE(feof(fp));
1819 fclose(fp);
1820}
1821
Christopher Ferris13f26a72016-01-13 13:47:58 -08001822TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001823 test_fread_from_write_only_stream(1);
1824}
1825
Christopher Ferris13f26a72016-01-13 13:47:58 -08001826TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001827 test_fread_from_write_only_stream(64*1024);
1828}
1829
1830static void test_fwrite_after_fread(size_t n) {
1831 TemporaryFile tf;
1832
1833 FILE* fp = fdopen(tf.fd, "w+");
1834 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1835 fflush(fp);
1836
1837 // We've flushed but not rewound, so there's nothing to read.
1838 std::vector<char> buf(n, 0);
1839 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1840 ASSERT_TRUE(feof(fp));
1841
1842 // But hitting EOF doesn't prevent us from writing...
1843 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08001844 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001845
1846 // And if we rewind, everything's there.
1847 rewind(fp);
1848 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1849 ASSERT_EQ('1', buf[0]);
1850 ASSERT_EQ('2', buf[1]);
1851
1852 fclose(fp);
1853}
1854
Christopher Ferris13f26a72016-01-13 13:47:58 -08001855TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001856 test_fwrite_after_fread(16);
1857}
1858
Christopher Ferris13f26a72016-01-13 13:47:58 -08001859TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001860 test_fwrite_after_fread(64*1024);
1861}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001862
1863// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08001864TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001865 TemporaryFile tf;
1866
1867 FILE* fp = fopen(tf.filename, "w+");
1868 ASSERT_TRUE(fp != nullptr);
1869
1870 char file_data[12288];
1871 for (size_t i = 0; i < 12288; i++) {
1872 file_data[i] = i;
1873 }
1874 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
1875 fclose(fp);
1876
1877 fp = fopen(tf.filename, "r");
1878 ASSERT_TRUE(fp != nullptr);
1879
1880 char buffer[8192];
1881 size_t cur_location = 0;
1882 // Small read to populate internal buffer.
1883 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1884 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1885
1886 cur_location = static_cast<size_t>(ftell(fp));
1887 // Large read to force reading into the user supplied buffer and bypassing
1888 // the internal buffer.
1889 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1890 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1891
1892 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08001893 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08001894 cur_location = static_cast<size_t>(ftell(fp));
1895 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1896 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1897
1898 fclose(fp);
1899}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001900
1901// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08001902TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07001903 TemporaryFile tf;
1904 char buf[6] = {0};
1905
1906 FILE* fw = fopen(tf.filename, "w");
1907 ASSERT_TRUE(fw != nullptr);
1908
1909 FILE* fr = fopen(tf.filename, "r");
1910 ASSERT_TRUE(fr != nullptr);
1911
1912 fwrite("a", 1, 1, fw);
1913 fflush(fw);
1914 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1915 ASSERT_STREQ("a", buf);
1916
1917 // 'fr' is now at EOF.
1918 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1919 ASSERT_TRUE(feof(fr));
1920
1921 // Write some more...
1922 fwrite("z", 1, 1, fw);
1923 fflush(fw);
1924
1925 // ...and check that we can read it back.
1926 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1927 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1928 ASSERT_STREQ("z", buf);
1929
1930 // But now we're done.
1931 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1932
1933 fclose(fr);
1934 fclose(fw);
1935}
Elliott Hughes923f1652016-01-19 15:46:05 -08001936
1937TEST(STDIO_TEST, fclose_invalidates_fd) {
1938 // The typical error we're trying to help people catch involves accessing
1939 // memory after it's been freed. But we know that stdin/stdout/stderr are
1940 // special and don't get deallocated, so this test uses stdin.
1941 ASSERT_EQ(0, fclose(stdin));
1942
1943 // Even though using a FILE* after close is undefined behavior, I've closed
1944 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1945 // especially because they might actually correspond to a real stream.
1946 errno = 0;
1947 ASSERT_EQ(-1, fileno(stdin));
1948 ASSERT_EQ(EBADF, errno);
1949}
Elliott Hughes2704bd12016-01-20 17:14:53 -08001950
1951TEST(STDIO_TEST, fseek_ftell_unseekable) {
1952#if defined(__BIONIC__) // glibc has fopencookie instead.
1953 auto read_fn = [](void*, char*, int) { return -1; };
1954 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1955 ASSERT_TRUE(fp != nullptr);
1956
1957 // Check that ftell balks on an unseekable FILE*.
1958 errno = 0;
1959 ASSERT_EQ(-1, ftell(fp));
1960 ASSERT_EQ(ESPIPE, errno);
1961
1962 // SEEK_CUR is rewritten as SEEK_SET internally...
1963 errno = 0;
1964 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1965 ASSERT_EQ(ESPIPE, errno);
1966
1967 // ...so it's worth testing the direct seek path too.
1968 errno = 0;
1969 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1970 ASSERT_EQ(ESPIPE, errno);
1971
1972 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08001973#else
1974 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1975#endif
1976}
1977
1978TEST(STDIO_TEST, funopen_EINVAL) {
1979#if defined(__BIONIC__)
1980 errno = 0;
1981 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
1982 ASSERT_EQ(EINVAL, errno);
1983#else
1984 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1985#endif
1986}
1987
1988TEST(STDIO_TEST, funopen_seek) {
1989#if defined(__BIONIC__)
1990 auto read_fn = [](void*, char*, int) { return -1; };
1991
1992 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
1993 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
1994
1995 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
1996 ASSERT_TRUE(fp != nullptr);
1997 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08001998#if defined(__LP64__)
1999 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2000 EXPECT_EQ(0xfedcba12LL, pos);
2001#else
2002 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2003 EXPECT_EQ(EOVERFLOW, errno);
2004#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002005
2006 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2007 ASSERT_TRUE(fp64 != nullptr);
2008 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002009 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2010 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002011#else
2012 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002013#endif
2014}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002015
2016TEST(STDIO_TEST, lots_of_concurrent_files) {
2017 std::vector<TemporaryFile*> tfs;
2018 std::vector<FILE*> fps;
2019
2020 for (size_t i = 0; i < 256; ++i) {
2021 TemporaryFile* tf = new TemporaryFile;
2022 tfs.push_back(tf);
2023 FILE* fp = fopen(tf->filename, "w+");
2024 fps.push_back(fp);
2025 fprintf(fp, "hello %zu!\n", i);
2026 fflush(fp);
2027 }
2028
2029 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002030 char expected[BUFSIZ];
2031 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002032
Elliott Hughes70715da2016-08-01 16:35:17 -07002033 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002034 fclose(fps[i]);
2035 delete tfs[i];
2036 }
2037}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002038
2039static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2040 EXPECT_EQ(offset, ftell(fp));
2041 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002042 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002043 fpos_t pos;
2044 fpos64_t pos64;
2045 EXPECT_EQ(0, fgetpos(fp, &pos));
2046 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2047#if defined(__BIONIC__)
2048 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2049 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2050#else
2051 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
2052#endif
2053}
2054
2055TEST(STDIO_TEST, seek_tell_family_smoke) {
2056 TemporaryFile tf;
2057 FILE* fp = fdopen(tf.fd, "w+");
2058
2059 // Initially we should be at 0.
2060 AssertFileOffsetAt(fp, 0);
2061
2062 // Seek to offset 8192.
2063 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2064 AssertFileOffsetAt(fp, 8192);
2065 fpos_t eight_k_pos;
2066 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2067
2068 // Seek forward another 8192...
2069 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2070 AssertFileOffsetAt(fp, 8192 + 8192);
2071 fpos64_t sixteen_k_pos64;
2072 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2073
2074 // Seek back 8192...
2075 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2076 AssertFileOffsetAt(fp, 8192);
2077
2078 // Since we haven't written anything, the end is also at 0.
2079 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2080 AssertFileOffsetAt(fp, 0);
2081
2082 // Check that our fpos64_t from 16KiB works...
2083 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2084 AssertFileOffsetAt(fp, 8192 + 8192);
2085 // ...as does our fpos_t from 8192.
2086 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2087 AssertFileOffsetAt(fp, 8192);
2088
2089 // Do fseeko and fseeko64 work too?
2090 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2091 AssertFileOffsetAt(fp, 1234);
2092 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2093 AssertFileOffsetAt(fp, 5678);
2094
2095 fclose(fp);
2096}
2097
2098TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2099 TemporaryFile tf;
2100 FILE* fp = fdopen(tf.fd, "w+");
2101
2102 // Bad whence.
2103 errno = 0;
2104 ASSERT_EQ(-1, fseek(fp, 0, 123));
2105 ASSERT_EQ(EINVAL, errno);
2106 errno = 0;
2107 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2108 ASSERT_EQ(EINVAL, errno);
2109 errno = 0;
2110 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2111 ASSERT_EQ(EINVAL, errno);
2112
2113 // Bad offset.
2114 errno = 0;
2115 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2116 ASSERT_EQ(EINVAL, errno);
2117 errno = 0;
2118 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2119 ASSERT_EQ(EINVAL, errno);
2120 errno = 0;
2121 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2122 ASSERT_EQ(EINVAL, errno);
2123
2124 fclose(fp);
2125}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002126
2127TEST(STDIO_TEST, ctermid) {
2128 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2129
2130 char buf[L_ctermid] = {};
2131 ASSERT_EQ(buf, ctermid(buf));
2132 ASSERT_STREQ("/dev/tty", buf);
2133}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002134
2135TEST(STDIO_TEST, remove) {
2136 struct stat sb;
2137
2138 TemporaryFile tf;
2139 ASSERT_EQ(0, remove(tf.filename));
2140 ASSERT_EQ(-1, lstat(tf.filename, &sb));
2141 ASSERT_EQ(ENOENT, errno);
2142
2143 TemporaryDir td;
2144 ASSERT_EQ(0, remove(td.dirname));
2145 ASSERT_EQ(-1, lstat(td.dirname, &sb));
2146 ASSERT_EQ(ENOENT, errno);
2147
2148 errno = 0;
2149 ASSERT_EQ(-1, remove(tf.filename));
2150 ASSERT_EQ(ENOENT, errno);
2151
2152 errno = 0;
2153 ASSERT_EQ(-1, remove(td.dirname));
2154 ASSERT_EQ(ENOENT, errno);
2155}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002156
2157TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2158 char buf[16];
2159 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2160 testing::KilledBySignal(SIGABRT),
2161#if defined(NOFORTIFY)
2162 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2163#else
2164 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2165#endif
2166 );
2167}
2168
2169TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2170 std::string buf = "world";
2171 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2172 testing::KilledBySignal(SIGABRT),
2173 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2174}
2175
2176TEST(STDIO_TEST, sprintf_30445072) {
2177 std::string buf = "world";
2178 sprintf(&buf[0], "hello");
2179 ASSERT_EQ(buf, "hello");
2180}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002181
2182TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2183 TemporaryFile tf;
2184 SetFileTo(tf.filename, "0123456789");
2185 FILE* fp = fopen(tf.filename, "a");
2186 EXPECT_EQ(10, ftell(fp));
2187 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2188 EXPECT_EQ(2, ftell(fp));
2189 ASSERT_NE(EOF, fputs("xxx", fp));
2190 ASSERT_EQ(0, fflush(fp));
2191 EXPECT_EQ(13, ftell(fp));
2192 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2193 EXPECT_EQ(13, ftell(fp));
2194 ASSERT_EQ(0, fclose(fp));
2195 AssertFileIs(tf.filename, "0123456789xxx");
2196}
2197
2198TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2199 TemporaryFile tf;
2200 SetFileTo(tf.filename, "0123456789");
2201 int fd = open(tf.filename, O_RDWR);
2202 ASSERT_NE(-1, fd);
2203 // POSIX: "The file position indicator associated with the new stream is set to the position
2204 // indicated by the file offset associated with the file descriptor."
2205 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2206 FILE* fp = fdopen(fd, "a");
2207 EXPECT_EQ(4, ftell(fp));
2208 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2209 EXPECT_EQ(2, ftell(fp));
2210 ASSERT_NE(EOF, fputs("xxx", fp));
2211 ASSERT_EQ(0, fflush(fp));
2212 EXPECT_EQ(13, ftell(fp));
2213 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2214 EXPECT_EQ(13, ftell(fp));
2215 ASSERT_EQ(0, fclose(fp));
2216 AssertFileIs(tf.filename, "0123456789xxx");
2217}
2218
2219TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2220 TemporaryFile tf;
2221 SetFileTo(tf.filename, "0123456789");
2222 FILE* other_fp = fopen("/proc/version", "r");
2223 FILE* fp = freopen(tf.filename, "a", other_fp);
2224 EXPECT_EQ(10, ftell(fp));
2225 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2226 EXPECT_EQ(2, ftell(fp));
2227 ASSERT_NE(EOF, fputs("xxx", fp));
2228 ASSERT_EQ(0, fflush(fp));
2229 EXPECT_EQ(13, ftell(fp));
2230 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2231 EXPECT_EQ(13, ftell(fp));
2232 ASSERT_EQ(0, fclose(fp));
2233 AssertFileIs(tf.filename, "0123456789xxx");
2234}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002235
2236TEST(STDIO_TEST, constants) {
2237 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2238 ASSERT_EQ(L_tmpnam, PATH_MAX);
2239}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002240
2241TEST(STDIO_TEST, perror) {
2242 ExecTestHelper eth;
2243 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2244 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2245 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2246}
2247
2248TEST(STDIO_TEST, puts) {
2249 ExecTestHelper eth;
2250 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2251}
2252
2253TEST(STDIO_TEST, unlocked) {
2254 TemporaryFile tf;
2255
2256 FILE* fp = fopen(tf.filename, "w+");
2257 ASSERT_TRUE(fp != nullptr);
2258
2259 clearerr_unlocked(fp);
2260 ASSERT_FALSE(feof_unlocked(fp));
2261 ASSERT_FALSE(ferror_unlocked(fp));
2262
2263 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2264
2265 ASSERT_NE(EOF, putc_unlocked('a', fp));
2266 ASSERT_NE(EOF, putc('b', fp));
2267 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2268 ASSERT_NE(EOF, fputc('d', fp));
2269
2270 rewind(fp);
2271 ASSERT_EQ('a', getc_unlocked(fp));
2272 ASSERT_EQ('b', getc(fp));
2273 ASSERT_EQ('c', fgetc_unlocked(fp));
2274 ASSERT_EQ('d', fgetc(fp));
2275
2276 rewind(fp);
2277 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2278 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2279 ASSERT_EQ(0, fflush_unlocked(fp));
2280
2281 rewind(fp);
2282 char buf[BUFSIZ] = {};
2283 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2284 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2285 ASSERT_STREQ("ABCD", buf);
2286
2287 rewind(fp);
2288 ASSERT_NE(EOF, fputs("hello ", fp));
2289 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2290 ASSERT_NE(EOF, fputc('\n', fp));
2291
2292 rewind(fp);
2293 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2294 ASSERT_STREQ("hello world\n", buf);
2295
2296 ASSERT_EQ(0, fclose(fp));
2297}
Ryan Prichardbf549862017-11-07 15:30:32 -08002298
2299TEST(STDIO_TEST, fseek_64bit) {
2300 TemporaryFile tf;
2301 FILE* fp = fopen64(tf.filename, "w+");
2302 ASSERT_TRUE(fp != nullptr);
2303 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2304 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2305 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2306 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2307 ASSERT_EQ(0, fclose(fp));
2308}
2309
2310// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2311// isn't representable in long/off_t.
2312TEST(STDIO_TEST, fseek_overflow_32bit) {
2313 TemporaryFile tf;
2314 FILE* fp = fopen64(tf.filename, "w+");
2315 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2316
2317 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2318#if defined(__BIONIC__) && !defined(__LP64__)
2319 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2320 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2321 ASSERT_EQ(EOVERFLOW, errno);
2322#endif
2323
2324 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2325 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2326 // and SEEK_END -- many C libraries check neither.)
2327 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2328 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2329
2330 fclose(fp);
2331}