xref: /illumos-gate/usr/src/test/libc-tests/tests/printf-6961.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 2017 Joyent, Inc.
14  */
15 
16 /*
17  * Regression test for illumos #6961. We mistakenly zeroed out a character that
18  * we shouldn't have when dealing with a 64-bit libc.
19  */
20 
21 #include <stdio.h>
22 #include <string.h>
23 #include <strings.h>
24 #include <stdlib.h>
25 
26 static void
27 print_diff(char *test, char *correct, char *wrong)
28 {
29 	int i;
30 	printf("test failed: received incorrect octal for case %s\n", test);
31 	for (i = 0; i < 32; i++) {
32 		printf("byte %d: expected 0x%x, found 0x%x\n", i, correct[i],
33 		    wrong[i]);
34 	}
35 }
36 
37 int
38 main(void)
39 {
40 	int ret = 0;
41 	char buf[32];
42 
43 	/* ~0L in octal */
44 	char octal0[32] = { 'r', 'r', 'r', 'r', '1', '7', '7', '7', '7', '7',
45 	    '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
46 	    '7', '7', '7', '\0', 'r', 'r', 'r', 'r', 'r' };
47 
48 	char decimal0[32] = { 'r', 'r', 'r', 'r', '-', '1', '\0', 'r', 'r', 'r',
49 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
50 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r' };
51 
52 	char hex0[32] = { 'r', 'r', 'r', 'r', 'f', 'f', 'f', 'f', 'f', 'f',
53 	    'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', 'f', '\0', 'r', 'r',
54 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r' };
55 
56 	/* 42 in octal */
57 	char octal1[32] = { 'r', 'r', 'r', 'r', '5', '2', '\0', 'r', 'r', 'r',
58 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
59 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r' };
60 
61 	/* 42 in decimal */
62 	char decimal1[32] = { 'r', 'r', 'r', 'r', '4', '2', '\0', 'r', 'r', 'r',
63 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
64 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r' };
65 
66 	/* 42 in hex */
67 	char hex1[32] = { 'r', 'r', 'r', 'r', '2', 'a', '\0', 'r', 'r', 'r',
68 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r',
69 	    'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r', 'r' };
70 
71 
72 	(void) memset(buf, 'r', sizeof (buf));
73 	(void) snprintf(buf + 4, sizeof (buf) - 4, "%lo", ~0L);
74 	if (bcmp(octal0, buf, sizeof (buf)) != 0) {
75 		print_diff("~0 in Octal", octal0, buf);
76 		ret++;
77 	}
78 
79 	(void) memset(buf, 'r', sizeof (buf));
80 	(void) snprintf(buf + 4, sizeof (buf) - 4, "%lo", 42L);
81 	if (bcmp(octal1, buf, sizeof (buf)) != 0) {
82 		print_diff("42 in Octal", octal1, buf);
83 		ret++;
84 	}
85 
86 	(void) memset(buf, 'r', sizeof (buf));
87 	(void) snprintf(buf + 4, sizeof (buf) - 4, "%ld", ~0L);
88 	if (bcmp(decimal0, buf, sizeof (buf)) != 0) {
89 		print_diff("~0 in Decimal", decimal0, buf);
90 		ret++;
91 	}
92 
93 	(void) memset(buf, 'r', sizeof (buf));
94 	(void) snprintf(buf + 4, sizeof (buf) - 4, "%ld", 42L);
95 	if (bcmp(decimal1, buf, sizeof (buf)) != 0) {
96 		print_diff("42 in Decimal", decimal1, buf);
97 		ret++;
98 	}
99 
100 	(void) memset(buf, 'r', sizeof (buf));
101 	(void) snprintf(buf + 4, sizeof (buf) - 4, "%lx", ~0L);
102 	if (bcmp(hex0, buf, sizeof (buf)) != 0) {
103 		print_diff("~0 in Hex", hex0, buf);
104 		ret++;
105 	}
106 
107 	(void) memset(buf, 'r', sizeof (buf));
108 	(void) snprintf(buf + 4, sizeof (buf) - 4, "%lx", 42L);
109 	if (bcmp(hex1, buf, sizeof (buf)) != 0) {
110 		print_diff("42 in Hex", hex1, buf);
111 		ret++;
112 	}
113 
114 	return (ret);
115 }
116