Skip to content

Commit 8bf75b0

Browse files
committed
safestringlib: Fix unit test failures with SAFECLIB_STR_NULL_SLACK
When SAFECLIB_STR_NULL_SLACK is defined, several unit tests fail. Some fail because the functions misbehave, others fail because the tests are expecting different values in the slack buffer. Fix them all. Signed-off-by: Mark Rustad <[email protected]>
1 parent 5e032ec commit 8bf75b0

File tree

4 files changed

+41
-11
lines changed

4 files changed

+41
-11
lines changed

safeclib/stpcpy_s.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,9 @@ stpcpy_s(char *dest, rsize_t dmax, const char *src, errno_t *err)
157157
if (*dest == '\0') {
158158
#ifdef SAFECLIB_STR_NULL_SLACK
159159
/* null slack to clear any data */
160-
while (dmax) { *dest = '\0'; dmax--; dest++; }
160+
char *filler = dest;
161+
162+
while (dmax) { *filler = '\0'; dmax--; filler++; }
161163
#endif
162164
*err = RCNEGATE(EOK);
163165
return dest;
@@ -191,7 +193,9 @@ stpcpy_s(char *dest, rsize_t dmax, const char *src, errno_t *err)
191193
if (*dest == '\0') {
192194
#ifdef SAFECLIB_STR_NULL_SLACK
193195
/* null slack to clear any data */
194-
while (dmax) { *dest = '\0'; dmax--; dest++; }
196+
char *filler = dest;
197+
198+
while (dmax) { *filler = '\0'; dmax--; filler++; }
195199
#endif
196200
*err = RCNEGATE(EOK);
197201
return dest;
@@ -219,7 +223,9 @@ stpcpy_s(char *dest, rsize_t dmax, const char *src, errno_t *err)
219223
if (*dest == '\0') {
220224
#ifdef SAFECLIB_STR_NULL_SLACK
221225
/* null slack to clear any data */
222-
while (dmax) { *dest = '\0'; dmax--; dest++; }
226+
char *filler = dest;
227+
228+
while (dmax) { *filler = '\0'; dmax--; filler++; }
223229
#endif
224230
*err = RCNEGATE(EOK);
225231
return dest;

safeclib/stpncpy_s.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,15 @@ stpncpy_s(char *dest, rsize_t dmax, const char *src, rsize_t smax, errno_t *err)
185185

186186
#ifdef SAFECLIB_STR_NULL_SLACK
187187
/* dmwheel1: Add check to prevent destruction of overlap into destination */
188-
if ((src < dest) && ((src+dmax) >= dest)) {
188+
if ((src < dest) && ((src + smax) > dest)) {
189189
invoke_safe_str_constraint_handler("stpncpy_s: src+dmax overlaps into dest",
190190
NULL, ESOVRLP);
191191
*err = RCNEGATE(ESOVRLP);
192192
return NULL;
193193
}
194194

195195
/* dmwheel1: Add check to prevent destruction of overlap into source */
196-
if ((dest < src) && ((dest+dmax) >= src)) {
196+
if ((dest < src) && ((dest + dmax) > src)) {
197197
invoke_safe_str_constraint_handler("stpncpy_s: dest+dmax overlaps into src",
198198
NULL, ESOVRLP);
199199
*err = RCNEGATE(ESOVRLP);

safeclib/wcpcpy_s.c

+6-2
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,9 @@ wcpcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, errno_t *err)
178178
if (*dest == L'\0') {
179179
#ifdef SAFECLIB_STR_NULL_SLACK
180180
/* null slack to clear any data */
181-
while (dmax) { *dest = L'\0'; dmax--; dest++; }
181+
wchar_t *filler = dest;
182+
183+
while (dmax) { *filler = L'\0'; dmax--; filler++; }
182184
#endif
183185
*err = RCNEGATE(EOK);
184186
return dest; /* successful return */
@@ -204,7 +206,9 @@ wcpcpy_s(wchar_t* dest, rsize_t dmax, const wchar_t* src, errno_t *err)
204206
if (*dest == L'\0') {
205207
#ifdef SAFECLIB_STR_NULL_SLACK
206208
/* null slack to clear any data */
207-
while (dmax) { *dest = L'\0'; dmax--; dest++; }
209+
wchar_t *filler = dest;
210+
211+
while (dmax) { *filler = L'\0'; dmax--; filler++; }
208212
#endif
209213
*err = RCNEGATE(EOK);
210214
return dest; /* successful return */

unittests/test_stpncpy_s.c

+24-4
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,12 @@ printf("Test #%d:\n", ++testno);
428428
}
429429
/* be sure that the slack is correct */
430430
for (i=1; i<6; i++) {
431-
if (ret[i] != 'x') {
431+
#ifdef SAFECLIB_STR_NULL_SLACK
432+
const char slack = '\0';
433+
#else
434+
const char slack = 'x';
435+
#endif // SAFECLIB_STR_NULL_SLACK
436+
if (ret[i] != slack) {
432437
printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
433438
__FUNCTION__, __LINE__, i, rc );
434439
++errs;
@@ -473,7 +478,12 @@ printf("Test #%d:\n", ++testno);
473478
}
474479
/* be sure that the slack is correct */
475480
for (; i<15; i++) {
476-
if (ret[i] != 'x') {
481+
#ifdef SAFECLIB_STR_NULL_SLACK
482+
const char slack = '\0';
483+
#else
484+
const char slack = 'x';
485+
#endif // SAFECLIB_STR_NULL_SLACK
486+
if (ret[i] != slack) {
477487
printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
478488
__FUNCTION__, __LINE__, i, rc );
479489
++errs;
@@ -511,7 +521,12 @@ printf("Test #%d:\n", ++testno);
511521
} else {
512522
/* be sure that the slack is correct */
513523
for (i=1; i<5; i++) {
514-
if (ret[i] != 'x') {
524+
#ifdef SAFECLIB_STR_NULL_SLACK
525+
const char slack = '\0';
526+
#else
527+
const char slack = 'x';
528+
#endif // SAFECLIB_STR_NULL_SLACK
529+
if (ret[i] != slack) {
515530
printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
516531
__FUNCTION__, __LINE__, i, rc );
517532
++errs;
@@ -549,7 +564,12 @@ printf("Test #%d:\n", ++testno);
549564
} else {
550565
/* be sure that the slack is correct */
551566
for (i=1; i<5; i++) {
552-
if (ret[i] != 'x') {
567+
#ifdef SAFECLIB_STR_NULL_SLACK
568+
const char slack = '\0';
569+
#else
570+
const char slack = 'x';
571+
#endif // SAFECLIB_STR_NULL_SLACK
572+
if (ret[i] != slack) {
553573
printf("%s %u Incorrect Slack at returned ptr index %d Error rc=%u \n",
554574
__FUNCTION__, __LINE__, i, rc );
555575
++errs;

0 commit comments

Comments
 (0)