Skip to content

Commit c66abd7

Browse files
Implement length modifiers for printf %n conv spec (#1278)
The C Standard specifies that, when a conversion specification specifies a conversion specifier of n, the type of the passed pointer is specified by the length modifier (if any), i.e. that e.g. the argument for %hhn is of type signed char *, but Cosmopolitan currently does not handle this - instead always simply assuming that the pointer always points to an int. This patch implements, and tests, length modifiers with the n conversion specifier, with the tests testing all of the available length modifiers.
1 parent d1157d4 commit c66abd7

File tree

2 files changed

+108
-6
lines changed

2 files changed

+108
-6
lines changed

libc/stdio/fmt.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,25 @@ int __fmt(void *fn, void *arg, const char *format, va_list va, int *wrote) {
11251125
}
11261126
break;
11271127
case 'n':
1128-
*va_arg(va, int *) = *wrote;
1128+
switch (signbit) {
1129+
case 7:
1130+
*va_arg(va, int8_t *) = *wrote;
1131+
break;
1132+
case 15:
1133+
*va_arg(va, int16_t *) = *wrote;
1134+
break;
1135+
case 31:
1136+
*va_arg(va, int32_t *) = *wrote;
1137+
break;
1138+
case 63:
1139+
*va_arg(va, int64_t *) = *wrote;
1140+
break;
1141+
case 127:
1142+
*va_arg(va, int128_t *) = *wrote;
1143+
break;
1144+
default:
1145+
npassert(false);
1146+
}
11291147
break;
11301148

11311149
case 'F':

test/libc/stdio/snprintf_test.c

Lines changed: 89 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,27 +43,27 @@ TEST(snprintf, testInf) {
4343
ASSERT_EQ(i, 3);
4444
ASSERT_STREQ(buf, "inf");
4545

46-
memset(buf, 0, 4);
46+
memset(buf, '\0', 4);
4747
i = snprintf(buf, sizeof(buf), "%Lf", 1.0L / 0.0L);
4848
ASSERT_EQ(i, 3);
4949
ASSERT_STREQ(buf, "inf");
5050

51-
memset(buf, 0, 4);
51+
memset(buf, '\0', 4);
5252
i = snprintf(buf, sizeof(buf), "%e", 1.0 / 0.0);
5353
ASSERT_EQ(i, 3);
5454
ASSERT_STREQ(buf, "inf");
5555

56-
memset(buf, 0, 4);
56+
memset(buf, '\0', 4);
5757
i = snprintf(buf, sizeof(buf), "%Le", 1.0L / 0.0L);
5858
ASSERT_EQ(i, 3);
5959
ASSERT_STREQ(buf, "inf");
6060

61-
memset(buf, 0, 4);
61+
memset(buf, '\0', 4);
6262
i = snprintf(buf, sizeof(buf), "%g", 1.0 / 0.0);
6363
ASSERT_EQ(i, 3);
6464
ASSERT_STREQ(buf, "inf");
6565

66-
memset(buf, 0, 4);
66+
memset(buf, '\0', 4);
6767
i = snprintf(buf, sizeof(buf), "%Lg", 1.0L / 0.0L);
6868
ASSERT_EQ(i, 3);
6969
ASSERT_STREQ(buf, "inf");
@@ -101,3 +101,87 @@ TEST(snprintf,
101101
ASSERT_EQ(i, 10);
102102
ASSERT_STREQ(buf, "00000000x1");
103103
}
104+
105+
static void check_n_buffer_contents(char buf[350]) {
106+
for (int i = 0; i < 284; ++i)
107+
ASSERT_EQ(buf[i], ' ');
108+
ASSERT_STREQ(&buf[284], "428463");
109+
for (int i = 290; i < 350; ++i)
110+
ASSERT_EQ(buf[i], '\0');
111+
}
112+
113+
TEST(snprintf, testNConversionSpecifier) {
114+
char buf[350] = {};
115+
116+
int n_res_int = -1;
117+
int i = snprintf(buf, sizeof(buf), "%286d%d%n%d", 42, 84, &n_res_int, 63);
118+
ASSERT_EQ(i, 290);
119+
check_n_buffer_contents(buf);
120+
ASSERT_EQ(n_res_int, 288);
121+
122+
memset(&buf, '\0', sizeof(buf));
123+
long n_res_long = -1;
124+
i = snprintf(buf, sizeof(buf), "%286ld%ld%ln%ld", 42L, 84L, &n_res_long, 63L);
125+
ASSERT_EQ(i, 290);
126+
check_n_buffer_contents(buf);
127+
ASSERT_EQ(n_res_long, 288);
128+
129+
memset(&buf, '\0', sizeof(buf));
130+
long long n_res_long_long = -1;
131+
i = snprintf(buf, sizeof(buf), "%286lld%lld%lln%lld", 42LL, 84LL,
132+
&n_res_long_long, 63LL);
133+
ASSERT_EQ(i, 290);
134+
check_n_buffer_contents(buf);
135+
ASSERT_EQ(n_res_long_long, 288);
136+
137+
ASSERT_EQ(sizeof(short), 2);
138+
ASSERT_EQ(sizeof(int), 4);
139+
memset(&buf, '\0', sizeof(buf));
140+
short n_res_short = -1;
141+
i = snprintf(buf, sizeof(buf), "%286hd%hd%hn%hd", (42 | 0xFFFF0000),
142+
(84 | 0xFFFF0000), &n_res_short, (63 | 0xFFFF0000));
143+
ASSERT_EQ(i, 290);
144+
check_n_buffer_contents(buf);
145+
ASSERT_EQ(n_res_short, 288);
146+
147+
ASSERT_EQ(sizeof(unsigned char), 1);
148+
memset(&buf, '\0', sizeof(buf));
149+
signed char n_res_char = -1;
150+
i = snprintf(buf, sizeof(buf), "%286hhd%hhd%hhn%hhd", (42 | 0xFFFFFF00),
151+
(84 | 0xFFFFFF00), &n_res_char, (63 | 0xFFFFFF00));
152+
ASSERT_EQ(i, 290);
153+
check_n_buffer_contents(buf);
154+
ASSERT_EQ(n_res_char, (signed char)288);
155+
156+
memset(&buf, '\0', sizeof(buf));
157+
ssize_t n_res_size_t = -1;
158+
i = snprintf(buf, sizeof(buf), "%286zd%zd%zn%zd", (ssize_t)42, (ssize_t)84,
159+
&n_res_size_t, (ssize_t)63);
160+
ASSERT_EQ(i, 290);
161+
check_n_buffer_contents(buf);
162+
ASSERT_EQ(n_res_size_t, 288);
163+
164+
memset(&buf, '\0', sizeof(buf));
165+
intmax_t n_res_intmax_t = -1;
166+
i = snprintf(buf, sizeof(buf), "%286jd%jd%jn%jd", (intmax_t)42, (intmax_t)84,
167+
&n_res_intmax_t, (intmax_t)63);
168+
ASSERT_EQ(i, 290);
169+
check_n_buffer_contents(buf);
170+
ASSERT_EQ(n_res_intmax_t, 288);
171+
172+
memset(&buf, '\0', sizeof(buf));
173+
int128_t n_res_int128_t = -1;
174+
i = snprintf(buf, sizeof(buf), "%286jjd%jjd%jjn%jjd", (int128_t)42,
175+
(int128_t)84, &n_res_int128_t, (int128_t)63);
176+
ASSERT_EQ(i, 290);
177+
check_n_buffer_contents(buf);
178+
ASSERT_EQ(n_res_int128_t, 288);
179+
180+
memset(&buf, '\0', sizeof(buf));
181+
ptrdiff_t n_res_ptrdiff_t = -1;
182+
i = snprintf(buf, sizeof(buf), "%286td%td%tn%td", (ptrdiff_t)42,
183+
(ptrdiff_t)84, &n_res_ptrdiff_t, (ptrdiff_t)63);
184+
ASSERT_EQ(i, 290);
185+
check_n_buffer_contents(buf);
186+
ASSERT_EQ(n_res_ptrdiff_t, 288);
187+
}

0 commit comments

Comments
 (0)