Skip to content

Commit b93a09f

Browse files
committed
fix snprintf_s tests
many copy&paste errors analog to sprintf_s
1 parent cd6aa04 commit b93a09f

File tree

2 files changed

+40
-55
lines changed

2 files changed

+40
-55
lines changed

src/safeclib/snprintf_s.c

+12-10
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,17 @@
3030
#include "safe_str_constraint.h"
3131
#include <stdarg.h>
3232

33+
/* TODO: error when fmt contains %n, or encoding errors occur.
34+
*/
3335

3436
/**
3537
* @brief
3638
* The snprintf_s function composes a string with same test that
3739
* would be printed if format was used on printf. Instead of being
3840
* printed, the content is stored in dest.
39-
* At most dmax - 1 characters are written. The resulting
40-
* character string will be terminated with a null character,
41+
* More than dmax - 1 characters might be written, so this variant is unsafe!
42+
* Always use sprintf_s instead.
43+
* The resulting character string will be terminated with a null character,
4144
* unless dmax is zero. If dmax is zero, nothing is written and
4245
* dest may be a null pointer, however the return value (number
4346
* of bytes that would be written) is still calculated and
@@ -53,14 +56,13 @@
5356
* @param fmt format-control string.
5457
* @param ... optional arguments
5558
*
56-
* @return On success the total number of characters written is returned.
57-
* @return On failure a negative number is returned.
58-
* @return If the buffer dest is too small for the formatted text,
59-
* including the terminating null, then the buffer is set to an
60-
* empty string by placing a null character at dest[0], and the
61-
* invalid parameter handler is invoked.
62-
* snprintf_s does not guarantees that the buffer will be
63-
* null-terminated unless the buffer size is zero.
59+
* @return Number of characters not including the terminating null
60+
* character (which is always written as long as buffer is not
61+
* a null pointer and bufsz is not zero and not greater than
62+
* RSIZE_MAX_STR), which would have been written to buffer if
63+
* bufsz was ignored, or a negative value if a runtime
64+
* constraints violation or an encoding error occurred.
65+
*
6466
* @retval ESNULLP when dest/fmt is NULL pointer
6567
* @retval ESZEROL when dmax = 0
6668
* @retval ESLEMAX when dmax > RSIZE_MAX_STR

tests/test_snprintf_s.c

+28-45
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*------------------------------------------------------------------
22
* test_snprintf_s
33
*
4-
*
54
*------------------------------------------------------------------
65
*/
76

@@ -33,7 +32,7 @@ int test_snprintf_s (void)
3332

3433
/*--------------------------------------------------*/
3534

