xref: /illumos-gate/usr/src/test/libc-tests/tests/stdio/ftello_12768.c (revision 09fbbb7d1b5a956a9b81304070dcff318a4a158e)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2020 Oxide Computer Company
14  */
15 
16 /*
17  * Regression test for 12768 '12392 regressed ftello64 behavior'. The heart of
18  * the problem was a bad cast that resulted in us not properly transmitting that
19  * size.
20  */
21 
22 #include <stdio.h>
23 #include <err.h>
24 #include <stdlib.h>
25 #include <sys/sysmacros.h>
26 
27 int
28 main(void)
29 {
30 	FILE *f;
31 	size_t i;
32 	int ret = EXIT_SUCCESS;
33 	static off_t offsets[] = {
34 		23,
35 		0xa0000,	/* 64 KiB */
36 		0x100000,	/* 1 MiB */
37 		0x7fffffffULL,	/* 2 GiB - 1 */
38 		0xc0000000ULL,	/* 3 GiB */
39 		0x200005432ULL	/* 8 GiB + misc */
40 	};
41 
42 	f = tmpfile();
43 	if (f == NULL) {
44 		err(EXIT_FAILURE, "TEST FAILED: failed to create "
45 		    "temporary file");
46 	}
47 
48 	for (i = 0; i < ARRAY_SIZE(offsets); i++) {
49 		off_t ftret;
50 
51 		if (fseeko(f, offsets[i], SEEK_SET) != 0) {
52 			warn("TEST FAILED: failed to seek to %lld",
53 			    (long long)offsets[i]);
54 			ret = EXIT_FAILURE;
55 		}
56 
57 		ftret = ftello(f);
58 		if (ftret == -1) {
59 			warn("TEST FAILED: failed to get stream position at "
60 			    "%lld", (long long)offsets[i]);
61 			ret = EXIT_FAILURE;
62 		}
63 
64 		if (ftret != offsets[i]) {
65 			warnx("TEST FAILED: stream position mismatch: expected "
66 			    "%lld, found %lld", (long long)offsets[i],
67 			    (long long)ftret);
68 			ret = EXIT_FAILURE;
69 		}
70 	}
71 
72 	return (ret);
73 }
74