2 * Copyright (C) 2012 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
24 #include <sys/types.h>
32 #include "BionicDeathTest.h"
33 #include "TemporaryFile.h"
36 #if defined(NOFORTIFY)
37 #define STDIO_TEST stdio_nofortify
38 #define STDIO_DEATHTEST stdio_nofortify_DeathTest
40 #define STDIO_TEST stdio
41 #define STDIO_DEATHTEST stdio_DeathTest
44 class stdio_DeathTest : public BionicDeathTest {};
45 class stdio_nofortify_DeathTest : public BionicDeathTest {};
47 static void AssertFileIs(FILE* fp, const char* expected, bool is_fmemopen = false) {
51 memset(line, 0xff, sizeof(line));
52 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
53 ASSERT_STREQ(expected, line);
56 // fmemopen appends a trailing NUL byte, which probably shouldn't show up as an
57 // extra empty line, but does on every C library I tested...
58 ASSERT_EQ(line, fgets(line, sizeof(line), fp));
59 ASSERT_STREQ("", line);
62 // Make sure there isn't anything else in the file.
63 ASSERT_EQ(nullptr, fgets(line, sizeof(line), fp)) << "junk at end of file: " << line;
66 TEST(STDIO_TEST, flockfile_18208568_stderr) {
67 // Check that we have a _recursive_ mutex for flockfile.
69 feof(stderr); // We don't care about the result, but this needs to take the lock.
73 TEST(STDIO_TEST, flockfile_18208568_regular) {
74 // We never had a bug for streams other than stdin/stdout/stderr, but test anyway.
75 FILE* fp = fopen("/dev/null", "w");
76 ASSERT_TRUE(fp != NULL);
83 TEST(STDIO_TEST, tmpfile_fileno_fprintf_rewind_fgets) {
85 ASSERT_TRUE(fp != NULL);
91 int rc = fstat(fd, &sb);
93 ASSERT_EQ(sb.st_mode & 0777, 0600U);
95 rc = fprintf(fp, "hello\n");
98 AssertFileIs(fp, "hello\n");
102 TEST(STDIO_TEST, tmpfile64) {
103 FILE* fp = tmpfile64();
104 ASSERT_TRUE(fp != nullptr);
108 TEST(STDIO_TEST, dprintf) {
111 int rc = dprintf(tf.fd, "hello\n");
114 lseek(tf.fd, 0, SEEK_SET);
115 FILE* tfile = fdopen(tf.fd, "r");
116 ASSERT_TRUE(tfile != NULL);
118 AssertFileIs(tfile, "hello\n");
122 TEST(STDIO_TEST, getdelim) {
123 FILE* fp = tmpfile();
124 ASSERT_TRUE(fp != NULL);
126 const char* line_written = "This is a test";
127 int rc = fprintf(fp, "%s", line_written);
128 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
132 char* word_read = NULL;
133 size_t allocated_length = 0;
135 const char* expected[] = { "This ", " ", "is ", "a ", "test" };
136 for (size_t i = 0; i < 5; ++i) {
137 ASSERT_FALSE(feof(fp));
138 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), static_cast<int>(strlen(expected[i])));
139 ASSERT_GE(allocated_length, strlen(expected[i]));
140 ASSERT_STREQ(expected[i], word_read);
142 // The last read should have set the end-of-file indicator for the stream.
143 ASSERT_TRUE(feof(fp));
146 // getdelim returns -1 but doesn't set errno if we're already at EOF.
147 // It should set the end-of-file indicator for the stream, though.
149 ASSERT_EQ(getdelim(&word_read, &allocated_length, ' ', fp), -1);
151 ASSERT_TRUE(feof(fp));
157 TEST(STDIO_TEST, getdelim_invalid) {
158 FILE* fp = tmpfile();
159 ASSERT_TRUE(fp != NULL);
162 size_t buffer_length = 0;
164 // The first argument can't be NULL.
166 ASSERT_EQ(getdelim(NULL, &buffer_length, ' ', fp), -1);
167 ASSERT_EQ(EINVAL, errno);
169 // The second argument can't be NULL.
171 ASSERT_EQ(getdelim(&buffer, NULL, ' ', fp), -1);
172 ASSERT_EQ(EINVAL, errno);
174 // The underlying fd can't be closed.
175 ASSERT_EQ(0, close(fileno(fp)));
177 ASSERT_EQ(getdelim(&buffer, &buffer_length, ' ', fp), -1);
178 ASSERT_EQ(EBADF, errno);
182 TEST(STDIO_TEST, getdelim_directory) {
183 FILE* fp = fopen("/proc", "r");
184 ASSERT_TRUE(fp != NULL);
186 size_t allocated_length;
187 ASSERT_EQ(-1, getdelim(&word_read, &allocated_length, ' ', fp));
191 TEST(STDIO_TEST, getline) {
192 FILE* fp = tmpfile();
193 ASSERT_TRUE(fp != NULL);
195 const char* line_written = "This is a test for getline\n";
196 const size_t line_count = 5;
198 for (size_t i = 0; i < line_count; ++i) {
199 int rc = fprintf(fp, "%s", line_written);
200 ASSERT_EQ(rc, static_cast<int>(strlen(line_written)));
205 char* line_read = NULL;
206 size_t allocated_length = 0;
208 size_t read_line_count = 0;
209 ssize_t read_char_count;
210 while ((read_char_count = getline(&line_read, &allocated_length, fp)) != -1) {
211 ASSERT_EQ(read_char_count, static_cast<int>(strlen(line_written)));
212 ASSERT_GE(allocated_length, strlen(line_written));
213 ASSERT_STREQ(line_written, line_read);
216 ASSERT_EQ(read_line_count, line_count);
218 // The last read should have set the end-of-file indicator for the stream.
219 ASSERT_TRUE(feof(fp));
222 // getline returns -1 but doesn't set errno if we're already at EOF.
223 // It should set the end-of-file indicator for the stream, though.
225 ASSERT_EQ(getline(&line_read, &allocated_length, fp), -1);
227 ASSERT_TRUE(feof(fp));
233 TEST(STDIO_TEST, getline_invalid) {
234 FILE* fp = tmpfile();
235 ASSERT_TRUE(fp != NULL);
238 size_t buffer_length = 0;
240 // The first argument can't be NULL.
242 ASSERT_EQ(getline(NULL, &buffer_length, fp), -1);
243 ASSERT_EQ(EINVAL, errno);
245 // The second argument can't be NULL.
247 ASSERT_EQ(getline(&buffer, NULL, fp), -1);
248 ASSERT_EQ(EINVAL, errno);
250 // The underlying fd can't be closed.
251 ASSERT_EQ(0, close(fileno(fp)));
253 ASSERT_EQ(getline(&buffer, &buffer_length, fp), -1);
254 ASSERT_EQ(EBADF, errno);
258 TEST(STDIO_TEST, printf_ssize_t) {
260 ASSERT_EQ(sizeof(ssize_t), sizeof(long int));
261 ASSERT_EQ(sizeof(ssize_t), sizeof(size_t));
262 // For our 32-bit ABI, we had a ssize_t definition that confuses GCC into saying:
263 // error: format '%zd' expects argument of type 'signed size_t',
264 // but argument 4 has type 'ssize_t {aka long int}' [-Werror=format]
267 snprintf(buf, sizeof(buf), "%zd", v);
270 // https://code.google.com/p/android/issues/detail?id=64886
271 TEST(STDIO_TEST, snprintf_a) {
273 EXPECT_EQ(23, snprintf(buf, sizeof(buf), "<%a>", 9990.235));
274 EXPECT_STREQ("<0x1.3831e147ae148p+13>", buf);
277 TEST(STDIO_TEST, snprintf_lc) {
280 EXPECT_EQ(3, snprintf(buf, sizeof(buf), "<%lc>", wc));
281 EXPECT_STREQ("<a>", buf);
284 TEST(STDIO_TEST, snprintf_ls) {
287 EXPECT_EQ(8, snprintf(buf, sizeof(buf), "<%ls>", ws));
288 EXPECT_STREQ("<(null)>", buf);
290 wchar_t chars[] = { L'h', L'i', 0 };
292 EXPECT_EQ(4, snprintf(buf, sizeof(buf), "<%ls>", ws));
293 EXPECT_STREQ("<hi>", buf);
296 TEST(STDIO_TEST, snprintf_n) {
297 #if defined(__BIONIC__)
301 EXPECT_EQ(5, snprintf(buf, sizeof(buf), "a %n b", &i));
303 EXPECT_STREQ("a n b", buf);
305 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
309 TEST(STDIO_TEST, snprintf_smoke) {
312 snprintf(buf, sizeof(buf), "a");
313 EXPECT_STREQ("a", buf);
315 snprintf(buf, sizeof(buf), "%%");
316 EXPECT_STREQ("%", buf);
318 snprintf(buf, sizeof(buf), "01234");
319 EXPECT_STREQ("01234", buf);
321 snprintf(buf, sizeof(buf), "a%sb", "01234");
322 EXPECT_STREQ("a01234b", buf);
325 snprintf(buf, sizeof(buf), "a%sb", s);
326 EXPECT_STREQ("a(null)b", buf);
328 snprintf(buf, sizeof(buf), "aa%scc", "bb");
329 EXPECT_STREQ("aabbcc", buf);
331 snprintf(buf, sizeof(buf), "a%cc", 'b');
332 EXPECT_STREQ("abc", buf);
334 snprintf(buf, sizeof(buf), "a%db", 1234);
335 EXPECT_STREQ("a1234b", buf);
337 snprintf(buf, sizeof(buf), "a%db", -8123);
338 EXPECT_STREQ("a-8123b", buf);
340 snprintf(buf, sizeof(buf), "a%hdb", static_cast<short>(0x7fff0010));
341 EXPECT_STREQ("a16b", buf);
343 snprintf(buf, sizeof(buf), "a%hhdb", static_cast<char>(0x7fffff10));
344 EXPECT_STREQ("a16b", buf);
346 snprintf(buf, sizeof(buf), "a%lldb", 0x1000000000LL);
347 EXPECT_STREQ("a68719476736b", buf);
349 snprintf(buf, sizeof(buf), "a%ldb", 70000L);
350 EXPECT_STREQ("a70000b", buf);
352 snprintf(buf, sizeof(buf), "a%pb", reinterpret_cast<void*>(0xb0001234));
353 EXPECT_STREQ("a0xb0001234b", buf);
355 snprintf(buf, sizeof(buf), "a%xz", 0x12ab);
356 EXPECT_STREQ("a12abz", buf);
358 snprintf(buf, sizeof(buf), "a%Xz", 0x12ab);
359 EXPECT_STREQ("a12ABz", buf);
361 snprintf(buf, sizeof(buf), "a%08xz", 0x123456);
362 EXPECT_STREQ("a00123456z", buf);
364 snprintf(buf, sizeof(buf), "a%5dz", 1234);
365 EXPECT_STREQ("a 1234z", buf);
367 snprintf(buf, sizeof(buf), "a%05dz", 1234);
368 EXPECT_STREQ("a01234z", buf);
370 snprintf(buf, sizeof(buf), "a%8dz", 1234);
371 EXPECT_STREQ("a 1234z", buf);
373 snprintf(buf, sizeof(buf), "a%-8dz", 1234);
374 EXPECT_STREQ("a1234 z", buf);
376 snprintf(buf, sizeof(buf), "A%-11sZ", "abcdef");
377 EXPECT_STREQ("Aabcdef Z", buf);
379 snprintf(buf, sizeof(buf), "A%s:%dZ", "hello", 1234);
380 EXPECT_STREQ("Ahello:1234Z", buf);
382 snprintf(buf, sizeof(buf), "a%03d:%d:%02dz", 5, 5, 5);
383 EXPECT_STREQ("a005:5:05z", buf);
386 snprintf(buf, sizeof(buf), "a%d,%pz", 5, p);
387 #if defined(__BIONIC__)
388 EXPECT_STREQ("a5,0x0z", buf);
390 EXPECT_STREQ("a5,(nil)z", buf);
393 snprintf(buf, sizeof(buf), "a%lld,%d,%d,%dz", 0x1000000000LL, 6, 7, 8);
394 EXPECT_STREQ("a68719476736,6,7,8z", buf);
396 snprintf(buf, sizeof(buf), "a_%f_b", 1.23f);
397 EXPECT_STREQ("a_1.230000_b", buf);
399 snprintf(buf, sizeof(buf), "a_%g_b", 3.14);
400 EXPECT_STREQ("a_3.14_b", buf);
402 snprintf(buf, sizeof(buf), "%1$s %1$s", "print_me_twice");
403 EXPECT_STREQ("print_me_twice print_me_twice", buf);
406 template <typename T>
407 static void CheckInfNan(int snprintf_fn(T*, size_t, const T*, ...),
408 int sscanf_fn(const T*, const T*, ...),
409 const T* fmt_string, const T* fmt, const T* fmt_plus,
410 const T* minus_inf, const T* inf_, const T* plus_inf,
411 const T* minus_nan, const T* nan_, const T* plus_nan) {
417 snprintf_fn(buf, sizeof(buf), fmt, nanf(""));
418 EXPECT_STREQ(nan_, buf) << fmt;
419 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
420 EXPECT_TRUE(isnan(f));
422 snprintf_fn(buf, sizeof(buf), fmt, -nanf(""));
423 EXPECT_STREQ(minus_nan, buf) << fmt;
424 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
425 EXPECT_TRUE(isnan(f));
427 snprintf_fn(buf, sizeof(buf), fmt_plus, nanf(""));
428 EXPECT_STREQ(plus_nan, buf) << fmt_plus;
429 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
430 EXPECT_TRUE(isnan(f));
432 snprintf_fn(buf, sizeof(buf), fmt_plus, -nanf(""));
433 EXPECT_STREQ(minus_nan, buf) << fmt_plus;
434 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
435 EXPECT_TRUE(isnan(f));
439 snprintf_fn(buf, sizeof(buf), fmt, HUGE_VALF);
440 EXPECT_STREQ(inf_, buf) << fmt;
441 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
442 EXPECT_EQ(HUGE_VALF, f);
444 snprintf_fn(buf, sizeof(buf), fmt, -HUGE_VALF);
445 EXPECT_STREQ(minus_inf, buf) << fmt;
446 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
447 EXPECT_EQ(-HUGE_VALF, f);
449 snprintf_fn(buf, sizeof(buf), fmt_plus, HUGE_VALF);
450 EXPECT_STREQ(plus_inf, buf) << fmt_plus;
451 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
452 EXPECT_EQ(HUGE_VALF, f);
454 snprintf_fn(buf, sizeof(buf), fmt_plus, -HUGE_VALF);
455 EXPECT_STREQ(minus_inf, buf) << fmt_plus;
456 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f));
457 EXPECT_EQ(-HUGE_VALF, f);
459 // Check case-insensitivity.
460 snprintf_fn(buf, sizeof(buf), fmt_string, "[InFiNiTy]");
461 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
462 EXPECT_EQ(HUGE_VALF, f);
463 snprintf_fn(buf, sizeof(buf), fmt_string, "[NaN]");
464 EXPECT_EQ(1, sscanf_fn(buf, fmt, &f)) << buf;
465 EXPECT_TRUE(isnan(f));
468 TEST(STDIO_TEST, snprintf_sscanf_inf_nan) {
469 CheckInfNan(snprintf, sscanf, "%s",
471 "[-inf]", "[inf]", "[+inf]",
472 "[-nan]", "[nan]", "[+nan]");
473 CheckInfNan(snprintf, sscanf, "%s",
475 "[-INF]", "[INF]", "[+INF]",
476 "[-NAN]", "[NAN]", "[+NAN]");
477 CheckInfNan(snprintf, sscanf, "%s",
479 "[-inf]", "[inf]", "[+inf]",
480 "[-nan]", "[nan]", "[+nan]");
481 CheckInfNan(snprintf, sscanf, "%s",
483 "[-INF]", "[INF]", "[+INF]",
484 "[-NAN]", "[NAN]", "[+NAN]");
485 CheckInfNan(snprintf, sscanf, "%s",
487 "[-inf]", "[inf]", "[+inf]",
488 "[-nan]", "[nan]", "[+nan]");
489 CheckInfNan(snprintf, sscanf, "%s",
491 "[-INF]", "[INF]", "[+INF]",
492 "[-NAN]", "[NAN]", "[+NAN]");
493 CheckInfNan(snprintf, sscanf, "%s",
495 "[-inf]", "[inf]", "[+inf]",
496 "[-nan]", "[nan]", "[+nan]");
497 CheckInfNan(snprintf, sscanf, "%s",
499 "[-INF]", "[INF]", "[+INF]",
500 "[-NAN]", "[NAN]", "[+NAN]");
503 TEST(STDIO_TEST, swprintf_swscanf_inf_nan) {
504 CheckInfNan(swprintf, swscanf, L"%s",
506 L"[-inf]", L"[inf]", L"[+inf]",
507 L"[-nan]", L"[nan]", L"[+nan]");
508 CheckInfNan(swprintf, swscanf, L"%s",
510 L"[-INF]", L"[INF]", L"[+INF]",
511 L"[-NAN]", L"[NAN]", L"[+NAN]");
512 CheckInfNan(swprintf, swscanf, L"%s",
514 L"[-inf]", L"[inf]", L"[+inf]",
515 L"[-nan]", L"[nan]", L"[+nan]");
516 CheckInfNan(swprintf, swscanf, L"%s",
518 L"[-INF]", L"[INF]", L"[+INF]",
519 L"[-NAN]", L"[NAN]", L"[+NAN]");
520 CheckInfNan(swprintf, swscanf, L"%s",
522 L"[-inf]", L"[inf]", L"[+inf]",
523 L"[-nan]", L"[nan]", L"[+nan]");
524 CheckInfNan(swprintf, swscanf, L"%s",
526 L"[-INF]", L"[INF]", L"[+INF]",
527 L"[-NAN]", L"[NAN]", L"[+NAN]");
528 CheckInfNan(swprintf, swscanf, L"%s",
530 L"[-inf]", L"[inf]", L"[+inf]",
531 L"[-nan]", L"[nan]", L"[+nan]");
532 CheckInfNan(swprintf, swscanf, L"%s",
534 L"[-INF]", L"[INF]", L"[+INF]",
535 L"[-NAN]", L"[NAN]", L"[+NAN]");
538 TEST(STDIO_TEST, snprintf_d_INT_MAX) {
540 snprintf(buf, sizeof(buf), "%d", INT_MAX);
541 EXPECT_STREQ("2147483647", buf);
544 TEST(STDIO_TEST, snprintf_d_INT_MIN) {
546 snprintf(buf, sizeof(buf), "%d", INT_MIN);
547 EXPECT_STREQ("-2147483648", buf);
550 TEST(STDIO_TEST, snprintf_ld_LONG_MAX) {
552 snprintf(buf, sizeof(buf), "%ld", LONG_MAX);
553 #if defined(__LP64__)
554 EXPECT_STREQ("9223372036854775807", buf);
556 EXPECT_STREQ("2147483647", buf);
560 TEST(STDIO_TEST, snprintf_ld_LONG_MIN) {
562 snprintf(buf, sizeof(buf), "%ld", LONG_MIN);
563 #if defined(__LP64__)
564 EXPECT_STREQ("-9223372036854775808", buf);
566 EXPECT_STREQ("-2147483648", buf);
570 TEST(STDIO_TEST, snprintf_lld_LLONG_MAX) {
572 snprintf(buf, sizeof(buf), "%lld", LLONG_MAX);
573 EXPECT_STREQ("9223372036854775807", buf);
576 TEST(STDIO_TEST, snprintf_lld_LLONG_MIN) {
578 snprintf(buf, sizeof(buf), "%lld", LLONG_MIN);
579 EXPECT_STREQ("-9223372036854775808", buf);
582 TEST(STDIO_TEST, snprintf_e) {
585 snprintf(buf, sizeof(buf), "%e", 1.5);
586 EXPECT_STREQ("1.500000e+00", buf);
588 snprintf(buf, sizeof(buf), "%Le", 1.5l);
589 EXPECT_STREQ("1.500000e+00", buf);
592 TEST(STDIO_TEST, snprintf_negative_zero_5084292) {
595 snprintf(buf, sizeof(buf), "%e", -0.0);
596 EXPECT_STREQ("-0.000000e+00", buf);
597 snprintf(buf, sizeof(buf), "%E", -0.0);
598 EXPECT_STREQ("-0.000000E+00", buf);
599 snprintf(buf, sizeof(buf), "%f", -0.0);
600 EXPECT_STREQ("-0.000000", buf);
601 snprintf(buf, sizeof(buf), "%F", -0.0);
602 EXPECT_STREQ("-0.000000", buf);
603 snprintf(buf, sizeof(buf), "%g", -0.0);
604 EXPECT_STREQ("-0", buf);
605 snprintf(buf, sizeof(buf), "%G", -0.0);
606 EXPECT_STREQ("-0", buf);
607 snprintf(buf, sizeof(buf), "%a", -0.0);
608 EXPECT_STREQ("-0x0p+0", buf);
609 snprintf(buf, sizeof(buf), "%A", -0.0);
610 EXPECT_STREQ("-0X0P+0", buf);
613 TEST(STDIO_TEST, snprintf_utf8_15439554) {
614 locale_t cloc = newlocale(LC_ALL, "C.UTF-8", 0);
615 locale_t old_locale = uselocale(cloc);
621 snprintf(buf, sizeof(buf), "%dx%d", 1, 2);
622 EXPECT_STREQ("1x2", buf);
624 snprintf(buf, sizeof(buf), "%d\xc2\xa2%d", 1, 2);
625 EXPECT_STREQ("1¢2", buf);
627 snprintf(buf, sizeof(buf), "%d\xe2\x82\xac%d", 1, 2);
628 EXPECT_STREQ("1€2", buf);
630 snprintf(buf, sizeof(buf), "%d\xf0\xa4\xad\xa2%d", 1, 2);
631 EXPECT_STREQ("1ð¤¢2", buf);
633 uselocale(old_locale);
637 static void* snprintf_small_stack_fn(void*) {
638 // Make life (realistically) hard for ourselves by allocating our own buffer for the result.
640 snprintf(buf, sizeof(buf), "/proc/%d", getpid());
644 TEST(STDIO_TEST, snprintf_small_stack) {
645 // Is it safe to call snprintf on a thread with a small stack?
646 // (The snprintf implementation puts some pretty large buffers on the stack.)
648 ASSERT_EQ(0, pthread_attr_init(&a));
649 ASSERT_EQ(0, pthread_attr_setstacksize(&a, PTHREAD_STACK_MIN));
652 ASSERT_EQ(0, pthread_create(&t, &a, snprintf_small_stack_fn, nullptr));
653 ASSERT_EQ(0, pthread_join(t, nullptr));
656 TEST(STDIO_TEST, snprintf_asterisk_overflow) {
658 ASSERT_EQ(5, snprintf(buf, sizeof(buf), "%.*s%c", 4, "hello world", '!'));
659 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX/2, "hello world", '!'));
660 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX-1, "hello world", '!'));
661 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", INT_MAX, "hello world", '!'));
662 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.*s%c", -1, "hello world", '!'));
664 // INT_MAX-1, INT_MAX, INT_MAX+1.
665 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483646s%c", "hello world", '!'));
666 ASSERT_EQ(12, snprintf(buf, sizeof(buf), "%.2147483647s%c", "hello world", '!'));
667 ASSERT_EQ(-1, snprintf(buf, sizeof(buf), "%.2147483648s%c", "hello world", '!'));
668 ASSERT_EQ(ENOMEM, errno);
671 TEST(STDIO_TEST, fprintf) {
674 FILE* tfile = fdopen(tf.fd, "r+");
675 ASSERT_TRUE(tfile != nullptr);
677 ASSERT_EQ(7, fprintf(tfile, "%d %s", 123, "abc"));
678 AssertFileIs(tfile, "123 abc");
682 TEST(STDIO_TEST, fprintf_failures_7229520) {
686 // Unbuffered case where the fprintf(3) itself fails.
687 ASSERT_NE(nullptr, fp = tmpfile());
689 ASSERT_EQ(4, fprintf(fp, "epic"));
690 ASSERT_EQ(0, close(fileno(fp)));
691 ASSERT_EQ(-1, fprintf(fp, "fail"));
692 ASSERT_EQ(-1, fclose(fp));
694 // Buffered case where we won't notice until the fclose(3).
695 // It's likely this is what was actually seen in http://b/7229520,
696 // and that expecting fprintf to fail is setting yourself up for
697 // disappointment. Remember to check fclose(3)'s return value, kids!
698 ASSERT_NE(nullptr, fp = tmpfile());
699 ASSERT_EQ(4, fprintf(fp, "epic"));
700 ASSERT_EQ(0, close(fileno(fp)));
701 ASSERT_EQ(4, fprintf(fp, "fail"));
702 ASSERT_EQ(-1, fclose(fp));
705 TEST(STDIO_TEST, popen) {
706 FILE* fp = popen("cat /proc/version", "r");
707 ASSERT_TRUE(fp != NULL);
710 char* s = fgets(buf, sizeof(buf), fp);
712 ASSERT_STREQ("Linux version", s);
714 ASSERT_EQ(0, pclose(fp));
717 TEST(STDIO_TEST, getc) {
718 FILE* fp = fopen("/proc/version", "r");
719 ASSERT_TRUE(fp != NULL);
720 ASSERT_EQ('L', getc(fp));
721 ASSERT_EQ('i', getc(fp));
722 ASSERT_EQ('n', getc(fp));
723 ASSERT_EQ('u', getc(fp));
724 ASSERT_EQ('x', getc(fp));
728 TEST(STDIO_TEST, putc) {
729 FILE* fp = fopen("/proc/version", "r");
730 ASSERT_TRUE(fp != NULL);
731 ASSERT_EQ(EOF, putc('x', fp));
735 TEST(STDIO_TEST, sscanf_swscanf) {
744 ASSERT_STREQ("hello", s1);
746 ASSERT_DOUBLE_EQ(1.23, d1);
747 ASSERT_FLOAT_EQ(9.0f, f1);
748 ASSERT_STREQ("world", s2);
752 memset(&s, 0, sizeof(s));
753 ASSERT_EQ(5, sscanf(" hello 123 1.23 0x1.2p3 world",
755 s.s1, &s.i1, &s.d1, &s.f1, s.s2));
758 memset(&s, 0, sizeof(s));
759 ASSERT_EQ(5, swscanf(L" hello 123 1.23 0x1.2p3 world",
761 s.s1, &s.i1, &s.d1, &s.f1, s.s2));
765 TEST(STDIO_TEST, cantwrite_EBADF) {
766 // If we open a file read-only...
767 FILE* fp = fopen("/proc/version", "r");
769 // ...all attempts to write to that file should return failure.
771 // They should also set errno to EBADF. This isn't POSIX, but it's traditional.
772 // glibc gets the wide-character functions wrong.
775 EXPECT_EQ(EOF, putc('x', fp));
776 EXPECT_EQ(EBADF, errno);
779 EXPECT_EQ(EOF, fprintf(fp, "hello"));
780 EXPECT_EQ(EBADF, errno);
783 EXPECT_EQ(EOF, fwprintf(fp, L"hello"));
784 #if defined(__BIONIC__)
785 EXPECT_EQ(EBADF, errno);
789 EXPECT_EQ(0U, fwrite("hello", 1, 2, fp));
790 EXPECT_EQ(EBADF, errno);
793 EXPECT_EQ(EOF, fputs("hello", fp));
794 EXPECT_EQ(EBADF, errno);
797 EXPECT_EQ(WEOF, fputwc(L'x', fp));
798 #if defined(__BIONIC__)
799 EXPECT_EQ(EBADF, errno);
803 // Tests that we can only have a consistent and correct fpos_t when using
804 // f*pos functions (i.e. fpos doesn't get inside a multi byte character).
805 TEST(STDIO_TEST, consistent_fpos_t) {
806 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
807 uselocale(LC_GLOBAL_LOCALE);
809 FILE* fp = tmpfile();
810 ASSERT_TRUE(fp != NULL);
812 wchar_t mb_one_bytes = L'h';
813 wchar_t mb_two_bytes = 0x00a2;
814 wchar_t mb_three_bytes = 0x20ac;
815 wchar_t mb_four_bytes = 0x24b62;
818 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fputwc(mb_one_bytes, fp)));
819 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
820 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
821 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
825 // Record each character position.
831 EXPECT_EQ(0, fgetpos(fp, &pos1));
832 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
833 EXPECT_EQ(0, fgetpos(fp, &pos2));
834 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
835 EXPECT_EQ(0, fgetpos(fp, &pos3));
836 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
837 EXPECT_EQ(0, fgetpos(fp, &pos4));
838 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
839 EXPECT_EQ(0, fgetpos(fp, &pos5));
841 #if defined(__BIONIC__)
842 // Bionic's fpos_t is just an alias for off_t. This is inherited from OpenBSD
843 // upstream. Glibc differs by storing the mbstate_t inside its fpos_t. In
844 // Bionic (and upstream OpenBSD) the mbstate_t is stored inside the FILE
846 ASSERT_EQ(0, static_cast<off_t>(pos1));
847 ASSERT_EQ(1, static_cast<off_t>(pos2));
848 ASSERT_EQ(3, static_cast<off_t>(pos3));
849 ASSERT_EQ(6, static_cast<off_t>(pos4));
850 ASSERT_EQ(10, static_cast<off_t>(pos5));
853 // Exercise back and forth movements of the position.
854 ASSERT_EQ(0, fsetpos(fp, &pos2));
855 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
856 ASSERT_EQ(0, fsetpos(fp, &pos1));
857 ASSERT_EQ(mb_one_bytes, static_cast<wchar_t>(fgetwc(fp)));
858 ASSERT_EQ(0, fsetpos(fp, &pos4));
859 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fgetwc(fp)));
860 ASSERT_EQ(0, fsetpos(fp, &pos3));
861 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fgetwc(fp)));
862 ASSERT_EQ(0, fsetpos(fp, &pos5));
863 ASSERT_EQ(WEOF, fgetwc(fp));
868 // Exercise the interaction between fpos and seek.
869 TEST(STDIO_TEST, fpos_t_and_seek) {
870 ASSERT_STREQ("C.UTF-8", setlocale(LC_CTYPE, "C.UTF-8"));
871 uselocale(LC_GLOBAL_LOCALE);
873 // In glibc-2.16 fseek doesn't work properly in wide mode
874 // (https://sourceware.org/bugzilla/show_bug.cgi?id=14543). One workaround is
875 // to close and re-open the file. We do it in order to make the test pass
879 FILE* fp = fdopen(tf.fd, "w+");
880 ASSERT_TRUE(fp != NULL);
882 wchar_t mb_two_bytes = 0x00a2;
883 wchar_t mb_three_bytes = 0x20ac;
884 wchar_t mb_four_bytes = 0x24b62;
887 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fputwc(mb_two_bytes, fp)));
888 ASSERT_EQ(mb_three_bytes, static_cast<wchar_t>(fputwc(mb_three_bytes, fp)));
889 ASSERT_EQ(mb_four_bytes, static_cast<wchar_t>(fputwc(mb_four_bytes, fp)));
894 fp = fopen(tf.filename, "r");
895 ASSERT_TRUE(fp != NULL);
897 // Store a valid position.
898 fpos_t mb_two_bytes_pos;
899 ASSERT_EQ(0, fgetpos(fp, &mb_two_bytes_pos));
901 // Move inside mb_four_bytes with fseek.
902 long offset_inside_mb = 6;
903 ASSERT_EQ(0, fseek(fp, offset_inside_mb, SEEK_SET));
905 // Store the "inside multi byte" position.
906 fpos_t pos_inside_mb;
907 ASSERT_EQ(0, fgetpos(fp, &pos_inside_mb));
908 #if defined(__BIONIC__)
909 ASSERT_EQ(offset_inside_mb, static_cast<off_t>(pos_inside_mb));
912 // Reading from within a byte should produce an error.
913 ASSERT_EQ(WEOF, fgetwc(fp));
914 ASSERT_EQ(EILSEQ, errno);
916 // Reverting to a valid position should work.
917 ASSERT_EQ(0, fsetpos(fp, &mb_two_bytes_pos));
918 ASSERT_EQ(mb_two_bytes, static_cast<wchar_t>(fgetwc(fp)));
920 // Moving withing a multi byte with fsetpos should work but reading should
922 ASSERT_EQ(0, fsetpos(fp, &pos_inside_mb));
923 ASSERT_EQ(WEOF, fgetwc(fp));
924 ASSERT_EQ(EILSEQ, errno);
929 TEST(STDIO_TEST, fmemopen) {
931 memset(buf, 0, sizeof(buf));
932 FILE* fp = fmemopen(buf, sizeof(buf), "r+");
933 ASSERT_EQ('<', fputc('<', fp));
934 ASSERT_NE(EOF, fputs("abc>\n", fp));
937 ASSERT_STREQ("<abc>\n", buf);
939 AssertFileIs(fp, "<abc>\n", true);
943 TEST(STDIO_TEST, KNOWN_FAILURE_ON_BIONIC(fmemopen_NULL)) {
944 FILE* fp = fmemopen(nullptr, 128, "r+");
945 ASSERT_NE(EOF, fputs("xyz\n", fp));
947 AssertFileIs(fp, "xyz\n", true);
951 TEST(STDIO_TEST, fmemopen_EINVAL) {
956 ASSERT_EQ(nullptr, fmemopen(buf, 0, "r+"));
957 ASSERT_EQ(EINVAL, errno);
959 // No '+' with NULL buffer.
961 ASSERT_EQ(nullptr, fmemopen(nullptr, 0, "r"));
962 ASSERT_EQ(EINVAL, errno);
965 TEST(STDIO_TEST, open_memstream) {
968 FILE* fp = open_memstream(&p, &size);
969 ASSERT_NE(EOF, fputs("hello, world!", fp));
972 ASSERT_STREQ("hello, world!", p);
973 ASSERT_EQ(strlen("hello, world!"), size);
977 TEST(STDIO_TEST, open_memstream_EINVAL) {
978 #if defined(__BIONIC__)
984 ASSERT_EQ(nullptr, open_memstream(nullptr, &size));
985 ASSERT_EQ(EINVAL, errno);
989 ASSERT_EQ(nullptr, open_memstream(&p, nullptr));
990 ASSERT_EQ(EINVAL, errno);
992 GTEST_LOG_(INFO) << "This test does nothing on glibc.\n";
996 TEST(STDIO_TEST, fdopen_CLOEXEC) {
997 int fd = open("/proc/version", O_RDONLY);
998 ASSERT_TRUE(fd != -1);
1000 // This fd doesn't have O_CLOEXEC...
1001 int flags = fcntl(fd, F_GETFD);
1002 ASSERT_TRUE(flags != -1);
1003 ASSERT_EQ(0, flags & FD_CLOEXEC);
1005 FILE* fp = fdopen(fd, "re");
1006 ASSERT_TRUE(fp != NULL);
1008 // ...but the new one does.
1009 flags = fcntl(fileno(fp), F_GETFD);
1010 ASSERT_TRUE(flags != -1);
1011 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1017 TEST(STDIO_TEST, freopen_CLOEXEC) {
1018 FILE* fp = fopen("/proc/version", "r");
1019 ASSERT_TRUE(fp != NULL);
1021 // This FILE* doesn't have O_CLOEXEC...
1022 int flags = fcntl(fileno(fp), F_GETFD);
1023 ASSERT_TRUE(flags != -1);
1024 ASSERT_EQ(0, flags & FD_CLOEXEC);
1026 fp = freopen("/proc/version", "re", fp);
1028 // ...but the new one does.
1029 flags = fcntl(fileno(fp), F_GETFD);
1030 ASSERT_TRUE(flags != -1);
1031 ASSERT_EQ(FD_CLOEXEC, flags & FD_CLOEXEC);
1036 TEST(STDIO_TEST, fopen64_freopen64) {
1037 FILE* fp = fopen64("/proc/version", "r");
1038 ASSERT_TRUE(fp != nullptr);
1039 fp = freopen64("/proc/version", "re", fp);
1040 ASSERT_TRUE(fp != nullptr);
1044 // https://code.google.com/p/android/issues/detail?id=81155
1045 // http://b/18556607
1046 TEST(STDIO_TEST, fread_unbuffered_pathological_performance) {
1047 FILE* fp = fopen("/dev/zero", "r");
1048 ASSERT_TRUE(fp != NULL);
1050 // Make this stream unbuffered.
1051 setvbuf(fp, 0, _IONBF, 0);
1054 memset(buf, 0xff, sizeof(buf));
1056 time_t t0 = time(NULL);
1057 for (size_t i = 0; i < 1024; ++i) {
1058 ASSERT_EQ(1U, fread(buf, 64*1024, 1, fp));
1060 time_t t1 = time(NULL);
1064 // 1024 64KiB reads should have been very quick.
1065 ASSERT_LE(t1 - t0, 1);
1067 for (size_t i = 0; i < 64*1024; ++i) {
1068 ASSERT_EQ('\0', buf[i]);
1070 for (size_t i = 64*1024; i < 65*1024; ++i) {
1071 ASSERT_EQ('\xff', buf[i]);
1075 TEST(STDIO_TEST, fread_EOF) {
1076 std::string digits("0123456789");
1077 FILE* fp = fmemopen(&digits[0], digits.size(), "r");
1079 // Try to read too much, but little enough that it still fits in the FILE's internal buffer.
1081 memset(buf1, 0, sizeof(buf1));
1082 ASSERT_EQ(2U, fread(buf1, 4, 4, fp));
1083 ASSERT_STREQ("0123456789", buf1);
1084 ASSERT_TRUE(feof(fp));
1088 // Try to read way too much so stdio tries to read more direct from the stream.
1089 char buf2[4 * 4096];
1090 memset(buf2, 0, sizeof(buf2));
1091 ASSERT_EQ(2U, fread(buf2, 4, 4096, fp));
1092 ASSERT_STREQ("0123456789", buf2);
1093 ASSERT_TRUE(feof(fp));
1098 static void test_fread_from_write_only_stream(size_t n) {
1099 FILE* fp = fopen("/dev/null", "w");
1100 std::vector<char> buf(n, 0);
1102 ASSERT_EQ(0U, fread(&buf[0], n, 1, fp));
1103 ASSERT_EQ(EBADF, errno);
1104 ASSERT_TRUE(ferror(fp));
1105 ASSERT_FALSE(feof(fp));
1109 TEST(STDIO_TEST, fread_from_write_only_stream_slow_path) {
1110 test_fread_from_write_only_stream(1);
1113 TEST(STDIO_TEST, fread_from_write_only_stream_fast_path) {
1114 test_fread_from_write_only_stream(64*1024);
1117 static void test_fwrite_after_fread(size_t n) {
1120 FILE* fp = fdopen(tf.fd, "w+");
1121 ASSERT_EQ(1U, fwrite("1", 1, 1, fp));
1124 // We've flushed but not rewound, so there's nothing to read.
1125 std::vector<char> buf(n, 0);
1126 ASSERT_EQ(0U, fread(&buf[0], 1, buf.size(), fp));
1127 ASSERT_TRUE(feof(fp));
1129 // But hitting EOF doesn't prevent us from writing...
1131 ASSERT_EQ(1U, fwrite("2", 1, 1, fp)) << strerror(errno);
1133 // And if we rewind, everything's there.
1135 ASSERT_EQ(2U, fread(&buf[0], 1, buf.size(), fp));
1136 ASSERT_EQ('1', buf[0]);
1137 ASSERT_EQ('2', buf[1]);
1142 TEST(STDIO_TEST, fwrite_after_fread_slow_path) {
1143 test_fwrite_after_fread(16);
1146 TEST(STDIO_TEST, fwrite_after_fread_fast_path) {
1147 test_fwrite_after_fread(64*1024);
1150 // http://b/19172514
1151 TEST(STDIO_TEST, fread_after_fseek) {
1154 FILE* fp = fopen(tf.filename, "w+");
1155 ASSERT_TRUE(fp != nullptr);
1157 char file_data[12288];
1158 for (size_t i = 0; i < 12288; i++) {
1161 ASSERT_EQ(12288U, fwrite(file_data, 1, 12288, fp));
1164 fp = fopen(tf.filename, "r");
1165 ASSERT_TRUE(fp != nullptr);
1168 size_t cur_location = 0;
1169 // Small read to populate internal buffer.
1170 ASSERT_EQ(100U, fread(buffer, 1, 100, fp));
1171 ASSERT_EQ(memcmp(file_data, buffer, 100), 0);
1173 cur_location = static_cast<size_t>(ftell(fp));
1174 // Large read to force reading into the user supplied buffer and bypassing
1175 // the internal buffer.
1176 ASSERT_EQ(8192U, fread(buffer, 1, 8192, fp));
1177 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 8192), 0);
1179 // Small backwards seek to verify fseek does not reuse the internal buffer.
1180 ASSERT_EQ(0, fseek(fp, -22, SEEK_CUR)) << strerror(errno);
1181 cur_location = static_cast<size_t>(ftell(fp));
1182 ASSERT_EQ(22U, fread(buffer, 1, 22, fp));
1183 ASSERT_EQ(memcmp(file_data+cur_location, buffer, 22), 0);
1188 // https://code.google.com/p/android/issues/detail?id=184847
1189 TEST(STDIO_TEST, fread_EOF_184847) {
1193 FILE* fw = fopen(tf.filename, "w");
1194 ASSERT_TRUE(fw != nullptr);
1196 FILE* fr = fopen(tf.filename, "r");
1197 ASSERT_TRUE(fr != nullptr);
1199 fwrite("a", 1, 1, fw);
1201 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1202 ASSERT_STREQ("a", buf);
1204 // 'fr' is now at EOF.
1205 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1206 ASSERT_TRUE(feof(fr));
1208 // Write some more...
1209 fwrite("z", 1, 1, fw);
1212 // ...and check that we can read it back.
1213 // (BSD thinks that once a stream has hit EOF, it must always return EOF. SysV disagrees.)
1214 ASSERT_EQ(1U, fread(buf, 1, 1, fr));
1215 ASSERT_STREQ("z", buf);
1217 // But now we're done.
1218 ASSERT_EQ(0U, fread(buf, 1, 1, fr));
1224 TEST(STDIO_TEST, fclose_invalidates_fd) {
1225 // The typical error we're trying to help people catch involves accessing
1226 // memory after it's been freed. But we know that stdin/stdout/stderr are
1227 // special and don't get deallocated, so this test uses stdin.
1228 ASSERT_EQ(0, fclose(stdin));
1230 // Even though using a FILE* after close is undefined behavior, I've closed
1231 // this bug as "WAI" too many times. We shouldn't hand out stale fds,
1232 // especially because they might actually correspond to a real stream.
1234 ASSERT_EQ(-1, fileno(stdin));
1235 ASSERT_EQ(EBADF, errno);
1238 TEST(STDIO_TEST, fseek_ftell_unseekable) {
1239 #if defined(__BIONIC__) // glibc has fopencookie instead.
1240 auto read_fn = [](void*, char*, int) { return -1; };
1241 FILE* fp = funopen(nullptr, read_fn, nullptr, nullptr, nullptr);
1242 ASSERT_TRUE(fp != nullptr);
1244 // Check that ftell balks on an unseekable FILE*.
1246 ASSERT_EQ(-1, ftell(fp));
1247 ASSERT_EQ(ESPIPE, errno);
1249 // SEEK_CUR is rewritten as SEEK_SET internally...
1251 ASSERT_EQ(-1, fseek(fp, 0, SEEK_CUR));
1252 ASSERT_EQ(ESPIPE, errno);
1254 // ...so it's worth testing the direct seek path too.
1256 ASSERT_EQ(-1, fseek(fp, 0, SEEK_SET));
1257 ASSERT_EQ(ESPIPE, errno);
1261 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1265 TEST(STDIO_TEST, funopen_EINVAL) {
1266 #if defined(__BIONIC__)
1268 ASSERT_EQ(nullptr, funopen(nullptr, nullptr, nullptr, nullptr, nullptr));
1269 ASSERT_EQ(EINVAL, errno);
1271 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1275 TEST(STDIO_TEST, funopen_seek) {
1276 #if defined(__BIONIC__)
1277 auto read_fn = [](void*, char*, int) { return -1; };
1279 auto seek_fn = [](void*, fpos_t, int) -> fpos_t { return 0xfedcba12; };
1280 auto seek64_fn = [](void*, fpos64_t, int) -> fpos64_t { return 0xfedcba12345678; };
1282 FILE* fp = funopen(nullptr, read_fn, nullptr, seek_fn, nullptr);
1283 ASSERT_TRUE(fp != nullptr);
1285 #if defined(__LP64__)
1286 EXPECT_EQ(0, fgetpos(fp, &pos)) << strerror(errno);
1287 EXPECT_EQ(0xfedcba12LL, pos);
1289 EXPECT_EQ(-1, fgetpos(fp, &pos)) << strerror(errno);
1290 EXPECT_EQ(EOVERFLOW, errno);
1293 FILE* fp64 = funopen64(nullptr, read_fn, nullptr, seek64_fn, nullptr);
1294 ASSERT_TRUE(fp64 != nullptr);
1296 EXPECT_EQ(0, fgetpos64(fp64, &pos64)) << strerror(errno);
1297 EXPECT_EQ(0xfedcba12345678, pos64);
1299 GTEST_LOG_(INFO) << "glibc uses fopencookie instead.\n";
1303 TEST(STDIO_TEST, lots_of_concurrent_files) {
1304 std::vector<TemporaryFile*> tfs;
1305 std::vector<FILE*> fps;
1307 for (size_t i = 0; i < 256; ++i) {
1308 TemporaryFile* tf = new TemporaryFile;
1310 FILE* fp = fopen(tf->filename, "w+");
1312 fprintf(fp, "hello %zu!\n", i);
1316 for (size_t i = 0; i < 256; ++i) {
1317 char expected[BUFSIZ];
1318 snprintf(expected, sizeof(expected), "hello %zu!\n", i);
1320 AssertFileIs(fps[i], expected);
1326 static void AssertFileOffsetAt(FILE* fp, off64_t offset) {
1327 EXPECT_EQ(offset, ftell(fp));
1328 EXPECT_EQ(offset, ftello(fp));
1329 EXPECT_EQ(offset, ftello64(fp));
1332 EXPECT_EQ(0, fgetpos(fp, &pos));
1333 EXPECT_EQ(0, fgetpos64(fp, &pos64));
1334 #if defined(__BIONIC__)
1335 EXPECT_EQ(offset, static_cast<off64_t>(pos));
1336 EXPECT_EQ(offset, static_cast<off64_t>(pos64));
1338 GTEST_LOG_(INFO) << "glibc's fpos_t is opaque.\n";
1342 TEST(STDIO_TEST, seek_tell_family_smoke) {
1344 FILE* fp = fdopen(tf.fd, "w+");
1346 // Initially we should be at 0.
1347 AssertFileOffsetAt(fp, 0);
1349 // Seek to offset 8192.
1350 ASSERT_EQ(0, fseek(fp, 8192, SEEK_SET));
1351 AssertFileOffsetAt(fp, 8192);
1353 ASSERT_EQ(0, fgetpos(fp, &eight_k_pos));
1355 // Seek forward another 8192...
1356 ASSERT_EQ(0, fseek(fp, 8192, SEEK_CUR));
1357 AssertFileOffsetAt(fp, 8192 + 8192);
1358 fpos64_t sixteen_k_pos64;
1359 ASSERT_EQ(0, fgetpos64(fp, &sixteen_k_pos64));
1361 // Seek back 8192...
1362 ASSERT_EQ(0, fseek(fp, -8192, SEEK_CUR));
1363 AssertFileOffsetAt(fp, 8192);
1365 // Since we haven't written anything, the end is also at 0.
1366 ASSERT_EQ(0, fseek(fp, 0, SEEK_END));
1367 AssertFileOffsetAt(fp, 0);
1369 // Check that our fpos64_t from 16KiB works...
1370 ASSERT_EQ(0, fsetpos64(fp, &sixteen_k_pos64));
1371 AssertFileOffsetAt(fp, 8192 + 8192);
1372 // ...as does our fpos_t from 8192.
1373 ASSERT_EQ(0, fsetpos(fp, &eight_k_pos));
1374 AssertFileOffsetAt(fp, 8192);
1376 // Do fseeko and fseeko64 work too?
1377 ASSERT_EQ(0, fseeko(fp, 1234, SEEK_SET));
1378 AssertFileOffsetAt(fp, 1234);
1379 ASSERT_EQ(0, fseeko64(fp, 5678, SEEK_SET));
1380 AssertFileOffsetAt(fp, 5678);
1385 TEST(STDIO_TEST, fseek_fseeko_EINVAL) {
1387 FILE* fp = fdopen(tf.fd, "w+");
1391 ASSERT_EQ(-1, fseek(fp, 0, 123));
1392 ASSERT_EQ(EINVAL, errno);
1394 ASSERT_EQ(-1, fseeko(fp, 0, 123));
1395 ASSERT_EQ(EINVAL, errno);
1397 ASSERT_EQ(-1, fseeko64(fp, 0, 123));
1398 ASSERT_EQ(EINVAL, errno);
1402 ASSERT_EQ(-1, fseek(fp, -1, SEEK_SET));
1403 ASSERT_EQ(EINVAL, errno);
1405 ASSERT_EQ(-1, fseeko(fp, -1, SEEK_SET));
1406 ASSERT_EQ(EINVAL, errno);
1408 ASSERT_EQ(-1, fseeko64(fp, -1, SEEK_SET));
1409 ASSERT_EQ(EINVAL, errno);
1414 TEST(STDIO_TEST, ctermid) {
1415 ASSERT_STREQ("/dev/tty", ctermid(nullptr));
1417 char buf[L_ctermid] = {};
1418 ASSERT_EQ(buf, ctermid(buf));
1419 ASSERT_STREQ("/dev/tty", buf);
1422 TEST(STDIO_TEST, remove) {
1426 ASSERT_EQ(0, remove(tf.filename));
1427 ASSERT_EQ(-1, lstat(tf.filename, &sb));
1428 ASSERT_EQ(ENOENT, errno);
1431 ASSERT_EQ(0, remove(td.dirname));
1432 ASSERT_EQ(-1, lstat(td.dirname, &sb));
1433 ASSERT_EQ(ENOENT, errno);
1436 ASSERT_EQ(-1, remove(tf.filename));
1437 ASSERT_EQ(ENOENT, errno);
1440 ASSERT_EQ(-1, remove(td.dirname));
1441 ASSERT_EQ(ENOENT, errno);
1444 TEST(STDIO_DEATHTEST, snprintf_30445072_known_buffer_size) {
1446 ASSERT_EXIT(snprintf(buf, atol("-1"), "hello"),
1447 testing::KilledBySignal(SIGABRT),
1448 #if defined(NOFORTIFY)
1449 "FORTIFY: vsnprintf: size .* > SSIZE_MAX"
1451 "FORTIFY: vsnprintf: prevented .*-byte write into 16-byte buffer"
1456 TEST(STDIO_DEATHTEST, snprintf_30445072_unknown_buffer_size) {
1457 std::string buf = "world";
1458 ASSERT_EXIT(snprintf(&buf[0], atol("-1"), "hello"),
1459 testing::KilledBySignal(SIGABRT),
1460 "FORTIFY: vsnprintf: size .* > SSIZE_MAX");
1463 TEST(STDIO_TEST, sprintf_30445072) {
1464 std::string buf = "world";
1465 sprintf(&buf[0], "hello");
1466 ASSERT_EQ(buf, "hello");