36-
rc = snprintf_s(str1, LEN, "%s", NULL);
35+
rc = snprintf_s(str1, LEN, NULL, NULL);
3736
if (rc != ESNULLP) {
3837
debug_printf("%s %u Error rc=%u \n",
3938
__FUNCTION__, __LINE__, rc);
@@ -64,7 +63,7 @@ int test_snprintf_s (void)
6463
strcpy(str2, "keep it simple");
6564

6665
rc = snprintf_s(str1, 1, "%s", str2);
67-
if (rc != ESUNTERM) {
66+
if (rc != 14) {
6867
debug_printf("%s %u Error rc=%u \n",
6968
__FUNCTION__, __LINE__, rc);
7069
errs++;
@@ -82,42 +81,37 @@ int test_snprintf_s (void)
8281
strcpy(str2, "keep it simple");
8382

8483
rc = snprintf_s(str1, 2, "%s", str2);
85-
if (rc != ESUNTERM) {
84+
if (rc != 14) {
8685
debug_printf("%s %u Error rc=%u \n",
8786
__FUNCTION__, __LINE__, rc);
8887
errs++;
8988
}
9089

91-
if (str1[0] != '\0') {
92-
debug_printf("%s %u Expected null \n",
93-
__FUNCTION__, __LINE__);
94-
errs++;
95-
}
96-
9790
/*--------------------------------------------------*/
9891

99-
strcpy(&str1[0], "aaaaaaaaaa");
100-
strcpy(&str2[0], "keep it simple");
92+
strcpy(str1, "aaaaaaaaaa");
93+
strcpy(str2, "keep it simple");
10194

10295
len1 = strlen(str1);
10396
len2 = strlen(str2);
10497

10598
rc = snprintf_s(str1, 50, "%s", str2);
106-
if (rc != EOK) {
99+
if (rc != len2) {
107100
debug_printf("%s %u Error rc=%u \n",
108101
__FUNCTION__, __LINE__, rc);
109102
errs++;
110103
}
111104

112105
len3 = strlen(str1);
113-
if (len3 != (len1+len2)) {
106+
if (len3 != len2) {
114107
debug_printf("%s %u lengths wrong: %u %u %u \n",
115108
__FUNCTION__, __LINE__, len1, len2, len3);
116109
errs++;
117110
}
118111

119112
/*--------------------------------------------------*/
120113

114+
str1[0] = '\0';
121115
strcpy(str2, "keep it simple");
122116

123117
rc = snprintf_s(str1, 5, "%s", str2);
@@ -140,27 +134,22 @@ int test_snprintf_s (void)
140134
strcpy(str2, "keep it simple");
141135

142136
rc = snprintf_s(str1, 2, "%s", str2);
143-
if (rc != ESNOSPC) {
137+
if (rc != 14) {
144138
debug_printf("%s %u Error rc=%u \n",
145139
__FUNCTION__, __LINE__, rc);
146140
errs++;
147141
}
148142

149-
if (str1[0] != '\0') {
150-
debug_printf("%s %u Expected null \n",
151-
__FUNCTION__, __LINE__);
152-
errs++;
153-
}
154-
155143
/*--------------------------------------------------*/
156144

157145
str1[0] = '\0';
158146
strcpy(str2, "keep it simple");
159147

160148
rc = snprintf_s(str1, 20, "%s", str2);
161-
if (rc != EOK) {
149+
if (rc <= 0) {
162150
debug_printf("%s %u Error rc=%u \n",
163151
__FUNCTION__, __LINE__, rc);
152+
errs++;
164153
}
165154

166155
ind = strcmp(str1, str2);
@@ -176,7 +165,7 @@ int test_snprintf_s (void)
176165
str2[0] = '\0';
177166

178167
rc = snprintf_s(str1, LEN, "%s", str2);
179-
if (rc != EOK) {
168+
if (rc != 0) {
180169
debug_printf("%s %u Error rc=%u \n",
181170
__FUNCTION__, __LINE__, rc);
182171
errs++;
@@ -194,7 +183,7 @@ int test_snprintf_s (void)
194183
strcpy(str2, "keep it simple");
195184

196185
rc = snprintf_s(str1, LEN, "%s", str2);
197-
if (rc != EOK) {
186+
if (rc <= 0) {
198187
debug_printf("%s %u Error rc=%u \n",
199188
__FUNCTION__, __LINE__, rc);
200189
errs++;
@@ -213,13 +202,13 @@ int test_snprintf_s (void)
213202
strcpy(str2, "keep it simple");
214203

215204
rc = snprintf_s(str1, LEN, "%s", str2);
216-
if (rc != EOK) {
205+
if (rc <= 0) {
217206
debug_printf("%s %u Error rc=%u \n",
218207
__FUNCTION__, __LINE__, rc);
219208
errs++;
220209
}
221210

222-
ind = strcmp(str1, "qqweqqkeep it simple");
211+
ind = strcmp(str1, "keep it simple");
223212
if (ind != 0) {
224213
debug_printf("%s %u Error -%s- \n",
225214
__FUNCTION__, __LINE__, str1);
@@ -232,7 +221,7 @@ int test_snprintf_s (void)
232221
strcpy(str2, "keep it simple");
233222

234223
rc = snprintf_s(str1, 12, "%s", str2);
235-
if (rc != ESNOSPC) {
224+
if (rc != 14) { /* sic! unsafe */
236225
debug_printf("%s %u Error rc=%u \n",
237226
__FUNCTION__, __LINE__, rc);
238227
errs++;
@@ -244,13 +233,13 @@ int test_snprintf_s (void)
244233
strcpy(str2, "keep it simple");
245234

246235
rc = snprintf_s(str1, 52, "%s", str2);
247-
if (rc != EOK) {
236+
if (rc <= 0) {
248237
debug_printf("%s %u Error rc=%u \n",
249238
__FUNCTION__, __LINE__, rc);
250239
errs++;
251240
}
252241

253-
ind = strcmp(str1, "1234keep it simple");
242+
ind = strcmp(str1, "keep it simple");
254243
if (ind != 0) {
255244
debug_printf("%s %u Error -%s- \n",
256245
__FUNCTION__, __LINE__, str1);
@@ -262,32 +251,26 @@ int test_snprintf_s (void)
262251
strcpy(str1, "12345678901234567890");
263252

264253
rc = snprintf_s(str1, 8, "%s", &str1[7]);
265-
if (rc != ESOVRLP) {
254+
if (rc != 13) {
266255
debug_printf("%s %u Error rc=%u \n",
267256
__FUNCTION__, __LINE__, rc);
268257
errs++;
269258
}
270259

271-
if (str1[0] != '\0') {
272-
debug_printf("%s %u Expected null \n",
273-
__FUNCTION__, __LINE__);
274-
errs++;
275-
}
276-
277260
/*--------------------------------------------------*/
278261

279262
strcpy(str1, "123456789");
280263

281264
rc = snprintf_s(str1, 9, "%s", &str1[8]);
282-
if (rc != ESOVRLP) {
265+
if (rc != 1) { /* overlapping allowed */
283266
debug_printf("%s %u Error rc=%u \n",
284267
__FUNCTION__, __LINE__, rc);
285268
errs++;
286269
}
287-
288-
if (str1[0] != '\0') {
289-
debug_printf("%s %u Expected null \n",
290-
__FUNCTION__, __LINE__);
270+
ind = strcmp(str1, "9");
271+
if (ind != 0) {
272+
debug_printf("%s %u Error -%s- \n",
273+
__FUNCTION__, __LINE__, str1);
291274
errs++;
292275
}
293276

@@ -297,13 +280,13 @@ int test_snprintf_s (void)
297280
strcpy(str1, "keep it simple");
298281

299282
rc = snprintf_s(str2, 31, "%s", &str1[0]);
300-
if (rc != EOK) {
283+
if (rc <= 0) {
301284
debug_printf("%s %u Error rc=%u \n",
302285
__FUNCTION__, __LINE__, rc);
303286
errs++;
304287
}
305288

306-
ind = strcmp(str2, "123keep it simple");
289+
ind = strcmp(str2, "keep it simple");
307290
if (ind != 0) {
308291
debug_printf("%s %u Error -%s- \n",
309292
__FUNCTION__, __LINE__, str1);
@@ -316,13 +299,13 @@ int test_snprintf_s (void)
316299
strcpy(str1, "56789");
317300

318301
rc = snprintf_s(str2, 10, "%s", str1);
319-
if (rc != EOK) {
302+
if (rc <= 0) {
320303
debug_printf("%s %u Error rc=%u \n",
321304
__FUNCTION__, __LINE__, rc);
322305
errs++;
323306
}
324307

325-
ind = strcmp(str2, "123456789");
308+
ind = strcmp(str2, "56789");
326309
if (ind != 0) {
327310
debug_printf("%s %u Error -%s- \n",
328311
__FUNCTION__, __LINE__, str1);

0 commit comments

Comments
 (0)