blob: 01b4dbab72945d537178a5f2823ccb9cc6ed4a20 [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 Hughes31c73092019-05-07 10:03:02 -070022#include <linux/fs.h>
Elliott Hughes7823f322014-04-14 12:11:28 -070023#include <math.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070024#include <stdio.h>
25#include <sys/types.h>
Elliott Hughes468efc82018-07-10 14:39:49 -070026#include <sys/socket.h>
Elliott Hughes91875dc2012-09-24 17:55:15 -070027#include <sys/stat.h>
28#include <unistd.h>
Elliott Hughes05493712014-04-17 17:30:03 -070029#include <wchar.h>
Calin Juravle03e4ebe2014-05-08 14:42:06 +010030#include <locale.h>
31
Elliott Hughes3a4c4542017-07-19 17:20:24 -070032#include <string>
Ryan Prichardc485cdb2019-04-30 14:47:34 -070033#include <thread>
Elliott Hughese6bb5a22015-01-23 17:48:15 -080034#include <vector>
35
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -080036#include <android-base/file.h>
37
Elliott Hughesfb3873d2016-08-10 11:07:54 -070038#include "BionicDeathTest.h"
Josh Gao2f06e102017-01-10 13:00:37 -080039#include "utils.h"
Elliott Hughes91875dc2012-09-24 17:55:15 -070040
Christopher Ferris13f26a72016-01-13 13:47:58 -080041#if defined(NOFORTIFY)
42#define STDIO_TEST stdio_nofortify
Elliott Hughesfb3873d2016-08-10 11:07:54 -070043#define STDIO_DEATHTEST stdio_nofortify_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080044#else
45#define STDIO_TEST stdio
Elliott Hughesfb3873d2016-08-10 11:07:54 -070046#define STDIO_DEATHTEST stdio_DeathTest
Christopher Ferris13f26a72016-01-13 13:47:58 -080047#endif
48
Elliott Hughes3a4c4542017-07-19 17:20:24 -070049using namespace std::string_literals;
50
Elliott Hughesfb3873d2016-08-10 11:07:54 -070051class stdio_DeathTest : public BionicDeathTest {};
52class stdio_nofortify_DeathTest : public BionicDeathTest {};
53
Elliott Hughes33a8cb12017-07-25 18:06:46 -070054static void SetFileTo(const char* path, const char* content) {
55 FILE* fp;
56 ASSERT_NE(nullptr, fp = fopen(path, "w"));
57 ASSERT_NE(EOF, fputs(content, fp));
58 ASSERT_EQ(0, fclose(fp));
59}
60
61static void AssertFileIs(const char* path, const char* expected) {
62 FILE* fp;
63 ASSERT_NE(nullptr, fp = fopen(path, "r"));
64 char* line = nullptr;
65 size_t length;
66 ASSERT_NE(EOF, getline(&line, &length, fp));
67 ASSERT_EQ(0, fclose(fp));
68 ASSERT_STREQ(expected, line);
69 free(line);
70}
71
Elliott Hughes70715da2016-08-01 16:35:17 -070072static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
73 rewind(fp);
74
75 char line[1024];
Josh Gao2f06e102017-01-10 13:00:37 -080076 memset(line, 0xff, sizeof(line));
Elliott Hughes70715da2016-08-01 16:35:17 -070077 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
78 ASSERT_STREQ(expected, line);
79
80 if (is_fmemopen) {
81 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
82 // extra empty line, but does on every C library I tested...
83 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
84 ASSERT_STREQ("", line);
85 }
86
87 // Make sure there isn't anything else in the file.
88 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
89}
90
Christopher Ferris13f26a72016-01-13 13:47:58 -080091TEST(STDIO_TEST, flockfile_18208568_stderr) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080092 // Check that we have a _recursive_ mutex for flockfile.
93 flockfile(stderr);
94 feof(stderr); // We don't care about the result, but this needs to take the lock.
95 funlockfile(stderr);
96}
97
Christopher Ferris13f26a72016-01-13 13:47:58 -080098TEST(STDIO_TEST, flockfile_18208568_regular) {
Elliott Hughes6a03abc2014-11-03 12:32:17 -080099 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
100 FILE* fp = fopen("/dev/null", "w");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700101 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6a03abc2014-11-03 12:32:17 -0800102 flockfile(fp);
103 feof(fp);
104 funlockfile(fp);
105 fclose(fp);
106}
107
Christopher Ferris13f26a72016-01-13 13:47:58 -0800108TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
Elliott Hughes91875dc2012-09-24 17:55:15 -0700109 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700110 ASSERT_TRUE(fp != nullptr);
Elliott Hughes91875dc2012-09-24 17:55:15 -0700111
112 int fd = fileno(fp);
113 ASSERT_NE(fd, -1);
114
115 struct stat sb;
116 int rc = fstat(fd, &sb);
117 ASSERT_NE(rc, -1);
118 ASSERT_EQ(sb.st_mode & 0777, 0600U);
119
120 rc = fprintf(fp, "hello\n");
121 ASSERT_EQ(rc, 6);
122
Elliott Hughes70715da2016-08-01 16:35:17 -0700123 AssertFileIs(fp, "hello\n");
Elliott Hughes91875dc2012-09-24 17:55:15 -0700124 fclose(fp);
125}
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300126
Elliott Hughesf226ee52016-02-03 11:24:28 -0800127TEST(STDIO_TEST, tmpfile64) {
128 FILE* fp = tmpfile64();
129 ASSERT_TRUE(fp != nullptr);
130 fclose(fp);
131}
132
Christopher Ferris13f26a72016-01-13 13:47:58 -0800133TEST(STDIO_TEST, dprintf) {
Calin Juravle6afb2a92014-05-22 11:47:47 +0100134 TemporaryFile tf;
135
136 int rc = dprintf(tf.fd, "hello\n");
137 ASSERT_EQ(rc, 6);
138
Yabin Cui5ca4a9e2014-11-06 19:55:09 -0800139 lseek(tf.fd, 0, SEEK_SET);
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700140 FILE* tfile = fdopen(tf.fd, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700141 ASSERT_TRUE(tfile != nullptr);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100142
Elliott Hughes70715da2016-08-01 16:35:17 -0700143 AssertFileIs(tfile, "hello\n");
Christopher Ferris9e01ea62014-05-29 12:49:35 -0700144 fclose(tfile);
Calin Juravle6afb2a92014-05-22 11:47:47 +0100145}
146
Christopher Ferris13f26a72016-01-13 13:47:58 -0800147TEST(STDIO_TEST, getdelim) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300148 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700149 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300150
151 const char* line_written = "This is a test";
152 int rc = fprintf(fp, "%s", line_written);
153 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
154
155 rewind(fp);
156
Yi Kong32bc0fc2018-08-02 17:31:13 -0700157 char* word_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300158 size_t allocated_length = 0;
159
160 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
161 for (size_t i = 0; i < 5; ++i) {
162 ASSERT_FALSE(feof(fp));
163 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
164 ASSERT_GE(allocated_length, strlen(expected[i]));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800165 ASSERT_STREQ(expected[i], word_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300166 }
167 // The last read should have set the end-of-file indicator for the stream.
168 ASSERT_TRUE(feof(fp));
169 clearerr(fp);
170
171 // getdelim returns -1 but doesn't set errno if we're already at EOF.
172 // It should set the end-of-file indicator for the stream, though.
173 errno = 0;
174 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800175 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300176 ASSERT_TRUE(feof(fp));
177
178 free(word_read);
179 fclose(fp);
180}
181
Christopher Ferris13f26a72016-01-13 13:47:58 -0800182TEST(STDIO_TEST, getdelim_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300183 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700184 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300185
Yi Kong32bc0fc2018-08-02 17:31:13 -0700186 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300187 size_t buffer_length = 0;
188
189 // The first argument can't be NULL.
190 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700191 ASSERT_EQ(getdelim(nullptr, &buffer_length, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800192 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300193
194 // The second argument can't be NULL.
195 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700196 ASSERT_EQ(getdelim(&buffer, nullptr, ' ', fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800197 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700198 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300199}
200
Christopher Ferris13f26a72016-01-13 13:47:58 -0800201TEST(STDIO_TEST, getdelim_directory) {
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700202 FILE* fp = fopen("/proc", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700203 ASSERT_TRUE(fp != nullptr);
Elliott Hughes694fd2d2015-04-05 10:51:56 -0700204 char* word_read;
205 size_t allocated_length;
206 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
207 fclose(fp);
208}
209
Christopher Ferris13f26a72016-01-13 13:47:58 -0800210TEST(STDIO_TEST, getline) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300211 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700212 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300213
214 const char* line_written = "This is a test for getline\n";
215 const size_t line_count = 5;
216
217 for (size_t i = 0; i < line_count; ++i) {
218 int rc = fprintf(fp, "%s", line_written);
219 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
220 }
221
222 rewind(fp);
223
Yi Kong32bc0fc2018-08-02 17:31:13 -0700224 char* line_read = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300225 size_t allocated_length = 0;
226
227 size_t read_line_count = 0;
228 ssize_t read_char_count;
229 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
230 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
231 ASSERT_GE(allocated_length, strlen(line_written));
Elliott Hughes0ed7e082015-01-22 15:13:38 -0800232 ASSERT_STREQ(line_written, line_read);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300233 ++read_line_count;
234 }
235 ASSERT_EQ(read_line_count, line_count);
236
237 // The last read should have set the end-of-file indicator for the stream.
238 ASSERT_TRUE(feof(fp));
239 clearerr(fp);
240
241 // getline returns -1 but doesn't set errno if we're already at EOF.
242 // It should set the end-of-file indicator for the stream, though.
243 errno = 0;
244 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800245 ASSERT_EQ(0, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300246 ASSERT_TRUE(feof(fp));
247
248 free(line_read);
249 fclose(fp);
250}
251
Christopher Ferris13f26a72016-01-13 13:47:58 -0800252TEST(STDIO_TEST, getline_invalid) {
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300253 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -0700254 ASSERT_TRUE(fp != nullptr);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300255
Yi Kong32bc0fc2018-08-02 17:31:13 -0700256 char* buffer = nullptr;
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300257 size_t buffer_length = 0;
258
259 // The first argument can't be NULL.
260 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700261 ASSERT_EQ(getline(nullptr, &buffer_length, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800262 ASSERT_EQ(EINVAL, errno);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300263
264 // The second argument can't be NULL.
265 errno = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700266 ASSERT_EQ(getline(&buffer, nullptr, fp), -1);
Elliott Hughes5e3fc432013-02-11 16:36:48 -0800267 ASSERT_EQ(EINVAL, errno);
Elliott Hughes20f8aec2014-05-12 15:15:37 -0700268 fclose(fp);
Irina Tirdeaeac9eb42012-09-08 09:28:30 +0300269}
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000270
Christopher Ferris13f26a72016-01-13 13:47:58 -0800271TEST(STDIO_TEST, printf_ssize_t) {
Elliott Hughese2556422013-02-28 10:51:14 -0800272 // http://b/8253769
Elliott Hughese2556422013-02-28 10:51:14 -0800273 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
Elliott Hughesb6e22482013-03-08 15:28:52 -0800274 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
275 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
Thorsten Glaserc641caf2013-02-17 16:50:58 +0000276 // error: format '%zd' expects argument of type 'signed size_t',
277 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
278 ssize_t v = 1;
279 char buf[32];
280 snprintf(buf, sizeof(buf), "%zd", v);
281}
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800282
Elliott Hughes05493712014-04-17 17:30:03 -0700283// https://code.google.com/p/android/issues/detail?id=64886
Christopher Ferris13f26a72016-01-13 13:47:58 -0800284TEST(STDIO_TEST, snprintf_a) {
Elliott Hughes05493712014-04-17 17:30:03 -0700285 char buf[BUFSIZ];
286 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
287 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
288}
289
Christopher Ferris13f26a72016-01-13 13:47:58 -0800290TEST(STDIO_TEST, snprintf_lc) {
Elliott Hughes05493712014-04-17 17:30:03 -0700291 char buf[BUFSIZ];
292 wint_t wc = L'a';
293 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
294 EXPECT_STREQ("<a>", buf);
295}
296
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700297TEST(STDIO_TEST, snprintf_C) { // Synonym for %lc.
298 char buf[BUFSIZ];
299 wchar_t wc = L'a';
300 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%C>", wc));
301 EXPECT_STREQ("<a>", buf);
302}
303
Christopher Ferris13f26a72016-01-13 13:47:58 -0800304TEST(STDIO_TEST, snprintf_ls) {
Elliott Hughes05493712014-04-17 17:30:03 -0700305 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700306 wchar_t* ws = nullptr;
Elliott Hughes05493712014-04-17 17:30:03 -0700307 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
308 EXPECT_STREQ("<(null)>", buf);
309
310 wchar_t chars[] = { L'h', L'i', 0 };
311 ws = chars;
312 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
313 EXPECT_STREQ("<hi>", buf);
314}
315
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700316TEST(STDIO_TEST, snprintf_S) { // Synonym for %ls.
317 char buf[BUFSIZ];
Yi Kong32bc0fc2018-08-02 17:31:13 -0700318 wchar_t* ws = nullptr;
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700319 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%S>", ws));
320 EXPECT_STREQ("<(null)>", buf);
321
322 wchar_t chars[] = { L'h', L'i', 0 };
323 ws = chars;
324 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%S>", ws));
325 EXPECT_STREQ("<hi>", buf);
326}
327
Christopher Ferris13f26a72016-01-13 13:47:58 -0800328TEST(STDIO_TEST, snprintf_n) {
Elliott Hughes063525c2014-05-13 11:19:57 -0700329#if defined(__BIONIC__)
Elliott Hughes41398d02018-03-07 13:32:58 -0800330 // http://b/14492135 and http://b/31832608.
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700331 char buf[32];
Elliott Hughese2341d02014-05-02 18:16:32 -0700332 int i = 1234;
Elliott Hughes41398d02018-03-07 13:32:58 -0800333 EXPECT_DEATH(snprintf(buf, sizeof(buf), "a %n b", &i), "%n not allowed on Android");
Elliott Hughese2341d02014-05-02 18:16:32 -0700334#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800335 GTEST_SKIP() << "glibc does allow %n";
Elliott Hughese2341d02014-05-02 18:16:32 -0700336#endif
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700337}
Elliott Hughes7248a2d2013-09-24 18:01:33 -0700338
Christopher Ferris13f26a72016-01-13 13:47:58 -0800339TEST(STDIO_TEST, snprintf_smoke) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700340 char buf[BUFSIZ];
341
342 snprintf(buf, sizeof(buf), "a");
343 EXPECT_STREQ("a", buf);
344
345 snprintf(buf, sizeof(buf), "%%");
346 EXPECT_STREQ("%", buf);
347
348 snprintf(buf, sizeof(buf), "01234");
349 EXPECT_STREQ("01234", buf);
350
351 snprintf(buf, sizeof(buf), "a%sb", "01234");
352 EXPECT_STREQ("a01234b", buf);
353
Yi Kong32bc0fc2018-08-02 17:31:13 -0700354 char* s = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700355 snprintf(buf, sizeof(buf), "a%sb", s);
356 EXPECT_STREQ("a(null)b", buf);
357
358 snprintf(buf, sizeof(buf), "aa%scc", "bb");
359 EXPECT_STREQ("aabbcc", buf);
360
361 snprintf(buf, sizeof(buf), "a%cc", 'b');
362 EXPECT_STREQ("abc", buf);
363
364 snprintf(buf, sizeof(buf), "a%db", 1234);
365 EXPECT_STREQ("a1234b", buf);
366
367 snprintf(buf, sizeof(buf), "a%db", -8123);
368 EXPECT_STREQ("a-8123b", buf);
369
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700370 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700371 EXPECT_STREQ("a16b", buf);
372
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700373 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
Elliott Hughes1d13c642013-09-23 16:02:39 -0700374 EXPECT_STREQ("a16b", buf);
375
376 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
377 EXPECT_STREQ("a68719476736b", buf);
378
379 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
380 EXPECT_STREQ("a70000b", buf);
381
382 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
383 EXPECT_STREQ("a0xb0001234b", buf);
384
385 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
386 EXPECT_STREQ("a12abz", buf);
387
388 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
389 EXPECT_STREQ("a12ABz", buf);
390
391 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
392 EXPECT_STREQ("a00123456z", buf);
393
394 snprintf(buf, sizeof(buf), "a%5dz", 1234);
395 EXPECT_STREQ("a 1234z", buf);
396
397 snprintf(buf, sizeof(buf), "a%05dz", 1234);
398 EXPECT_STREQ("a01234z", buf);
399
400 snprintf(buf, sizeof(buf), "a%8dz", 1234);
401 EXPECT_STREQ("a 1234z", buf);
402
403 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
404 EXPECT_STREQ("a1234 z", buf);
405
406 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
407 EXPECT_STREQ("Aabcdef Z", buf);
408
409 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
410 EXPECT_STREQ("Ahello:1234Z", buf);
411
412 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
413 EXPECT_STREQ("a005:5:05z", buf);
414
Yi Kong32bc0fc2018-08-02 17:31:13 -0700415 void* p = nullptr;
Elliott Hughes1d13c642013-09-23 16:02:39 -0700416 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
Christopher Ferris13613132013-10-28 15:24:04 -0700417#if defined(__BIONIC__)
Elliott Hughes1d13c642013-09-23 16:02:39 -0700418 EXPECT_STREQ("a5,0x0z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800419#else // __BIONIC__
Christopher Ferris13613132013-10-28 15:24:04 -0700420 EXPECT_STREQ("a5,(nil)z", buf);
Christopher Ferrisf04935c2013-12-20 18:43:21 -0800421#endif // __BIONIC__
Elliott Hughes1d13c642013-09-23 16:02:39 -0700422
423 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
424 EXPECT_STREQ("a68719476736,6,7,8z", buf);
425
426 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
427 EXPECT_STREQ("a_1.230000_b", buf);
428
Stephen Hines6c7b3cb2013-10-11 16:03:21 -0700429 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700430 EXPECT_STREQ("a_3.14_b", buf);
Alexander Ivchenkoedd7c2e2014-04-01 17:01:39 +0400431
432 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
433 EXPECT_STREQ("print_me_twice print_me_twice", buf);
Elliott Hughes1d13c642013-09-23 16:02:39 -0700434}
435
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800436template <typename T>
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700437static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
438 int sscanf_fn(const T*, const T*, ...),
439 const T* fmt_string, const T* fmt, const T* fmt_plus,
440 const T* minus_inf, const T* inf_, const T* plus_inf,
441 const T* minus_nan, const T* nan_, const T* plus_nan) {
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800442 T buf[BUFSIZ];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700443 float f;
Elliott Hughes7823f322014-04-14 12:11:28 -0700444
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700445 // NaN.
446
447 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800448 EXPECT_STREQ(nan_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700449 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
450 EXPECT_TRUE(isnan(f));
451
452 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800453 EXPECT_STREQ(minus_nan, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700454 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
455 EXPECT_TRUE(isnan(f));
456
457 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800458 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
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_plus, -nanf(""));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800463 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700464 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
465 EXPECT_TRUE(isnan(f));
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800466
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700467 // Inf.
468
469 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800470 EXPECT_STREQ(inf_, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700471 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
472 EXPECT_EQ(HUGE_VALF, f);
473
474 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800475 EXPECT_STREQ(minus_inf, buf) << fmt;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700476 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
477 EXPECT_EQ(-HUGE_VALF, f);
478
479 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800480 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
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_plus, -HUGE_VALF);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800485 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700486 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
487 EXPECT_EQ(-HUGE_VALF, f);
488
489 // Check case-insensitivity.
490 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
491 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
492 EXPECT_EQ(HUGE_VALF, f);
493 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
494 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
495 EXPECT_TRUE(isnan(f));
Elliott Hughes7823f322014-04-14 12:11:28 -0700496}
497
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700498TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
499 CheckInfNan(snprintf, sscanf, "%s",
500 "[%a]", "[%+a]",
501 "[-inf]", "[inf]", "[+inf]",
502 "[-nan]", "[nan]", "[+nan]");
503 CheckInfNan(snprintf, sscanf, "%s",
504 "[%A]", "[%+A]",
505 "[-INF]", "[INF]", "[+INF]",
506 "[-NAN]", "[NAN]", "[+NAN]");
507 CheckInfNan(snprintf, sscanf, "%s",
508 "[%e]", "[%+e]",
509 "[-inf]", "[inf]", "[+inf]",
510 "[-nan]", "[nan]", "[+nan]");
511 CheckInfNan(snprintf, sscanf, "%s",
512 "[%E]", "[%+E]",
513 "[-INF]", "[INF]", "[+INF]",
514 "[-NAN]", "[NAN]", "[+NAN]");
515 CheckInfNan(snprintf, sscanf, "%s",
516 "[%f]", "[%+f]",
517 "[-inf]", "[inf]", "[+inf]",
518 "[-nan]", "[nan]", "[+nan]");
519 CheckInfNan(snprintf, sscanf, "%s",
520 "[%F]", "[%+F]",
521 "[-INF]", "[INF]", "[+INF]",
522 "[-NAN]", "[NAN]", "[+NAN]");
523 CheckInfNan(snprintf, sscanf, "%s",
524 "[%g]", "[%+g]",
525 "[-inf]", "[inf]", "[+inf]",
526 "[-nan]", "[nan]", "[+nan]");
527 CheckInfNan(snprintf, sscanf, "%s",
528 "[%G]", "[%+G]",
529 "[-INF]", "[INF]", "[+INF]",
530 "[-NAN]", "[NAN]", "[+NAN]");
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800531}
Elliott Hughes7823f322014-04-14 12:11:28 -0700532
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700533TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
534 CheckInfNan(swprintf, swscanf, L"%s",
535 L"[%a]", L"[%+a]",
536 L"[-inf]", L"[inf]", L"[+inf]",
537 L"[-nan]", L"[nan]", L"[+nan]");
538 CheckInfNan(swprintf, swscanf, L"%s",
539 L"[%A]", L"[%+A]",
540 L"[-INF]", L"[INF]", L"[+INF]",
541 L"[-NAN]", L"[NAN]", L"[+NAN]");
542 CheckInfNan(swprintf, swscanf, L"%s",
543 L"[%e]", L"[%+e]",
544 L"[-inf]", L"[inf]", L"[+inf]",
545 L"[-nan]", L"[nan]", L"[+nan]");
546 CheckInfNan(swprintf, swscanf, L"%s",
547 L"[%E]", L"[%+E]",
548 L"[-INF]", L"[INF]", L"[+INF]",
549 L"[-NAN]", L"[NAN]", L"[+NAN]");
550 CheckInfNan(swprintf, swscanf, L"%s",
551 L"[%f]", L"[%+f]",
552 L"[-inf]", L"[inf]", L"[+inf]",
553 L"[-nan]", L"[nan]", L"[+nan]");
554 CheckInfNan(swprintf, swscanf, L"%s",
555 L"[%F]", L"[%+F]",
556 L"[-INF]", L"[INF]", L"[+INF]",
557 L"[-NAN]", L"[NAN]", L"[+NAN]");
558 CheckInfNan(swprintf, swscanf, L"%s",
559 L"[%g]", L"[%+g]",
560 L"[-inf]", L"[inf]", L"[+inf]",
561 L"[-nan]", L"[nan]", L"[+nan]");
562 CheckInfNan(swprintf, swscanf, L"%s",
563 L"[%G]", L"[%+G]",
564 L"[-INF]", L"[INF]", L"[+INF]",
565 L"[-NAN]", L"[NAN]", L"[+NAN]");
Elliott Hughes7823f322014-04-14 12:11:28 -0700566}
567
Dan Albert9601f162017-08-09 14:59:06 -0700568TEST(STDIO_TEST, swprintf) {
569 constexpr size_t nchars = 32;
570 wchar_t buf[nchars];
571
572 ASSERT_EQ(2, swprintf(buf, nchars, L"ab")) << strerror(errno);
573 ASSERT_EQ(std::wstring(L"ab"), buf);
574 ASSERT_EQ(5, swprintf(buf, nchars, L"%s", "abcde"));
575 ASSERT_EQ(std::wstring(L"abcde"), buf);
576
577 // Unlike swprintf(), swprintf() returns -1 in case of truncation
578 // and doesn't necessarily zero-terminate the output!
579 ASSERT_EQ(-1, swprintf(buf, 4, L"%s", "abcde"));
580
581 const char kString[] = "Hello, World";
582 ASSERT_EQ(12, swprintf(buf, nchars, L"%s", kString));
583 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
584 ASSERT_EQ(12, swprintf(buf, 13, L"%s", kString));
585 ASSERT_EQ(std::wstring(L"Hello, World"), buf);
586}
587
588TEST(STDIO_TEST, swprintf_a) {
589 constexpr size_t nchars = 32;
590 wchar_t buf[nchars];
591
592 ASSERT_EQ(20, swprintf(buf, nchars, L"%a", 3.1415926535));
593 ASSERT_EQ(std::wstring(L"0x1.921fb54411744p+1"), buf);
594}
595
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700596TEST(STDIO_TEST, swprintf_lc) {
597 constexpr size_t nchars = 32;
598 wchar_t buf[nchars];
599
600 wint_t wc = L'a';
601 EXPECT_EQ(3, swprintf(buf, nchars, L"<%lc>", wc));
602 EXPECT_EQ(std::wstring(L"<a>"), buf);
603}
604
605TEST(STDIO_TEST, swprintf_C) { // Synonym for %lc.
606 constexpr size_t nchars = 32;
607 wchar_t buf[nchars];
608
609 wint_t wc = L'a';
610 EXPECT_EQ(3, swprintf(buf, nchars, L"<%C>", wc));
611 EXPECT_EQ(std::wstring(L"<a>"), buf);
612}
613
Elliott Hughes618303c2017-11-02 16:58:44 -0700614TEST(STDIO_TEST, swprintf_jd_INTMAX_MAX) {
615 constexpr size_t nchars = 32;
616 wchar_t buf[nchars];
617
618 swprintf(buf, nchars, L"%jd", INTMAX_MAX);
619 EXPECT_EQ(std::wstring(L"9223372036854775807"), buf);
620}
621
622TEST(STDIO_TEST, swprintf_jd_INTMAX_MIN) {
623 constexpr size_t nchars = 32;
624 wchar_t buf[nchars];
625
626 swprintf(buf, nchars, L"%jd", INTMAX_MIN);
627 EXPECT_EQ(std::wstring(L"-9223372036854775808"), buf);
628}
629
630TEST(STDIO_TEST, swprintf_ju_UINTMAX_MAX) {
631 constexpr size_t nchars = 32;
632 wchar_t buf[nchars];
633
634 swprintf(buf, nchars, L"%ju", UINTMAX_MAX);
635 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
636}
637
638TEST(STDIO_TEST, swprintf_1$ju_UINTMAX_MAX) {
639 constexpr size_t nchars = 32;
640 wchar_t buf[nchars];
641
642 swprintf(buf, nchars, L"%1$ju", UINTMAX_MAX);
643 EXPECT_EQ(std::wstring(L"18446744073709551615"), buf);
644}
645
Dan Albert9601f162017-08-09 14:59:06 -0700646TEST(STDIO_TEST, swprintf_ls) {
647 constexpr size_t nchars = 32;
648 wchar_t buf[nchars];
649
650 static const wchar_t kWideString[] = L"Hello\uff41 World";
651 ASSERT_EQ(12, swprintf(buf, nchars, L"%ls", kWideString));
652 ASSERT_EQ(std::wstring(kWideString), buf);
653 ASSERT_EQ(12, swprintf(buf, 13, L"%ls", kWideString));
654 ASSERT_EQ(std::wstring(kWideString), buf);
655}
656
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700657TEST(STDIO_TEST, swprintf_S) { // Synonym for %ls.
658 constexpr size_t nchars = 32;
659 wchar_t buf[nchars];
660
661 static const wchar_t kWideString[] = L"Hello\uff41 World";
662 ASSERT_EQ(12, swprintf(buf, nchars, L"%S", kWideString));
663 ASSERT_EQ(std::wstring(kWideString), buf);
664 ASSERT_EQ(12, swprintf(buf, 13, L"%S", kWideString));
665 ASSERT_EQ(std::wstring(kWideString), buf);
666}
667
Christopher Ferris13f26a72016-01-13 13:47:58 -0800668TEST(STDIO_TEST, snprintf_d_INT_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700669 char buf[BUFSIZ];
670 snprintf(buf, sizeof(buf), "%d", INT_MAX);
671 EXPECT_STREQ("2147483647", buf);
672}
673
Christopher Ferris13f26a72016-01-13 13:47:58 -0800674TEST(STDIO_TEST, snprintf_d_INT_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700675 char buf[BUFSIZ];
676 snprintf(buf, sizeof(buf), "%d", INT_MIN);
677 EXPECT_STREQ("-2147483648", buf);
678}
679
Elliott Hughes618303c2017-11-02 16:58:44 -0700680TEST(STDIO_TEST, snprintf_jd_INTMAX_MAX) {
681 char buf[BUFSIZ];
682 snprintf(buf, sizeof(buf), "%jd", INTMAX_MAX);
683 EXPECT_STREQ("9223372036854775807", buf);
684}
685
686TEST(STDIO_TEST, snprintf_jd_INTMAX_MIN) {
687 char buf[BUFSIZ];
688 snprintf(buf, sizeof(buf), "%jd", INTMAX_MIN);
689 EXPECT_STREQ("-9223372036854775808", buf);
690}
691
692TEST(STDIO_TEST, snprintf_ju_UINTMAX_MAX) {
693 char buf[BUFSIZ];
694 snprintf(buf, sizeof(buf), "%ju", UINTMAX_MAX);
695 EXPECT_STREQ("18446744073709551615", buf);
696}
697
698TEST(STDIO_TEST, snprintf_1$ju_UINTMAX_MAX) {
699 char buf[BUFSIZ];
700 snprintf(buf, sizeof(buf), "%1$ju", UINTMAX_MAX);
701 EXPECT_STREQ("18446744073709551615", buf);
702}
703
Christopher Ferris13f26a72016-01-13 13:47:58 -0800704TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700705 char buf[BUFSIZ];
706 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
Josh Gaob36efa42016-09-15 13:55:41 -0700707#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700708 EXPECT_STREQ("9223372036854775807", buf);
709#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700710 EXPECT_STREQ("2147483647", buf);
Elliott Hughes925753a2013-10-18 13:17:18 -0700711#endif
Elliott Hughes1d13c642013-09-23 16:02:39 -0700712}
713
Christopher Ferris13f26a72016-01-13 13:47:58 -0800714TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700715 char buf[BUFSIZ];
716 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
Josh Gaob36efa42016-09-15 13:55:41 -0700717#if defined(__LP64__)
Elliott Hughes925753a2013-10-18 13:17:18 -0700718 EXPECT_STREQ("-9223372036854775808", buf);
719#else
Elliott Hughes1d13c642013-09-23 16:02:39 -0700720 EXPECT_STREQ("-2147483648", 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_lld_LLONG_MAX) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700725 char buf[BUFSIZ];
726 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
727 EXPECT_STREQ("9223372036854775807", buf);
728}
729
Christopher Ferris13f26a72016-01-13 13:47:58 -0800730TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
Elliott Hughes1d13c642013-09-23 16:02:39 -0700731 char buf[BUFSIZ];
732 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
733 EXPECT_STREQ("-9223372036854775808", buf);
734}
735
Elliott Hughes2f9c8ce2017-11-01 13:54:47 -0700736TEST(STDIO_TEST, snprintf_o_UINT_MAX) {
737 char buf[BUFSIZ];
738 snprintf(buf, sizeof(buf), "%o", UINT_MAX);
739 EXPECT_STREQ("37777777777", buf);
740}
741
742TEST(STDIO_TEST, snprintf_u_UINT_MAX) {
743 char buf[BUFSIZ];
744 snprintf(buf, sizeof(buf), "%u", UINT_MAX);
745 EXPECT_STREQ("4294967295", buf);
746}
747
748TEST(STDIO_TEST, snprintf_x_UINT_MAX) {
749 char buf[BUFSIZ];
750 snprintf(buf, sizeof(buf), "%x", UINT_MAX);
751 EXPECT_STREQ("ffffffff", buf);
752}
753
754TEST(STDIO_TEST, snprintf_X_UINT_MAX) {
755 char buf[BUFSIZ];
756 snprintf(buf, sizeof(buf), "%X", UINT_MAX);
757 EXPECT_STREQ("FFFFFFFF", buf);
758}
759
Christopher Ferris13f26a72016-01-13 13:47:58 -0800760TEST(STDIO_TEST, snprintf_e) {
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700761 char buf[BUFSIZ];
762
763 snprintf(buf, sizeof(buf), "%e", 1.5);
764 EXPECT_STREQ("1.500000e+00", buf);
765
Chih-Hung Hsiehc2edae32018-12-11 15:16:24 -0800766 snprintf(buf, sizeof(buf), "%Le", 1.5L);
Elliott Hughes4bd97ce2014-04-10 17:48:14 -0700767 EXPECT_STREQ("1.500000e+00", buf);
768}
769
Christopher Ferris13f26a72016-01-13 13:47:58 -0800770TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
Elliott Hughese77f38f2014-05-14 12:39:12 -0700771 char buf[BUFSIZ];
772
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800773 snprintf(buf, sizeof(buf), "%e", -0.0);
774 EXPECT_STREQ("-0.000000e+00", buf);
775 snprintf(buf, sizeof(buf), "%E", -0.0);
776 EXPECT_STREQ("-0.000000E+00", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700777 snprintf(buf, sizeof(buf), "%f", -0.0);
778 EXPECT_STREQ("-0.000000", buf);
Elliott Hughes1b18aff2014-12-16 14:45:32 -0800779 snprintf(buf, sizeof(buf), "%F", -0.0);
780 EXPECT_STREQ("-0.000000", buf);
781 snprintf(buf, sizeof(buf), "%g", -0.0);
782 EXPECT_STREQ("-0", buf);
783 snprintf(buf, sizeof(buf), "%G", -0.0);
784 EXPECT_STREQ("-0", buf);
785 snprintf(buf, sizeof(buf), "%a", -0.0);
786 EXPECT_STREQ("-0x0p+0", buf);
787 snprintf(buf, sizeof(buf), "%A", -0.0);
788 EXPECT_STREQ("-0X0P+0", buf);
Elliott Hughese77f38f2014-05-14 12:39:12 -0700789}
790
Christopher Ferris13f26a72016-01-13 13:47:58 -0800791TEST(STDIO_TEST, snprintf_utf8_15439554) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700792 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", nullptr);
Wally Yaua40fdbd2014-08-26 09:47:23 -0700793 locale_t old_locale = uselocale(cloc);
Dan Albert1aec7c12014-07-30 10:53:48 -0700794
Elliott Hughes69f05d22014-06-05 20:10:09 -0700795 // http://b/15439554
796 char buf[BUFSIZ];
797
798 // 1-byte character.
799 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
800 EXPECT_STREQ("1x2", buf);
801 // 2-byte character.
802 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
803 EXPECT_STREQ("1¢2", buf);
804 // 3-byte character.
805 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
806 EXPECT_STREQ("1€2", buf);
807 // 4-byte character.
808 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
809 EXPECT_STREQ("1𤭢2", buf);
Dan Albert1aec7c12014-07-30 10:53:48 -0700810
Wally Yaua40fdbd2014-08-26 09:47:23 -0700811 uselocale(old_locale);
Dan Albert1aec7c12014-07-30 10:53:48 -0700812 freelocale(cloc);
Elliott Hughes69f05d22014-06-05 20:10:09 -0700813}
814
Elliott Hughes43f7c872016-02-05 11:18:41 -0800815static void* snprintf_small_stack_fn(void*) {
816 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
817 char buf[PATH_MAX];
818 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
819 return nullptr;
820}
821
822TEST(STDIO_TEST, snprintf_small_stack) {
823 // Is it safe to call snprintf on a thread with a small stack?
824 // (The snprintf implementation puts some pretty large buffers on the stack.)
825 pthread_attr_t a;
826 ASSERT_EQ(0, pthread_attr_init(&a));
827 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
828
829 pthread_t t;
830 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
831 ASSERT_EQ(0, pthread_join(t, nullptr));
832}
833
Elliott Hughes8200e552016-02-05 21:57:37 -0800834TEST(STDIO_TEST, snprintf_asterisk_overflow) {
835 char buf[128];
836 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
837 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
838 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
839 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
840 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
841
842 // INT_MAX-1, INT_MAX, INT_MAX+1.
843 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
844 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
845 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
846 ASSERT_EQ(ENOMEM, errno);
847}
848
Elliott Hughes70715da2016-08-01 16:35:17 -0700849TEST(STDIO_TEST, fprintf) {
850 TemporaryFile tf;
851
852 FILE* tfile = fdopen(tf.fd, "r+");
853 ASSERT_TRUE(tfile != nullptr);
854
855 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
856 AssertFileIs(tfile, "123 abc");
857 fclose(tfile);
858}
859
Christopher Ferris13f26a72016-01-13 13:47:58 -0800860TEST(STDIO_TEST, fprintf_failures_7229520) {
Elliott Hughes69f05d22014-06-05 20:10:09 -0700861 // http://b/7229520
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700862 FILE* fp;
Josh Gaof6e5b582018-06-01 15:30:54 -0700863 int fd_rdonly = open("/dev/null", O_RDONLY);
864 ASSERT_NE(-1, fd_rdonly);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700865
866 // Unbuffered case where the fprintf(3) itself fails.
867 ASSERT_NE(nullptr, fp = tmpfile());
Yi Kong32bc0fc2018-08-02 17:31:13 -0700868 setbuf(fp, nullptr);
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700869 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700870 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700871 ASSERT_EQ(-1, fprintf(fp, "fail"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700872 ASSERT_EQ(0, fclose(fp));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700873
874 // Buffered case where we won't notice until the fclose(3).
875 // It's likely this is what was actually seen in http://b/7229520,
876 // and that expecting fprintf to fail is setting yourself up for
877 // disappointment. Remember to check fclose(3)'s return value, kids!
878 ASSERT_NE(nullptr, fp = tmpfile());
879 ASSERT_EQ(4, fprintf(fp, "epic"));
Josh Gaof6e5b582018-06-01 15:30:54 -0700880 ASSERT_NE(-1, dup2(fd_rdonly, fileno(fp)));
Elliott Hughesc9244bd2014-05-14 13:31:35 -0700881 ASSERT_EQ(4, fprintf(fp, "fail"));
882 ASSERT_EQ(-1, fclose(fp));
883}
884
Elliott Hughes468efc82018-07-10 14:39:49 -0700885TEST(STDIO_TEST, popen_r) {
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800886 FILE* fp = popen("cat /proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700887 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b3f49a2013-03-06 16:20:55 -0800888
889 char buf[16];
890 char* s = fgets(buf, sizeof(buf), fp);
891 buf[13] = '\0';
892 ASSERT_STREQ("Linux version", s);
893
894 ASSERT_EQ(0, pclose(fp));
895}
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700896
Elliott Hughes468efc82018-07-10 14:39:49 -0700897TEST(STDIO_TEST, popen_socketpair) {
898 FILE* fp = popen("cat", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700899 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700900
901 fputs("hello\nworld\n", fp);
902 fflush(fp);
903
904 char buf[16];
905 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
906 EXPECT_STREQ("hello\n", buf);
907 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
908 EXPECT_STREQ("world\n", buf);
909
910 ASSERT_EQ(0, pclose(fp));
911}
912
913TEST(STDIO_TEST, popen_socketpair_shutdown) {
914 FILE* fp = popen("uniq -c", "r+");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700915 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700916
917 fputs("a\na\na\na\nb\n", fp);
918 fflush(fp);
919 ASSERT_EQ(0, shutdown(fileno(fp), SHUT_WR));
920
921 char buf[16];
922 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
923 EXPECT_STREQ(" 4 a\n", buf);
924 ASSERT_NE(nullptr, fgets(buf, sizeof(buf), fp));
925 EXPECT_STREQ(" 1 b\n", buf);
926
927 ASSERT_EQ(0, pclose(fp));
928}
929
930TEST(STDIO_TEST, popen_return_value_0) {
931 FILE* fp = popen("true", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700932 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700933 int status = pclose(fp);
934 EXPECT_TRUE(WIFEXITED(status));
935 EXPECT_EQ(0, WEXITSTATUS(status));
936}
937
938TEST(STDIO_TEST, popen_return_value_1) {
939 FILE* fp = popen("false", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700940 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700941 int status = pclose(fp);
942 EXPECT_TRUE(WIFEXITED(status));
943 EXPECT_EQ(1, WEXITSTATUS(status));
944}
945
946TEST(STDIO_TEST, popen_return_value_signal) {
947 FILE* fp = popen("kill -7 $$", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700948 ASSERT_TRUE(fp != nullptr);
Elliott Hughes468efc82018-07-10 14:39:49 -0700949 int status = pclose(fp);
950 EXPECT_TRUE(WIFSIGNALED(status));
951 EXPECT_EQ(7, WTERMSIG(status));
952}
953
Christopher Ferris13f26a72016-01-13 13:47:58 -0800954TEST(STDIO_TEST, getc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700955 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700956 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700957 ASSERT_EQ('L', getc(fp));
958 ASSERT_EQ('i', getc(fp));
959 ASSERT_EQ('n', getc(fp));
960 ASSERT_EQ('u', getc(fp));
961 ASSERT_EQ('x', getc(fp));
962 fclose(fp);
963}
964
Christopher Ferris13f26a72016-01-13 13:47:58 -0800965TEST(STDIO_TEST, putc) {
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700966 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -0700967 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b05c8e2013-04-11 13:54:48 -0700968 ASSERT_EQ(EOF, putc('x', fp));
969 fclose(fp);
970}
Elliott Hughes603332f2014-03-12 17:10:41 -0700971
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700972TEST(STDIO_TEST, sscanf_swscanf) {
973 struct stuff {
974 char s1[123];
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800975 int i1, i2;
976 char cs1[3];
977 char s2[3];
978 char c1;
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700979 double d1;
980 float f1;
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800981 char s3[123];
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700982
983 void Check() {
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800984 EXPECT_STREQ("hello", s1);
985 EXPECT_EQ(123, i1);
986 EXPECT_EQ(456, i2);
987 EXPECT_EQ('a', cs1[0]);
988 EXPECT_EQ('b', cs1[1]);
989 EXPECT_EQ('x', cs1[2]); // No terminating NUL.
990 EXPECT_STREQ("AB", s2); // Terminating NUL.
991 EXPECT_EQ('!', c1);
992 EXPECT_DOUBLE_EQ(1.23, d1);
993 EXPECT_FLOAT_EQ(9.0f, f1);
994 EXPECT_STREQ("world", s3);
Elliott Hughes7f0849f2016-08-26 16:17:17 -0700995 }
996 } s;
997
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -0800998 memset(&s, 'x', sizeof(s));
999 ASSERT_EQ(9, sscanf(" hello 123 456abAB! 1.23 0x1.2p3 world",
1000 "%s %i%i%2c%[A-Z]%c %lf %f %s",
1001 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 -07001002 s.Check();
1003
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001004 memset(&s, 'x', sizeof(s));
1005 ASSERT_EQ(9, swscanf(L" hello 123 456abAB! 1.23 0x1.2p3 world",
1006 L"%s %i%i%2c%[A-Z]%c %lf %f %s",
1007 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 -07001008 s.Check();
Elliott Hughes603332f2014-03-12 17:10:41 -07001009}
Elliott Hughes53b24382014-05-02 18:29:25 -07001010
Elliott Hughes0d3ba1f2017-12-06 16:41:35 -08001011template <typename T>
1012static void CheckScanf(int sscanf_fn(const T*, const T*, ...),
1013 const T* input, const T* fmt,
1014 int expected_count, const char* expected_string) {
1015 char buf[256] = {};
1016 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &buf)) << fmt;
1017 ASSERT_STREQ(expected_string, buf) << fmt;
1018}
1019
1020TEST(STDIO_TEST, sscanf_ccl) {
1021 // `abc` is just those characters.
1022 CheckScanf(sscanf, "abcd", "%[abc]", 1, "abc");
1023 // `a-c` is the range 'a' .. 'c'.
1024 CheckScanf(sscanf, "abcd", "%[a-c]", 1, "abc");
1025 CheckScanf(sscanf, "-d", "%[a-c]", 0, "");
1026 CheckScanf(sscanf, "ac-bAd", "%[a--c]", 1, "ac-bA");
1027 // `a-c-e` is equivalent to `a-e`.
1028 CheckScanf(sscanf, "abcdefg", "%[a-c-e]", 1, "abcde");
1029 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1030 CheckScanf(sscanf, "-a-e-b", "%[e-a]", 1, "-a-e-");
1031 // An initial '^' negates the set.
1032 CheckScanf(sscanf, "abcde", "%[^d]", 1, "abc");
1033 CheckScanf(sscanf, "abcdefgh", "%[^c-d]", 1, "ab");
1034 CheckScanf(sscanf, "hgfedcba", "%[^c-d]", 1, "hgfe");
1035 // The first character may be ']' or '-' without being special.
1036 CheckScanf(sscanf, "[[]]x", "%[][]", 1, "[[]]");
1037 CheckScanf(sscanf, "-a-x", "%[-a]", 1, "-a-");
1038 // The last character may be '-' without being special.
1039 CheckScanf(sscanf, "-a-x", "%[a-]", 1, "-a-");
1040 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1041 CheckScanf(sscanf, "+,-/.", "%[+--/]", 1, "+,-/");
1042}
1043
1044TEST(STDIO_TEST, swscanf_ccl) {
1045 // `abc` is just those characters.
1046 CheckScanf(swscanf, L"abcd", L"%[abc]", 1, "abc");
1047 // `a-c` is the range 'a' .. 'c'.
1048 CheckScanf(swscanf, L"abcd", L"%[a-c]", 1, "abc");
1049 CheckScanf(swscanf, L"-d", L"%[a-c]", 0, "");
1050 CheckScanf(swscanf, L"ac-bAd", L"%[a--c]", 1, "ac-bA");
1051 // `a-c-e` is equivalent to `a-e`.
1052 CheckScanf(swscanf, L"abcdefg", L"%[a-c-e]", 1, "abcde");
1053 // `e-a` is equivalent to `ae-` (because 'e' > 'a').
1054 CheckScanf(swscanf, L"-a-e-b", L"%[e-a]", 1, "-a-e-");
1055 // An initial '^' negates the set.
1056 CheckScanf(swscanf, L"abcde", L"%[^d]", 1, "abc");
1057 CheckScanf(swscanf, L"abcdefgh", L"%[^c-d]", 1, "ab");
1058 CheckScanf(swscanf, L"hgfedcba", L"%[^c-d]", 1, "hgfe");
1059 // The first character may be ']' or '-' without being special.
1060 CheckScanf(swscanf, L"[[]]x", L"%[][]", 1, "[[]]");
1061 CheckScanf(swscanf, L"-a-x", L"%[-a]", 1, "-a-");
1062 // The last character may be '-' without being special.
1063 CheckScanf(swscanf, L"-a-x", L"%[a-]", 1, "-a-");
1064 // X--Y is [X--] + Y, not [X--] + [--Y] (a bug in my initial implementation).
1065 CheckScanf(swscanf, L"+,-/.", L"%[+--/]", 1, "+,-/");
1066}
1067
Elliott Hughes38e4aef2018-01-18 10:21:29 -08001068template <typename T1, typename T2>
1069static void CheckScanfM(int sscanf_fn(const T1*, const T1*, ...),
1070 const T1* input, const T1* fmt,
1071 int expected_count, const T2* expected_string) {
1072 T2* result = nullptr;
1073 ASSERT_EQ(expected_count, sscanf_fn(input, fmt, &result)) << fmt;
1074 if (expected_string == nullptr) {
1075 ASSERT_EQ(nullptr, result);
1076 } else {
1077 ASSERT_STREQ(expected_string, result) << fmt;
1078 }
1079 free(result);
1080}
1081
1082TEST(STDIO_TEST, sscanf_mc) {
1083 char* p1 = nullptr;
1084 char* p2 = nullptr;
1085 ASSERT_EQ(2, sscanf("hello", "%mc%mc", &p1, &p2));
1086 ASSERT_EQ('h', *p1);
1087 ASSERT_EQ('e', *p2);
1088 free(p1);
1089 free(p2);
1090
1091 p1 = nullptr;
1092 ASSERT_EQ(1, sscanf("hello", "%4mc", &p1));
1093 ASSERT_EQ('h', p1[0]);
1094 ASSERT_EQ('e', p1[1]);
1095 ASSERT_EQ('l', p1[2]);
1096 ASSERT_EQ('l', p1[3]);
1097 free(p1);
1098
1099 p1 = nullptr;
1100 ASSERT_EQ(1, sscanf("hello world", "%30mc", &p1));
1101 ASSERT_EQ('h', p1[0]);
1102 ASSERT_EQ('e', p1[1]);
1103 ASSERT_EQ('l', p1[2]);
1104 ASSERT_EQ('l', p1[3]);
1105 ASSERT_EQ('o', p1[4]);
1106 free(p1);
1107}
1108
1109
1110TEST(STDIO_TEST, sscanf_mlc) {
1111 // This is so useless that clang doesn't even believe it exists...
1112#pragma clang diagnostic push
1113#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1114#pragma clang diagnostic ignored "-Wformat-extra-args"
1115
1116 wchar_t* p1 = nullptr;
1117 wchar_t* p2 = nullptr;
1118 ASSERT_EQ(2, sscanf("hello", "%mlc%mlc", &p1, &p2));
1119 ASSERT_EQ(L'h', *p1);
1120 ASSERT_EQ(L'e', *p2);
1121 free(p1);
1122 free(p2);
1123
1124 p1 = nullptr;
1125 ASSERT_EQ(1, sscanf("hello", "%4mlc", &p1));
1126 ASSERT_EQ(L'h', p1[0]);
1127 ASSERT_EQ(L'e', p1[1]);
1128 ASSERT_EQ(L'l', p1[2]);
1129 ASSERT_EQ(L'l', p1[3]);
1130 free(p1);
1131
1132 p1 = nullptr;
1133 ASSERT_EQ(1, sscanf("hello world", "%30mlc", &p1));
1134 ASSERT_EQ(L'h', p1[0]);
1135 ASSERT_EQ(L'e', p1[1]);
1136 ASSERT_EQ(L'l', p1[2]);
1137 ASSERT_EQ(L'l', p1[3]);
1138 ASSERT_EQ(L'o', p1[4]);
1139 free(p1);
1140#pragma clang diagnostic pop
1141}
1142
1143
1144TEST(STDIO_TEST, sscanf_ms) {
1145 CheckScanfM(sscanf, "hello", "%ms", 1, "hello");
1146 CheckScanfM(sscanf, "hello", "%4ms", 1, "hell");
1147 CheckScanfM(sscanf, "hello world", "%30ms", 1, "hello");
1148}
1149
1150TEST(STDIO_TEST, sscanf_mls) {
1151 CheckScanfM(sscanf, "hello", "%mls", 1, L"hello");
1152 CheckScanfM(sscanf, "hello", "%4mls", 1, L"hell");
1153 CheckScanfM(sscanf, "hello world", "%30mls", 1, L"hello");
1154}
1155
1156TEST(STDIO_TEST, sscanf_m_ccl) {
1157 CheckScanfM(sscanf, "hello", "%m[a-z]", 1, "hello");
1158 CheckScanfM(sscanf, "hello", "%4m[a-z]", 1, "hell");
1159 CheckScanfM(sscanf, "hello world", "%30m[a-z]", 1, "hello");
1160}
1161
1162TEST(STDIO_TEST, sscanf_ml_ccl) {
1163 CheckScanfM(sscanf, "hello", "%ml[a-z]", 1, L"hello");
1164 CheckScanfM(sscanf, "hello", "%4ml[a-z]", 1, L"hell");
1165 CheckScanfM(sscanf, "hello world", "%30ml[a-z]", 1, L"hello");
1166}
1167
1168TEST(STDIO_TEST, sscanf_ls) {
1169 wchar_t w[32] = {};
1170 ASSERT_EQ(1, sscanf("hello world", "%ls", w));
1171 ASSERT_EQ(L"hello", std::wstring(w));
1172}
1173
1174TEST(STDIO_TEST, sscanf_ls_suppress) {
1175 ASSERT_EQ(0, sscanf("hello world", "%*ls %*ls"));
1176}
1177
1178TEST(STDIO_TEST, sscanf_ls_n) {
1179 setlocale(LC_ALL, "C.UTF-8");
1180 wchar_t w[32] = {};
1181 int pos = 0;
1182 ASSERT_EQ(1, sscanf("\xc4\x80", "%ls%n", w, &pos));
1183 ASSERT_EQ(static_cast<wchar_t>(256), w[0]);
1184 ASSERT_EQ(2, pos);
1185}
1186
1187TEST(STDIO_TEST, sscanf_ls_realloc) {
1188 // This is so useless that clang doesn't even believe it exists...
1189#pragma clang diagnostic push
1190#pragma clang diagnostic ignored "-Wformat-invalid-specifier"
1191#pragma clang diagnostic ignored "-Wformat-extra-args"
1192 wchar_t* p1 = nullptr;
1193 wchar_t* p2 = nullptr;
1194 ASSERT_EQ(2, sscanf("1234567890123456789012345678901234567890 world", "%mls %mls", &p1, &p2));
1195 ASSERT_EQ(L"1234567890123456789012345678901234567890", std::wstring(p1));
1196 ASSERT_EQ(L"world", std::wstring(p2));
1197#pragma clang diagnostic pop
1198}
1199
Elliott Hughesbf9cb9e2017-12-11 12:39:01 -08001200// https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240
1201TEST(STDIO_TEST, scanf_wscanf_EOF) {
1202 EXPECT_EQ(0, sscanf("b", "ab"));
1203 EXPECT_EQ(EOF, sscanf("", "a"));
1204 EXPECT_EQ(0, swscanf(L"b", L"ab"));
1205 EXPECT_EQ(EOF, swscanf(L"", L"a"));
1206}
1207
1208TEST(STDIO_TEST, scanf_invalid_UTF8) {
1209#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1210 char buf[BUFSIZ];
1211 wchar_t wbuf[BUFSIZ];
1212
1213 memset(buf, 0, sizeof(buf));
1214 memset(wbuf, 0, sizeof(wbuf));
1215 EXPECT_EQ(0, sscanf("\xc0" " foo", "%ls %s", wbuf, buf));
1216#endif
1217}
1218
1219TEST(STDIO_TEST, scanf_no_match_no_termination) {
1220 char buf[4] = "x";
1221 EXPECT_EQ(0, sscanf("d", "%[abc]", buf));
1222 EXPECT_EQ('x', buf[0]);
1223 EXPECT_EQ(0, swscanf(L"d", L"%[abc]", buf));
1224 EXPECT_EQ('x', buf[0]);
1225
1226 wchar_t wbuf[4] = L"x";
1227 EXPECT_EQ(0, swscanf(L"d", L"%l[abc]", wbuf));
1228 EXPECT_EQ(L'x', wbuf[0]);
1229
1230 EXPECT_EQ(EOF, sscanf("", "%s", buf));
1231 EXPECT_EQ('x', buf[0]);
1232
1233 EXPECT_EQ(EOF, swscanf(L"", L"%ls", wbuf));
1234 EXPECT_EQ(L'x', wbuf[0]);
1235}
1236
1237TEST(STDIO_TEST, scanf_wscanf_wide_character_class) {
1238#if 0 // TODO: more tests invented during code review; no regressions, so fix later.
1239 wchar_t buf[BUFSIZ];
1240
1241 // A wide character shouldn't match an ASCII-only class for scanf or wscanf.
1242 memset(buf, 0, sizeof(buf));
1243 EXPECT_EQ(1, sscanf("xĀyz", "%l[xy]", buf));
1244 EXPECT_EQ(L"x"s, std::wstring(buf));
1245 memset(buf, 0, sizeof(buf));
1246 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xy]", buf));
1247 EXPECT_EQ(L"x"s, std::wstring(buf));
1248
1249 // Even if scanf has wide characters in a class, they won't match...
1250 // TODO: is that a bug?
1251 memset(buf, 0, sizeof(buf));
1252 EXPECT_EQ(1, sscanf("xĀyz", "%l[xĀy]", buf));
1253 EXPECT_EQ(L"x"s, std::wstring(buf));
1254 // ...unless you use wscanf.
1255 memset(buf, 0, sizeof(buf));
1256 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[xĀy]", buf));
1257 EXPECT_EQ(L"xĀy"s, std::wstring(buf));
1258
1259 // Negation only covers ASCII for scanf...
1260 memset(buf, 0, sizeof(buf));
1261 EXPECT_EQ(1, sscanf("xĀyz", "%l[^ab]", buf));
1262 EXPECT_EQ(L"x"s, std::wstring(buf));
1263 // ...but covers wide characters for wscanf.
1264 memset(buf, 0, sizeof(buf));
1265 EXPECT_EQ(1, swscanf(L"xĀyz", L"%l[^ab]", buf));
1266 EXPECT_EQ(L"xĀyz"s, std::wstring(buf));
1267
1268 // We already determined that non-ASCII characters are ignored in scanf classes.
1269 memset(buf, 0, sizeof(buf));
1270 EXPECT_EQ(1, sscanf("x"
1271 "\xc4\x80" // Matches a byte from each wide char in the class.
1272 "\xc6\x82" // Neither byte is in the class.
1273 "yz",
1274 "%l[xy" "\xc5\x80" "\xc4\x81" "]", buf));
1275 EXPECT_EQ(L"x", std::wstring(buf));
1276 // bionic and glibc both behave badly for wscanf, so let's call it right for now...
1277 memset(buf, 0, sizeof(buf));
1278 EXPECT_EQ(1, swscanf(L"x"
1279 L"\xc4\x80"
1280 L"\xc6\x82"
1281 L"yz",
1282 L"%l[xy" L"\xc5\x80" L"\xc4\x81" L"]", buf));
1283 // Note that this isn't L"xĀ" --- although the *bytes* matched, they're
1284 // not put back together as a wide character.
1285 EXPECT_EQ(L"x" L"\xc4" L"\x80", std::wstring(buf));
1286#endif
1287}
1288
Christopher Ferris13f26a72016-01-13 13:47:58 -08001289TEST(STDIO_TEST, cantwrite_EBADF) {
Elliott Hughes53b24382014-05-02 18:29:25 -07001290 // If we open a file read-only...
1291 FILE* fp = fopen("/proc/version", "r");
1292
1293 // ...all attempts to write to that file should return failure.
1294
1295 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
1296 // glibc gets the wide-character functions wrong.
1297
1298 errno = 0;
1299 EXPECT_EQ(EOF, putc('x', fp));
1300 EXPECT_EQ(EBADF, errno);
1301
1302 errno = 0;
1303 EXPECT_EQ(EOF, fprintf(fp, "hello"));
1304 EXPECT_EQ(EBADF, errno);
1305
1306 errno = 0;
1307 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
Elliott Hughes063525c2014-05-13 11:19:57 -07001308#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001309 EXPECT_EQ(EBADF, errno);
1310#endif
1311
1312 errno = 0;
Elliott Hughes53b24382014-05-02 18:29:25 -07001313 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
1314 EXPECT_EQ(EBADF, errno);
1315
1316 errno = 0;
1317 EXPECT_EQ(EOF, fputs("hello", fp));
1318 EXPECT_EQ(EBADF, errno);
1319
1320 errno = 0;
1321 EXPECT_EQ(WEOF, fputwc(L'x', fp));
Elliott Hughes063525c2014-05-13 11:19:57 -07001322#if defined(__BIONIC__)
Elliott Hughes53b24382014-05-02 18:29:25 -07001323 EXPECT_EQ(EBADF, errno);
1324#endif
1325}
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001326
1327// Tests that we can only have a consistent and correct fpos_t when using
1328// f*pos functions (i.e. fpos doesn't get inside a multi byte character).
Christopher Ferris13f26a72016-01-13 13:47:58 -08001329TEST(STDIO_TEST, consistent_fpos_t) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001330 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1331 uselocale(LC_GLOBAL_LOCALE);
1332
1333 FILE* fp = tmpfile();
Yi Kong32bc0fc2018-08-02 17:31:13 -07001334 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001335
1336 wchar_t mb_one_bytes = L'h';
1337 wchar_t mb_two_bytes = 0x00a2;
1338 wchar_t mb_three_bytes = 0x20ac;
1339 wchar_t mb_four_bytes = 0x24b62;
1340
1341 // Write to file.
1342 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
1343 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1344 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1345 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1346
1347 rewind(fp);
1348
1349 // Record each character position.
1350 fpos_t pos1;
1351 fpos_t pos2;
1352 fpos_t pos3;
1353 fpos_t pos4;
1354 fpos_t pos5;
1355 EXPECT_EQ(0, fgetpos(fp, &pos1));
1356 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1357 EXPECT_EQ(0, fgetpos(fp, &pos2));
1358 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1359 EXPECT_EQ(0, fgetpos(fp, &pos3));
1360 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1361 EXPECT_EQ(0, fgetpos(fp, &pos4));
1362 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1363 EXPECT_EQ(0, fgetpos(fp, &pos5));
1364
Elliott Hughes063525c2014-05-13 11:19:57 -07001365#if defined(__BIONIC__)
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001366 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
1367 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
1368 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
1369 // structure.
1370 ASSERT_EQ(0, static_cast<off_t>(pos1));
1371 ASSERT_EQ(1, static_cast<off_t>(pos2));
1372 ASSERT_EQ(3, static_cast<off_t>(pos3));
1373 ASSERT_EQ(6, static_cast<off_t>(pos4));
1374 ASSERT_EQ(10, static_cast<off_t>(pos5));
1375#endif
1376
1377 // Exercise back and forth movements of the position.
1378 ASSERT_EQ(0, fsetpos(fp, &pos2));
1379 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1380 ASSERT_EQ(0, fsetpos(fp, &pos1));
1381 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
1382 ASSERT_EQ(0, fsetpos(fp, &pos4));
1383 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
1384 ASSERT_EQ(0, fsetpos(fp, &pos3));
1385 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
1386 ASSERT_EQ(0, fsetpos(fp, &pos5));
1387 ASSERT_EQ(WEOF, fgetwc(fp));
1388
1389 fclose(fp);
1390}
1391
1392// Exercise the interaction between fpos and seek.
Christopher Ferris13f26a72016-01-13 13:47:58 -08001393TEST(STDIO_TEST, fpos_t_and_seek) {
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001394 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
1395 uselocale(LC_GLOBAL_LOCALE);
1396
Calin Juravle9b95ea92014-05-14 17:07:10 +01001397 // In glibc-2.16 fseek doesn't work properly in wide mode
1398 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
1399 // to close and re-open the file. We do it in order to make the test pass
1400 // with all glibcs.
1401
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001402 TemporaryFile tf;
1403 FILE* fp = fdopen(tf.fd, "w+");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001404 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001405
1406 wchar_t mb_two_bytes = 0x00a2;
1407 wchar_t mb_three_bytes = 0x20ac;
1408 wchar_t mb_four_bytes = 0x24b62;
1409
1410 // Write to file.
1411 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
1412 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
1413 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
1414
1415 fflush(fp);
1416 fclose(fp);
1417
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08001418 fp = fopen(tf.path, "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001419 ASSERT_TRUE(fp != nullptr);
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001420
1421 // Store a valid position.
1422 fpos_t mb_two_bytes_pos;
1423 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
1424
1425 // Move inside mb_four_bytes with fseek.
1426 long offset_inside_mb = 6;
1427 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
1428
1429 // Store the "inside multi byte" position.
1430 fpos_t pos_inside_mb;
1431 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
Elliott Hughes063525c2014-05-13 11:19:57 -07001432#if defined(__BIONIC__)
1433 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
1434#endif
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001435
1436 // Reading from within a byte should produce an error.
1437 ASSERT_EQ(WEOF, fgetwc(fp));
1438 ASSERT_EQ(EILSEQ, errno);
1439
1440 // Reverting to a valid position should work.
1441 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
1442 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
1443
1444 // Moving withing a multi byte with fsetpos should work but reading should
1445 // produce an error.
1446 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
1447 ASSERT_EQ(WEOF, fgetwc(fp));
1448 ASSERT_EQ(EILSEQ, errno);
1449
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001450 ASSERT_EQ(0, fclose(fp));
Calin Juravle03e4ebe2014-05-08 14:42:06 +01001451}
Elliott Hughes6b841db2014-08-20 16:10:49 -07001452
Christopher Ferris13f26a72016-01-13 13:47:58 -08001453TEST(STDIO_TEST, fmemopen) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001454 char buf[16];
1455 memset(buf, 0, sizeof(buf));
1456 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
1457 ASSERT_EQ('<', fputc('<', fp));
1458 ASSERT_NE(EOF, fputs("abc>\n", fp));
1459 fflush(fp);
1460
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001461 // We wrote to the buffer...
Elliott Hughes6b841db2014-08-20 16:10:49 -07001462 ASSERT_STREQ("<abc>\n", buf);
1463
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001464 // And can read back from the file.
Elliott Hughes70715da2016-08-01 16:35:17 -07001465 AssertFileIs(fp, "<abc>\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001466 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001467}
1468
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001469TEST(STDIO_TEST, fmemopen_nullptr) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001470 FILE* fp = fmemopen(nullptr, 128, "r+");
1471 ASSERT_NE(EOF, fputs("xyz\n", fp));
1472
Elliott Hughes70715da2016-08-01 16:35:17 -07001473 AssertFileIs(fp, "xyz\n", true);
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001474 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001475}
1476
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001477TEST(STDIO_TEST, fmemopen_trailing_NUL_byte) {
1478 FILE* fp;
1479 char buf[8];
1480
1481 // POSIX: "When a stream open for writing is flushed or closed, a null byte
1482 // shall be written at the current position or at the end of the buffer,
1483 // depending on the size of the contents."
1484 memset(buf, 'x', sizeof(buf));
1485 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1486 // Even with nothing written (and not in truncate mode), we'll flush a NUL...
1487 ASSERT_EQ(0, fflush(fp));
1488 EXPECT_EQ("\0xxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1489 // Now write and check that the NUL moves along with our writes...
1490 ASSERT_NE(EOF, fputs("hello", fp));
1491 ASSERT_EQ(0, fflush(fp));
1492 EXPECT_EQ("hello\0xx"s, std::string(buf, buf + sizeof(buf)));
1493 ASSERT_NE(EOF, fputs("wo", fp));
1494 ASSERT_EQ(0, fflush(fp));
1495 EXPECT_EQ("hellowo\0"s, std::string(buf, buf + sizeof(buf)));
1496 ASSERT_EQ(0, fclose(fp));
1497
1498 // "If a stream open for update is flushed or closed and the last write has
1499 // advanced the current buffer size, a null byte shall be written at the end
1500 // of the buffer if it fits."
1501 memset(buf, 'x', sizeof(buf));
1502 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1503 // Nothing written yet, so no advance...
1504 ASSERT_EQ(0, fflush(fp));
1505 EXPECT_EQ("xxxxxxxx"s, std::string(buf, buf + sizeof(buf)));
1506 ASSERT_NE(EOF, fputs("hello", fp));
1507 ASSERT_EQ(0, fclose(fp));
1508}
1509
1510TEST(STDIO_TEST, fmemopen_size) {
1511 FILE* fp;
Elliott Hughes6b841db2014-08-20 16:10:49 -07001512 char buf[16];
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001513 memset(buf, 'x', sizeof(buf));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001514
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001515 // POSIX: "The stream shall also maintain the size of the current buffer
1516 // contents; use of fseek() or fseeko() on the stream with SEEK_END shall
1517 // seek relative to this size."
Elliott Hughes6b841db2014-08-20 16:10:49 -07001518
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001519 // "For modes r and r+ the size shall be set to the value given by the size
1520 // argument."
1521 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r"));
1522 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1523 EXPECT_EQ(16, ftell(fp));
1524 EXPECT_EQ(16, ftello(fp));
1525 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1526 EXPECT_EQ(16, ftell(fp));
1527 EXPECT_EQ(16, ftello(fp));
1528 ASSERT_EQ(0, fclose(fp));
1529 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "r+"));
1530 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1531 EXPECT_EQ(16, ftell(fp));
1532 EXPECT_EQ(16, ftello(fp));
1533 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1534 EXPECT_EQ(16, ftell(fp));
1535 EXPECT_EQ(16, ftello(fp));
1536 ASSERT_EQ(0, fclose(fp));
1537
1538 // "For modes w and w+ the initial size shall be zero..."
1539 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1540 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1541 EXPECT_EQ(0, ftell(fp));
1542 EXPECT_EQ(0, ftello(fp));
1543 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1544 EXPECT_EQ(0, ftell(fp));
1545 EXPECT_EQ(0, ftello(fp));
1546 ASSERT_EQ(0, fclose(fp));
1547 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w+"));
1548 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1549 EXPECT_EQ(0, ftell(fp));
1550 EXPECT_EQ(0, ftello(fp));
1551 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1552 EXPECT_EQ(0, ftell(fp));
1553 EXPECT_EQ(0, ftello(fp));
1554 ASSERT_EQ(0, fclose(fp));
1555
1556 // "...and for modes a and a+ the initial size shall be:
1557 // 1. Zero, if buf is a null pointer
1558 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a"));
1559 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1560 EXPECT_EQ(0, ftell(fp));
1561 EXPECT_EQ(0, ftello(fp));
1562 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1563 EXPECT_EQ(0, ftell(fp));
1564 EXPECT_EQ(0, ftello(fp));
1565 ASSERT_EQ(0, fclose(fp));
1566 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "a+"));
1567 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1568 EXPECT_EQ(0, ftell(fp));
1569 EXPECT_EQ(0, ftello(fp));
1570 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1571 EXPECT_EQ(0, ftell(fp));
1572 EXPECT_EQ(0, ftello(fp));
1573 ASSERT_EQ(0, fclose(fp));
1574
1575 // 2. The position of the first null byte in the buffer, if one is found
1576 memset(buf, 'x', sizeof(buf));
1577 buf[3] = '\0';
1578 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1579 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1580 EXPECT_EQ(3, ftell(fp));
1581 EXPECT_EQ(3, ftello(fp));
1582 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1583 EXPECT_EQ(3, ftell(fp));
1584 EXPECT_EQ(3, ftello(fp));
1585 ASSERT_EQ(0, fclose(fp));
1586 memset(buf, 'x', sizeof(buf));
1587 buf[3] = '\0';
1588 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1589 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1590 EXPECT_EQ(3, ftell(fp));
1591 EXPECT_EQ(3, ftello(fp));
1592 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1593 EXPECT_EQ(3, ftell(fp));
1594 EXPECT_EQ(3, ftello(fp));
1595 ASSERT_EQ(0, fclose(fp));
1596
1597 // 3. The value of the size argument, if buf is not a null pointer and no
1598 // null byte is found.
1599 memset(buf, 'x', sizeof(buf));
1600 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a"));
1601 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1602 EXPECT_EQ(16, ftell(fp));
1603 EXPECT_EQ(16, ftello(fp));
1604 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1605 EXPECT_EQ(16, ftell(fp));
1606 EXPECT_EQ(16, ftello(fp));
1607 ASSERT_EQ(0, fclose(fp));
1608 memset(buf, 'x', sizeof(buf));
1609 ASSERT_NE(nullptr, fp = fmemopen(buf, 16, "a+"));
1610 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1611 EXPECT_EQ(16, ftell(fp));
1612 EXPECT_EQ(16, ftello(fp));
1613 ASSERT_EQ(0, fseeko(fp, 0, SEEK_END));
1614 EXPECT_EQ(16, ftell(fp));
1615 EXPECT_EQ(16, ftello(fp));
1616 ASSERT_EQ(0, fclose(fp));
1617}
1618
1619TEST(STDIO_TEST, fmemopen_SEEK_END) {
1620 // fseek SEEK_END is relative to the current string length, not the buffer size.
1621 FILE* fp;
1622 char buf[8];
1623 memset(buf, 'x', sizeof(buf));
1624 strcpy(buf, "str");
1625 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1626 ASSERT_NE(EOF, fputs("string", fp));
1627 EXPECT_EQ(0, fseek(fp, 0, SEEK_END));
1628 EXPECT_EQ(static_cast<long>(strlen("string")), ftell(fp));
1629 EXPECT_EQ(static_cast<off_t>(strlen("string")), ftello(fp));
1630 EXPECT_EQ(0, fclose(fp));
1631
1632 // glibc < 2.22 interpreted SEEK_END the wrong way round (subtracting rather
1633 // than adding).
1634 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1635 ASSERT_NE(EOF, fputs("54321", fp));
1636 EXPECT_EQ(0, fseek(fp, -2, SEEK_END));
1637 EXPECT_EQ('2', fgetc(fp));
1638 EXPECT_EQ(0, fclose(fp));
1639}
1640
1641TEST(STDIO_TEST, fmemopen_seek_invalid) {
1642 char buf[8];
1643 memset(buf, 'x', sizeof(buf));
1644 FILE* fp = fmemopen(buf, sizeof(buf), "w");
1645 ASSERT_TRUE(fp != nullptr);
1646
1647 // POSIX: "An attempt to seek ... to a negative position or to a position
1648 // larger than the buffer size given in the size argument shall fail."
1649 // (There's no mention of what errno should be set to, and glibc doesn't
1650 // set errno in any of these cases.)
1651 EXPECT_EQ(-1, fseek(fp, -2, SEEK_SET));
1652 EXPECT_EQ(-1, fseeko(fp, -2, SEEK_SET));
1653 EXPECT_EQ(-1, fseek(fp, sizeof(buf) + 1, SEEK_SET));
1654 EXPECT_EQ(-1, fseeko(fp, sizeof(buf) + 1, SEEK_SET));
1655}
1656
1657TEST(STDIO_TEST, fmemopen_read_EOF) {
1658 // POSIX: "A read operation on the stream shall not advance the current
1659 // buffer position beyond the current buffer size."
1660 char buf[8];
1661 memset(buf, 'x', sizeof(buf));
1662 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1663 ASSERT_TRUE(fp != nullptr);
1664 char buf2[BUFSIZ];
1665 ASSERT_EQ(8U, fread(buf2, 1, sizeof(buf2), fp));
1666 // POSIX: "Reaching the buffer size in a read operation shall count as
1667 // end-of-file.
1668 ASSERT_TRUE(feof(fp));
1669 ASSERT_EQ(EOF, fgetc(fp));
1670 ASSERT_EQ(0, fclose(fp));
1671}
1672
1673TEST(STDIO_TEST, fmemopen_read_null_bytes) {
1674 // POSIX: "Null bytes in the buffer shall have no special meaning for reads."
1675 char buf[] = "h\0e\0l\0l\0o";
1676 FILE* fp = fmemopen(buf, sizeof(buf), "r");
1677 ASSERT_TRUE(fp != nullptr);
1678 ASSERT_EQ('h', fgetc(fp));
1679 ASSERT_EQ(0, fgetc(fp));
1680 ASSERT_EQ('e', fgetc(fp));
1681 ASSERT_EQ(0, fgetc(fp));
1682 ASSERT_EQ('l', fgetc(fp));
1683 ASSERT_EQ(0, fgetc(fp));
1684 // POSIX: "The read operation shall start at the current buffer position of
1685 // the stream."
1686 char buf2[8];
1687 memset(buf2, 'x', sizeof(buf2));
1688 ASSERT_EQ(4U, fread(buf2, 1, sizeof(buf2), fp));
1689 ASSERT_EQ('l', buf2[0]);
1690 ASSERT_EQ(0, buf2[1]);
1691 ASSERT_EQ('o', buf2[2]);
1692 ASSERT_EQ(0, buf2[3]);
1693 for (size_t i = 4; i < sizeof(buf2); ++i) ASSERT_EQ('x', buf2[i]) << i;
1694 ASSERT_TRUE(feof(fp));
1695 ASSERT_EQ(0, fclose(fp));
1696}
1697
1698TEST(STDIO_TEST, fmemopen_write) {
1699 FILE* fp;
1700 char buf[8];
1701
1702 // POSIX: "A write operation shall start either at the current position of
1703 // the stream (if mode has not specified 'a' as the first character)..."
1704 memset(buf, 'x', sizeof(buf));
1705 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r+"));
1706 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1707 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
1708 ASSERT_EQ(' ', fputc(' ', fp));
1709 EXPECT_EQ("xx xxxxx", std::string(buf, buf + sizeof(buf)));
1710 ASSERT_EQ(0, fclose(fp));
1711
1712 // "...or at the current size of the stream (if mode had 'a' as the first
1713 // character)." (See the fmemopen_size test for what "size" means, but for
1714 // mode "a", it's the first NUL byte.)
1715 memset(buf, 'x', sizeof(buf));
1716 buf[3] = '\0';
1717 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1718 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1719 ASSERT_EQ(' ', fputc(' ', fp));
1720 EXPECT_EQ("xxx \0xxx"s, std::string(buf, buf + sizeof(buf)));
1721 ASSERT_EQ(0, fclose(fp));
1722
1723 // "If the current position at the end of the write is larger than the
1724 // current buffer size, the current buffer size shall be set to the current
1725 // position." (See the fmemopen_size test for what "size" means, but to
1726 // query it we SEEK_END with offset 0, and then ftell.)
1727 memset(buf, 'x', sizeof(buf));
1728 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w+"));
1729 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1730 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1731 EXPECT_EQ(0, ftell(fp));
1732 ASSERT_EQ(' ', fputc(' ', fp));
1733 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1734 EXPECT_EQ(1, ftell(fp));
1735 ASSERT_NE(EOF, fputs("123", fp));
1736 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1737 EXPECT_EQ(4, ftell(fp));
1738 EXPECT_EQ(" 123\0xxx"s, std::string(buf, buf + sizeof(buf)));
1739 ASSERT_EQ(0, fclose(fp));
1740}
1741
1742TEST(STDIO_TEST, fmemopen_write_EOF) {
1743 // POSIX: "A write operation on the stream shall not advance the current
1744 // buffer size beyond the size given in the size argument."
1745 FILE* fp;
1746
1747 // Scalar writes...
1748 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1749 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1750 ASSERT_EQ('x', fputc('x', fp));
1751 ASSERT_EQ('x', fputc('x', fp));
1752 ASSERT_EQ('x', fputc('x', fp));
1753 ASSERT_EQ(EOF, fputc('x', fp)); // Only 3 fit because of the implicit NUL.
1754 ASSERT_EQ(0, fclose(fp));
1755
1756 // Vector writes...
1757 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 4, "w"));
1758 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1759 ASSERT_EQ(3U, fwrite("xxxx", 1, 4, fp));
1760 ASSERT_EQ(0, fclose(fp));
1761}
1762
1763TEST(STDIO_TEST, fmemopen_initial_position) {
1764 // POSIX: "The ... current position in the buffer ... shall be initially
1765 // set to either the beginning of the buffer (for r and w modes) ..."
1766 char buf[] = "hello\0world";
1767 FILE* fp;
1768 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "r"));
1769 EXPECT_EQ(0L, ftell(fp));
1770 EXPECT_EQ(0, fclose(fp));
1771 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "w"));
1772 EXPECT_EQ(0L, ftell(fp));
1773 EXPECT_EQ(0, fclose(fp));
1774 buf[0] = 'h'; // (Undo the effects of the above.)
1775
1776 // POSIX: "...or to the first null byte in the buffer (for a modes)."
1777 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1778 EXPECT_EQ(5L, ftell(fp));
1779 EXPECT_EQ(0, fclose(fp));
1780
1781 // POSIX: "If no null byte is found in append mode, the initial position
1782 // shall be set to one byte after the end of the buffer."
1783 memset(buf, 'x', sizeof(buf));
1784 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1785 EXPECT_EQ(static_cast<long>(sizeof(buf)), ftell(fp));
1786 EXPECT_EQ(0, fclose(fp));
1787}
1788
1789TEST(STDIO_TEST, fmemopen_initial_position_allocated) {
1790 // POSIX: "If buf is a null pointer, the initial position shall always be
1791 // set to the beginning of the buffer."
1792 FILE* fp = fmemopen(nullptr, 128, "a+");
1793 ASSERT_TRUE(fp != nullptr);
1794 EXPECT_EQ(0L, ftell(fp));
1795 EXPECT_EQ(0L, fseek(fp, 0, SEEK_SET));
1796 EXPECT_EQ(0, fclose(fp));
1797}
1798
1799TEST(STDIO_TEST, fmemopen_zero_length) {
1800 // POSIX says it's up to the implementation whether or not you can have a
1801 // zero-length buffer (but "A future version of this standard may require
1802 // support of zero-length buffer streams explicitly"). BSD and glibc < 2.22
1803 // agreed that you couldn't, but glibc >= 2.22 allows it for consistency.
1804 FILE* fp;
1805 char buf[16];
1806 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "r+"));
1807 ASSERT_EQ(EOF, fgetc(fp));
1808 ASSERT_TRUE(feof(fp));
1809 ASSERT_EQ(0, fclose(fp));
1810 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "r+"));
1811 ASSERT_EQ(EOF, fgetc(fp));
1812 ASSERT_TRUE(feof(fp));
1813 ASSERT_EQ(0, fclose(fp));
1814
1815 ASSERT_NE(nullptr, fp = fmemopen(buf, 0, "w+"));
1816 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1817 ASSERT_EQ(EOF, fputc('x', fp));
1818 ASSERT_EQ(0, fclose(fp));
1819 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 0, "w+"));
1820 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1821 ASSERT_EQ(EOF, fputc('x', fp));
1822 ASSERT_EQ(0, fclose(fp));
1823}
1824
Elliott Hughes288465d2019-02-05 15:00:13 -08001825TEST(STDIO_TEST, fmemopen_zero_length_buffer_overrun) {
1826 char buf[2] = "x";
1827 ASSERT_EQ('x', buf[0]);
1828 FILE* fp = fmemopen(buf, 0, "w");
1829 ASSERT_EQ('x', buf[0]);
1830 ASSERT_EQ(0, fclose(fp));
1831}
1832
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001833TEST(STDIO_TEST, fmemopen_write_only_allocated) {
1834 // POSIX says fmemopen "may fail if the mode argument does not include a '+'".
1835 // BSD fails, glibc doesn't. We side with the more lenient.
1836 FILE* fp;
1837 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "r"));
1838 ASSERT_EQ(0, fclose(fp));
1839 ASSERT_NE(nullptr, fp = fmemopen(nullptr, 16, "w"));
1840 ASSERT_EQ(0, fclose(fp));
1841}
1842
1843TEST(STDIO_TEST, fmemopen_fileno) {
1844 // There's no fd backing an fmemopen FILE*.
1845 FILE* fp = fmemopen(nullptr, 16, "r");
1846 ASSERT_TRUE(fp != nullptr);
Elliott Hughes6b841db2014-08-20 16:10:49 -07001847 errno = 0;
Elliott Hughes3a4c4542017-07-19 17:20:24 -07001848 ASSERT_EQ(-1, fileno(fp));
1849 ASSERT_EQ(EBADF, errno);
1850 ASSERT_EQ(0, fclose(fp));
1851}
1852
1853TEST(STDIO_TEST, fmemopen_append_after_seek) {
1854 // In BSD and glibc < 2.22, append mode didn't force writes to append if
1855 // there had been an intervening seek.
1856
1857 FILE* fp;
1858 char buf[] = "hello\0world";
1859 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a"));
1860 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1861 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1862 ASSERT_NE(EOF, fputc('!', fp));
1863 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1864 ASSERT_EQ(0, fclose(fp));
1865
1866 memcpy(buf, "hello\0world", sizeof(buf));
1867 ASSERT_NE(nullptr, fp = fmemopen(buf, sizeof(buf), "a+"));
1868 setbuf(fp, nullptr); // Turn off buffering so we can see what's happening as it happens.
1869 ASSERT_EQ(0, fseek(fp, 0, SEEK_SET));
1870 ASSERT_NE(EOF, fputc('!', fp));
1871 EXPECT_EQ("hello!\0orld\0"s, std::string(buf, buf + sizeof(buf)));
1872 ASSERT_EQ(0, fclose(fp));
Elliott Hughes6b841db2014-08-20 16:10:49 -07001873}
1874
Christopher Ferris13f26a72016-01-13 13:47:58 -08001875TEST(STDIO_TEST, open_memstream) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001876 char* p = nullptr;
1877 size_t size = 0;
1878 FILE* fp = open_memstream(&p, &size);
1879 ASSERT_NE(EOF, fputs("hello, world!", fp));
1880 fclose(fp);
1881
1882 ASSERT_STREQ("hello, world!", p);
1883 ASSERT_EQ(strlen("hello, world!"), size);
1884 free(p);
1885}
1886
Christopher Ferris13f26a72016-01-13 13:47:58 -08001887TEST(STDIO_TEST, open_memstream_EINVAL) {
Elliott Hughes6b841db2014-08-20 16:10:49 -07001888#if defined(__BIONIC__)
1889 char* p;
1890 size_t size;
1891
1892 // Invalid buffer.
1893 errno = 0;
1894 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
1895 ASSERT_EQ(EINVAL, errno);
1896
1897 // Invalid size.
1898 errno = 0;
1899 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
1900 ASSERT_EQ(EINVAL, errno);
1901#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08001902 GTEST_SKIP() << "glibc is broken";
Elliott Hughes6b841db2014-08-20 16:10:49 -07001903#endif
1904}
Elliott Hughes31165ed2014-09-23 17:34:29 -07001905
Christopher Ferris13f26a72016-01-13 13:47:58 -08001906TEST(STDIO_TEST, fdopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001907 int fd = open("/proc/version", O_RDONLY);
1908 ASSERT_TRUE(fd != -1);
1909
1910 // This fd doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001911 AssertCloseOnExec(fd, false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001912
1913 FILE* fp = fdopen(fd, "re");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001914 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001915
1916 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001917 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001918
1919 fclose(fp);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001920}
1921
Christopher Ferris13f26a72016-01-13 13:47:58 -08001922TEST(STDIO_TEST, freopen_CLOEXEC) {
Elliott Hughes31165ed2014-09-23 17:34:29 -07001923 FILE* fp = fopen("/proc/version", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001924 ASSERT_TRUE(fp != nullptr);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001925
1926 // This FILE* doesn't have O_CLOEXEC...
Elliott Hughesa7f12942017-12-15 13:55:53 -08001927 AssertCloseOnExec(fileno(fp), false);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001928
1929 fp = freopen("/proc/version", "re", fp);
1930
1931 // ...but the new one does.
Elliott Hughesa7f12942017-12-15 13:55:53 -08001932 AssertCloseOnExec(fileno(fp), true);
Elliott Hughes31165ed2014-09-23 17:34:29 -07001933
1934 fclose(fp);
1935}
Elliott Hughes20841a12014-12-01 16:13:30 -08001936
Elliott Hughesf226ee52016-02-03 11:24:28 -08001937TEST(STDIO_TEST, fopen64_freopen64) {
1938 FILE* fp = fopen64("/proc/version", "r");
1939 ASSERT_TRUE(fp != nullptr);
1940 fp = freopen64("/proc/version", "re", fp);
1941 ASSERT_TRUE(fp != nullptr);
1942 fclose(fp);
1943}
1944
Elliott Hughes20841a12014-12-01 16:13:30 -08001945// https://code.google.com/p/android/issues/detail?id=81155
1946// http://b/18556607
Christopher Ferris13f26a72016-01-13 13:47:58 -08001947TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
Elliott Hughes20841a12014-12-01 16:13:30 -08001948 FILE* fp = fopen("/dev/zero", "r");
Yi Kong32bc0fc2018-08-02 17:31:13 -07001949 ASSERT_TRUE(fp != nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001950
1951 // Make this stream unbuffered.
Yi Kong32bc0fc2018-08-02 17:31:13 -07001952 setvbuf(fp, nullptr, _IONBF, 0);
Elliott Hughes20841a12014-12-01 16:13:30 -08001953
1954 char buf[65*1024];
1955 memset(buf, 0xff, sizeof(buf));
1956
Yi Kong32bc0fc2018-08-02 17:31:13 -07001957 time_t t0 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001958 for (size_t i = 0; i < 1024; ++i) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001959 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
Elliott Hughes20841a12014-12-01 16:13:30 -08001960 }
Yi Kong32bc0fc2018-08-02 17:31:13 -07001961 time_t t1 = time(nullptr);
Elliott Hughes20841a12014-12-01 16:13:30 -08001962
1963 fclose(fp);
1964
1965 // 1024 64KiB reads should have been very quick.
1966 ASSERT_LE(t1 - t0, 1);
1967
1968 for (size_t i = 0; i < 64*1024; ++i) {
1969 ASSERT_EQ('\0', buf[i]);
1970 }
1971 for (size_t i = 64*1024; i < 65*1024; ++i) {
1972 ASSERT_EQ('\xff', buf[i]);
1973 }
1974}
Elliott Hughes75b99382015-01-20 11:23:50 -08001975
Christopher Ferris13f26a72016-01-13 13:47:58 -08001976TEST(STDIO_TEST, fread_EOF) {
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001977 std::string digits("0123456789");
1978 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
Elliott Hughes75b99382015-01-20 11:23:50 -08001979
1980 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1981 char buf1[4 * 4];
1982 memset(buf1, 0, sizeof(buf1));
1983 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001984 ASSERT_STREQ("0123456789", buf1);
Elliott Hughes75b99382015-01-20 11:23:50 -08001985 ASSERT_TRUE(feof(fp));
1986
1987 rewind(fp);
1988
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001989 // Try to read way too much so stdio tries to read more direct from the stream.
1990 char buf2[4 * 4096];
Elliott Hughes75b99382015-01-20 11:23:50 -08001991 memset(buf2, 0, sizeof(buf2));
1992 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
Elliott Hughes0ed7e082015-01-22 15:13:38 -08001993 ASSERT_STREQ("0123456789", buf2);
Elliott Hughes75b99382015-01-20 11:23:50 -08001994 ASSERT_TRUE(feof(fp));
1995
1996 fclose(fp);
1997}
Elliott Hughese6bb5a22015-01-23 17:48:15 -08001998
1999static void test_fread_from_write_only_stream(size_t n) {
2000 FILE* fp = fopen("/dev/null", "w");
2001 std::vector<char> buf(n, 0);
2002 errno = 0;
2003 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
2004 ASSERT_EQ(EBADF, errno);
2005 ASSERT_TRUE(ferror(fp));
2006 ASSERT_FALSE(feof(fp));
2007 fclose(fp);
2008}
2009
Christopher Ferris13f26a72016-01-13 13:47:58 -08002010TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002011 test_fread_from_write_only_stream(1);
2012}
2013
Christopher Ferris13f26a72016-01-13 13:47:58 -08002014TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002015 test_fread_from_write_only_stream(64*1024);
2016}
2017
2018static void test_fwrite_after_fread(size_t n) {
2019 TemporaryFile tf;
2020
2021 FILE* fp = fdopen(tf.fd, "w+");
2022 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
2023 fflush(fp);
2024
2025 // We've flushed but not rewound, so there's nothing to read.
2026 std::vector<char> buf(n, 0);
2027 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
2028 ASSERT_TRUE(feof(fp));
2029
2030 // But hitting EOF doesn't prevent us from writing...
2031 errno = 0;
Elliott Hughes9677fab2016-01-25 15:50:59 -08002032 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002033
2034 // And if we rewind, everything's there.
2035 rewind(fp);
2036 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
2037 ASSERT_EQ('1', buf[0]);
2038 ASSERT_EQ('2', buf[1]);
2039
2040 fclose(fp);
2041}
2042
Christopher Ferris13f26a72016-01-13 13:47:58 -08002043TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002044 test_fwrite_after_fread(16);
2045}
2046
Christopher Ferris13f26a72016-01-13 13:47:58 -08002047TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
Elliott Hughese6bb5a22015-01-23 17:48:15 -08002048 test_fwrite_after_fread(64*1024);
2049}
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002050
2051// http://b/19172514
Christopher Ferris13f26a72016-01-13 13:47:58 -08002052TEST(STDIO_TEST, fread_after_fseek) {
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002053 TemporaryFile tf;
2054
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002055 FILE* fp = fopen(tf.path, "w+");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002056 ASSERT_TRUE(fp != nullptr);
2057
2058 char file_data[12288];
2059 for (size_t i = 0; i < 12288; i++) {
2060 file_data[i] = i;
2061 }
2062 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
2063 fclose(fp);
2064
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002065 fp = fopen(tf.path, "r");
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002066 ASSERT_TRUE(fp != nullptr);
2067
2068 char buffer[8192];
2069 size_t cur_location = 0;
2070 // Small read to populate internal buffer.
2071 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
2072 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
2073
2074 cur_location = static_cast<size_t>(ftell(fp));
2075 // Large read to force reading into the user supplied buffer and bypassing
2076 // the internal buffer.
2077 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
2078 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
2079
2080 // Small backwards seek to verify fseek does not reuse the internal buffer.
Elliott Hughes9677fab2016-01-25 15:50:59 -08002081 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
Christopher Ferriscc9ca102015-02-27 18:22:45 -08002082 cur_location = static_cast<size_t>(ftell(fp));
2083 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
2084 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
2085
2086 fclose(fp);
2087}
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002088
2089// https://code.google.com/p/android/issues/detail?id=184847
Christopher Ferris13f26a72016-01-13 13:47:58 -08002090TEST(STDIO_TEST, fread_EOF_184847) {
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002091 TemporaryFile tf;
2092 char buf[6] = {0};
2093
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002094 FILE* fw = fopen(tf.path, "w");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002095 ASSERT_TRUE(fw != nullptr);
2096
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002097 FILE* fr = fopen(tf.path, "r");
Elliott Hughes8ab433d2015-10-09 17:57:26 -07002098 ASSERT_TRUE(fr != nullptr);
2099
2100 fwrite("a", 1, 1, fw);
2101 fflush(fw);
2102 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2103 ASSERT_STREQ("a", buf);
2104
2105 // 'fr' is now at EOF.
2106 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2107 ASSERT_TRUE(feof(fr));
2108
2109 // Write some more...
2110 fwrite("z", 1, 1, fw);
2111 fflush(fw);
2112
2113 // ...and check that we can read it back.
2114 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
2115 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
2116 ASSERT_STREQ("z", buf);
2117
2118 // But now we're done.
2119 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
2120
2121 fclose(fr);
2122 fclose(fw);
2123}
Elliott Hughes923f1652016-01-19 15:46:05 -08002124
2125TEST(STDIO_TEST, fclose_invalidates_fd) {
2126 // The typical error we're trying to help people catch involves accessing
2127 // memory after it's been freed. But we know that stdin/stdout/stderr are
2128 // special and don't get deallocated, so this test uses stdin.
2129 ASSERT_EQ(0, fclose(stdin));
2130
2131 // Even though using a FILE* after close is undefined behavior, I've closed
2132 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
2133 // especially because they might actually correspond to a real stream.
2134 errno = 0;
2135 ASSERT_EQ(-1, fileno(stdin));
2136 ASSERT_EQ(EBADF, errno);
2137}
Elliott Hughes2704bd12016-01-20 17:14:53 -08002138
2139TEST(STDIO_TEST, fseek_ftell_unseekable) {
2140#if defined(__BIONIC__) // glibc has fopencookie instead.
2141 auto read_fn = [](void*, char*, int) { return -1; };
2142 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
2143 ASSERT_TRUE(fp != nullptr);
2144
2145 // Check that ftell balks on an unseekable FILE*.
2146 errno = 0;
2147 ASSERT_EQ(-1, ftell(fp));
2148 ASSERT_EQ(ESPIPE, errno);
2149
2150 // SEEK_CUR is rewritten as SEEK_SET internally...
2151 errno = 0;
2152 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
2153 ASSERT_EQ(ESPIPE, errno);
2154
2155 // ...so it's worth testing the direct seek path too.
2156 errno = 0;
2157 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
2158 ASSERT_EQ(ESPIPE, errno);
2159
2160 fclose(fp);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002161#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002162 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002163#endif
2164}
2165
2166TEST(STDIO_TEST, funopen_EINVAL) {
2167#if defined(__BIONIC__)
2168 errno = 0;
2169 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
2170 ASSERT_EQ(EINVAL, errno);
2171#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002172 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002173#endif
2174}
2175
2176TEST(STDIO_TEST, funopen_seek) {
2177#if defined(__BIONIC__)
2178 auto read_fn = [](void*, char*, int) { return -1; };
2179
2180 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
2181 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
2182
2183 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
2184 ASSERT_TRUE(fp != nullptr);
2185 fpos_t pos;
Elliott Hughes955426e2016-01-26 18:25:52 -08002186#if defined(__LP64__)
2187 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
2188 EXPECT_EQ(0xfedcba12LL, pos);
2189#else
2190 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
2191 EXPECT_EQ(EOVERFLOW, errno);
2192#endif
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002193
2194 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
2195 ASSERT_TRUE(fp64 != nullptr);
2196 fpos64_t pos64;
Elliott Hughes955426e2016-01-26 18:25:52 -08002197 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
2198 EXPECT_EQ(0xfedcba12345678, pos64);
Elliott Hughes03e65eb2016-01-26 14:13:04 -08002199#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002200 GTEST_SKIP() << "glibc uses fopencookie instead";
Elliott Hughes2704bd12016-01-20 17:14:53 -08002201#endif
2202}
Elliott Hughes71288cb2016-01-22 19:22:44 -08002203
2204TEST(STDIO_TEST, lots_of_concurrent_files) {
2205 std::vector<TemporaryFile*> tfs;
2206 std::vector<FILE*> fps;
2207
2208 for (size_t i = 0; i < 256; ++i) {
2209 TemporaryFile* tf = new TemporaryFile;
2210 tfs.push_back(tf);
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002211 FILE* fp = fopen(tf->path, "w+");
Elliott Hughes71288cb2016-01-22 19:22:44 -08002212 fps.push_back(fp);
2213 fprintf(fp, "hello %zu!\n", i);
2214 fflush(fp);
2215 }
2216
2217 for (size_t i = 0; i < 256; ++i) {
Elliott Hughes71288cb2016-01-22 19:22:44 -08002218 char expected[BUFSIZ];
2219 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002220
Elliott Hughes70715da2016-08-01 16:35:17 -07002221 AssertFileIs(fps[i], expected);
Elliott Hughes71288cb2016-01-22 19:22:44 -08002222 fclose(fps[i]);
2223 delete tfs[i];
2224 }
2225}
Elliott Hughes9677fab2016-01-25 15:50:59 -08002226
2227static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
2228 EXPECT_EQ(offset, ftell(fp));
2229 EXPECT_EQ(offset, ftello(fp));
Elliott Hughese4fa6e92016-02-02 22:39:15 -08002230 EXPECT_EQ(offset, ftello64(fp));
Elliott Hughes9677fab2016-01-25 15:50:59 -08002231 fpos_t pos;
2232 fpos64_t pos64;
2233 EXPECT_EQ(0, fgetpos(fp, &pos));
2234 EXPECT_EQ(0, fgetpos64(fp, &pos64));
2235#if defined(__BIONIC__)
2236 EXPECT_EQ(offset, static_cast<off64_t>(pos));
2237 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
2238#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -08002239 GTEST_SKIP() << "glibc's fpos_t is opaque";
Elliott Hughes9677fab2016-01-25 15:50:59 -08002240#endif
2241}
2242
2243TEST(STDIO_TEST, seek_tell_family_smoke) {
2244 TemporaryFile tf;
2245 FILE* fp = fdopen(tf.fd, "w+");
2246
2247 // Initially we should be at 0.
2248 AssertFileOffsetAt(fp, 0);
2249
2250 // Seek to offset 8192.
2251 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
2252 AssertFileOffsetAt(fp, 8192);
2253 fpos_t eight_k_pos;
2254 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
2255
2256 // Seek forward another 8192...
2257 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
2258 AssertFileOffsetAt(fp, 8192 + 8192);
2259 fpos64_t sixteen_k_pos64;
2260 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
2261
2262 // Seek back 8192...
2263 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
2264 AssertFileOffsetAt(fp, 8192);
2265
2266 // Since we haven't written anything, the end is also at 0.
2267 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2268 AssertFileOffsetAt(fp, 0);
2269
2270 // Check that our fpos64_t from 16KiB works...
2271 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
2272 AssertFileOffsetAt(fp, 8192 + 8192);
2273 // ...as does our fpos_t from 8192.
2274 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
2275 AssertFileOffsetAt(fp, 8192);
2276
2277 // Do fseeko and fseeko64 work too?
2278 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
2279 AssertFileOffsetAt(fp, 1234);
2280 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
2281 AssertFileOffsetAt(fp, 5678);
2282
2283 fclose(fp);
2284}
2285
2286TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
2287 TemporaryFile tf;
2288 FILE* fp = fdopen(tf.fd, "w+");
2289
2290 // Bad whence.
2291 errno = 0;
2292 ASSERT_EQ(-1, fseek(fp, 0, 123));
2293 ASSERT_EQ(EINVAL, errno);
2294 errno = 0;
2295 ASSERT_EQ(-1, fseeko(fp, 0, 123));
2296 ASSERT_EQ(EINVAL, errno);
2297 errno = 0;
2298 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
2299 ASSERT_EQ(EINVAL, errno);
2300
2301 // Bad offset.
2302 errno = 0;
2303 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
2304 ASSERT_EQ(EINVAL, errno);
2305 errno = 0;
2306 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
2307 ASSERT_EQ(EINVAL, errno);
2308 errno = 0;
2309 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
2310 ASSERT_EQ(EINVAL, errno);
2311
2312 fclose(fp);
2313}
Elliott Hughes20788ae2016-06-09 15:16:32 -07002314
2315TEST(STDIO_TEST, ctermid) {
2316 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
2317
2318 char buf[L_ctermid] = {};
2319 ASSERT_EQ(buf, ctermid(buf));
2320 ASSERT_STREQ("/dev/tty", buf);
2321}
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002322
2323TEST(STDIO_TEST, remove) {
2324 struct stat sb;
2325
2326 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002327 ASSERT_EQ(0, remove(tf.path));
2328 ASSERT_EQ(-1, lstat(tf.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002329 ASSERT_EQ(ENOENT, errno);
2330
2331 TemporaryDir td;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002332 ASSERT_EQ(0, remove(td.path));
2333 ASSERT_EQ(-1, lstat(td.path, &sb));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002334 ASSERT_EQ(ENOENT, errno);
2335
2336 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002337 ASSERT_EQ(-1, remove(tf.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002338 ASSERT_EQ(ENOENT, errno);
2339
2340 errno = 0;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002341 ASSERT_EQ(-1, remove(td.path));
Elliott Hughesd1f25a72016-08-05 15:53:03 -07002342 ASSERT_EQ(ENOENT, errno);
2343}
Elliott Hughesfb3873d2016-08-10 11:07:54 -07002344
2345TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
2346 char buf[16];
2347 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
2348 testing::KilledBySignal(SIGABRT),
2349#if defined(NOFORTIFY)
2350 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
2351#else
2352 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
2353#endif
2354 );
2355}
2356
2357TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
2358 std::string buf = "world";
2359 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
2360 testing::KilledBySignal(SIGABRT),
2361 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
2362}
2363
2364TEST(STDIO_TEST, sprintf_30445072) {
2365 std::string buf = "world";
2366 sprintf(&buf[0], "hello");
2367 ASSERT_EQ(buf, "hello");
2368}
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002369
Elliott Hughes654cd832018-08-30 16:00:42 -07002370TEST(STDIO_TEST, printf_m) {
2371 char buf[BUFSIZ];
2372 errno = 0;
2373 snprintf(buf, sizeof(buf), "<%m>");
2374 ASSERT_STREQ("<Success>", buf);
2375 errno = -1;
2376 snprintf(buf, sizeof(buf), "<%m>");
2377 ASSERT_STREQ("<Unknown error -1>", buf);
2378 errno = EINVAL;
2379 snprintf(buf, sizeof(buf), "<%m>");
2380 ASSERT_STREQ("<Invalid argument>", buf);
2381}
2382
Elliott Hughesf340a562018-09-06 10:42:40 -07002383TEST(STDIO_TEST, printf_m_does_not_clobber_strerror) {
2384 char buf[BUFSIZ];
2385 const char* m = strerror(-1);
2386 ASSERT_STREQ("Unknown error -1", m);
2387 errno = -2;
2388 snprintf(buf, sizeof(buf), "<%m>");
2389 ASSERT_STREQ("<Unknown error -2>", buf);
2390 ASSERT_STREQ("Unknown error -1", m);
2391}
2392
Elliott Hughes654cd832018-08-30 16:00:42 -07002393TEST(STDIO_TEST, wprintf_m) {
2394 wchar_t buf[BUFSIZ];
2395 errno = 0;
2396 swprintf(buf, sizeof(buf), L"<%m>");
2397 ASSERT_EQ(std::wstring(L"<Success>"), buf);
2398 errno = -1;
2399 swprintf(buf, sizeof(buf), L"<%m>");
2400 ASSERT_EQ(std::wstring(L"<Unknown error -1>"), buf);
2401 errno = EINVAL;
2402 swprintf(buf, sizeof(buf), L"<%m>");
2403 ASSERT_EQ(std::wstring(L"<Invalid argument>"), buf);
2404}
2405
Elliott Hughesf340a562018-09-06 10:42:40 -07002406TEST(STDIO_TEST, wprintf_m_does_not_clobber_strerror) {
2407 wchar_t buf[BUFSIZ];
2408 const char* m = strerror(-1);
2409 ASSERT_STREQ("Unknown error -1", m);
2410 errno = -2;
2411 swprintf(buf, sizeof(buf), L"<%m>");
2412 ASSERT_EQ(std::wstring(L"<Unknown error -2>"), buf);
2413 ASSERT_STREQ("Unknown error -1", m);
2414}
2415
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002416TEST(STDIO_TEST, fopen_append_mode_and_ftell) {
2417 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002418 SetFileTo(tf.path, "0123456789");
2419 FILE* fp = fopen(tf.path, "a");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002420 EXPECT_EQ(10, ftell(fp));
2421 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2422 EXPECT_EQ(2, ftell(fp));
2423 ASSERT_NE(EOF, fputs("xxx", fp));
2424 ASSERT_EQ(0, fflush(fp));
2425 EXPECT_EQ(13, ftell(fp));
2426 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2427 EXPECT_EQ(13, ftell(fp));
2428 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002429 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002430}
2431
2432TEST(STDIO_TEST, fdopen_append_mode_and_ftell) {
2433 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002434 SetFileTo(tf.path, "0123456789");
2435 int fd = open(tf.path, O_RDWR);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002436 ASSERT_NE(-1, fd);
2437 // POSIX: "The file position indicator associated with the new stream is set to the position
2438 // indicated by the file offset associated with the file descriptor."
2439 ASSERT_EQ(4, lseek(fd, 4, SEEK_SET));
2440 FILE* fp = fdopen(fd, "a");
2441 EXPECT_EQ(4, ftell(fp));
2442 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2443 EXPECT_EQ(2, ftell(fp));
2444 ASSERT_NE(EOF, fputs("xxx", fp));
2445 ASSERT_EQ(0, fflush(fp));
2446 EXPECT_EQ(13, ftell(fp));
2447 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2448 EXPECT_EQ(13, ftell(fp));
2449 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002450 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002451}
2452
2453TEST(STDIO_TEST, freopen_append_mode_and_ftell) {
2454 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002455 SetFileTo(tf.path, "0123456789");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002456 FILE* other_fp = fopen("/proc/version", "r");
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002457 FILE* fp = freopen(tf.path, "a", other_fp);
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002458 EXPECT_EQ(10, ftell(fp));
2459 ASSERT_EQ(0, fseek(fp, 2, SEEK_SET));
2460 EXPECT_EQ(2, ftell(fp));
2461 ASSERT_NE(EOF, fputs("xxx", fp));
2462 ASSERT_EQ(0, fflush(fp));
2463 EXPECT_EQ(13, ftell(fp));
2464 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2465 EXPECT_EQ(13, ftell(fp));
2466 ASSERT_EQ(0, fclose(fp));
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002467 AssertFileIs(tf.path, "0123456789xxx");
Elliott Hughes33a8cb12017-07-25 18:06:46 -07002468}
Elliott Hughesb15feb72017-07-31 17:20:18 -07002469
2470TEST(STDIO_TEST, constants) {
2471 ASSERT_LE(FILENAME_MAX, PATH_MAX);
2472 ASSERT_EQ(L_tmpnam, PATH_MAX);
2473}
Elliott Hughes37ad9592017-10-30 17:47:12 -07002474
2475TEST(STDIO_TEST, perror) {
2476 ExecTestHelper eth;
2477 eth.Run([&]() { errno = EINVAL; perror("a b c"); exit(0); }, 0, "a b c: Invalid argument\n");
2478 eth.Run([&]() { errno = EINVAL; perror(nullptr); exit(0); }, 0, "Invalid argument\n");
2479 eth.Run([&]() { errno = EINVAL; perror(""); exit(0); }, 0, "Invalid argument\n");
2480}
2481
2482TEST(STDIO_TEST, puts) {
2483 ExecTestHelper eth;
2484 eth.Run([&]() { exit(puts("a b c")); }, 0, "a b c\n");
2485}
2486
2487TEST(STDIO_TEST, unlocked) {
2488 TemporaryFile tf;
2489
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002490 FILE* fp = fopen(tf.path, "w+");
Elliott Hughes37ad9592017-10-30 17:47:12 -07002491 ASSERT_TRUE(fp != nullptr);
2492
2493 clearerr_unlocked(fp);
2494 ASSERT_FALSE(feof_unlocked(fp));
2495 ASSERT_FALSE(ferror_unlocked(fp));
2496
2497 ASSERT_EQ(fileno(fp), fileno_unlocked(fp));
2498
2499 ASSERT_NE(EOF, putc_unlocked('a', fp));
2500 ASSERT_NE(EOF, putc('b', fp));
2501 ASSERT_NE(EOF, fputc_unlocked('c', fp));
2502 ASSERT_NE(EOF, fputc('d', fp));
2503
2504 rewind(fp);
2505 ASSERT_EQ('a', getc_unlocked(fp));
2506 ASSERT_EQ('b', getc(fp));
2507 ASSERT_EQ('c', fgetc_unlocked(fp));
2508 ASSERT_EQ('d', fgetc(fp));
2509
2510 rewind(fp);
2511 ASSERT_EQ(2U, fwrite_unlocked("AB", 1, 2, fp));
2512 ASSERT_EQ(2U, fwrite("CD", 1, 2, fp));
2513 ASSERT_EQ(0, fflush_unlocked(fp));
2514
2515 rewind(fp);
2516 char buf[BUFSIZ] = {};
2517 ASSERT_EQ(2U, fread_unlocked(&buf[0], 1, 2, fp));
2518 ASSERT_EQ(2U, fread(&buf[2], 1, 2, fp));
2519 ASSERT_STREQ("ABCD", buf);
2520
2521 rewind(fp);
2522 ASSERT_NE(EOF, fputs("hello ", fp));
2523 ASSERT_NE(EOF, fputs_unlocked("world", fp));
2524 ASSERT_NE(EOF, fputc('\n', fp));
2525
2526 rewind(fp);
2527 ASSERT_TRUE(fgets_unlocked(buf, sizeof(buf), fp) != nullptr);
2528 ASSERT_STREQ("hello world\n", buf);
2529
2530 ASSERT_EQ(0, fclose(fp));
2531}
Ryan Prichardbf549862017-11-07 15:30:32 -08002532
2533TEST(STDIO_TEST, fseek_64bit) {
2534 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002535 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002536 ASSERT_TRUE(fp != nullptr);
2537 ASSERT_EQ(0, fseeko64(fp, 0x2'0000'0000, SEEK_SET));
2538 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2539 ASSERT_EQ(0, fseeko64(fp, 0x1'0000'0000, SEEK_CUR));
2540 ASSERT_EQ(0x3'0000'0000, ftello64(fp));
2541 ASSERT_EQ(0, fclose(fp));
2542}
2543
2544// POSIX requires that fseek/fseeko fail with EOVERFLOW if the new file offset
2545// isn't representable in long/off_t.
2546TEST(STDIO_TEST, fseek_overflow_32bit) {
2547 TemporaryFile tf;
Mark Salyzyn68a3bcc2018-11-13 07:35:21 -08002548 FILE* fp = fopen64(tf.path, "w+");
Ryan Prichardbf549862017-11-07 15:30:32 -08002549 ASSERT_EQ(0, ftruncate64(fileno(fp), 0x2'0000'0000));
2550
2551 // Bionic implements overflow checking for SEEK_CUR, but glibc doesn't.
2552#if defined(__BIONIC__) && !defined(__LP64__)
2553 ASSERT_EQ(0, fseek(fp, 0x7fff'ffff, SEEK_SET));
2554 ASSERT_EQ(-1, fseek(fp, 1, SEEK_CUR));
2555 ASSERT_EQ(EOVERFLOW, errno);
2556#endif
2557
2558 // Neither Bionic nor glibc implement the overflow checking for SEEK_END.
2559 // (Aside: FreeBSD's libc is an example of a libc that checks both SEEK_CUR
2560 // and SEEK_END -- many C libraries check neither.)
2561 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
2562 ASSERT_EQ(0x2'0000'0000, ftello64(fp));
2563
2564 fclose(fp);
2565}
Elliott Hughesf1a38382018-08-22 15:38:07 -07002566
2567TEST(STDIO_TEST, dev_std_files) {
2568 // POSIX only mentions /dev/stdout, but we should have all three (http://b/31824379).
2569 char path[PATH_MAX];
Christopher Ferris2c4ec7e2018-08-23 11:17:55 -07002570 ssize_t length = readlink("/dev/stdin", path, sizeof(path));
2571 ASSERT_LT(0, length);
2572 ASSERT_EQ("/proc/self/fd/0", std::string(path, length));
2573
2574 length = readlink("/dev/stdout", path, sizeof(path));
2575 ASSERT_LT(0, length);
2576 ASSERT_EQ("/proc/self/fd/1", std::string(path, length));
2577
2578 length = readlink("/dev/stderr", path, sizeof(path));
2579 ASSERT_LT(0, length);
2580 ASSERT_EQ("/proc/self/fd/2", std::string(path, length));
Elliott Hughesf1a38382018-08-22 15:38:07 -07002581}
Ryan Prichardc485cdb2019-04-30 14:47:34 -07002582
2583TEST(STDIO_TEST, fread_with_locked_file) {
2584 // Reading an unbuffered/line-buffered file from one thread shouldn't block on
2585 // files locked on other threads, even if it flushes some line-buffered files.
2586 FILE* fp1 = fopen("/dev/zero", "r");
2587 ASSERT_TRUE(fp1 != nullptr);
2588 flockfile(fp1);
2589
2590 std::thread([] {
2591 for (int mode : { _IONBF, _IOLBF }) {
2592 FILE* fp2 = fopen("/dev/zero", "r");
2593 ASSERT_TRUE(fp2 != nullptr);
2594 setvbuf(fp2, nullptr, mode, 0);
2595 ASSERT_EQ('\0', fgetc(fp2));
2596 fclose(fp2);
2597 }
2598 }).join();
2599
2600 funlockfile(fp1);
2601 fclose(fp1);
2602}
Elliott Hughes31c73092019-05-07 10:03:02 -07002603
2604TEST(STDIO_TEST, SEEK_macros) {
2605 ASSERT_EQ(0, SEEK_SET);
2606 ASSERT_EQ(1, SEEK_CUR);
2607 ASSERT_EQ(2, SEEK_END);
2608 ASSERT_EQ(3, SEEK_DATA);
2609 ASSERT_EQ(4, SEEK_HOLE);
2610 // So we'll notice if Linux grows another constant in <linux/fs.h>...
2611 ASSERT_EQ(SEEK_MAX, SEEK_HOLE);
2612}