Skip to content

Commit a299046

Browse files
rdmarkmsteinert
authored andcommitted
Abort tests when encountering NULL pointers
Abort instead of assert on NULL pointer to avoid using NULL pointer as parameter in function where this leads to undefined behavior Also, consistently use ck_abort() rather than ck_abort_msg() with message, since the messages are redundant to the context of a test failure
1 parent 844a617 commit a299046

1 file changed

Lines changed: 78 additions & 36 deletions

File tree

tests/bstest.c

Lines changed: 78 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ test0_0(const char *s, const char *res)
5555
bstring b0 = bfromcstr(s);
5656
if (s) {
5757
if (b0 == NULL) {
58-
ck_abort_msg("test0_0: bfromcstr failed");
58+
ck_abort();
5959
return; /* Just a safeguard */
6060
}
6161
if (res == NULL) {
62-
ck_abort_msg("test0_0: res is NULL but s is not");
62+
ck_abort();
6363
return; /* Just a safeguard */
6464
}
6565
ret = strlen(res);
@@ -81,11 +81,11 @@ test0_1(const char *s, int len, const char *res)
8181
bstring b0 = bfromcstralloc(len, s);
8282
if (s) {
8383
if (b0 == NULL) {
84-
ck_abort_msg("test0_1: bfromcstralloc failed");
84+
ck_abort();
8585
return; /* Just a safeguard */
8686
}
8787
if (res == NULL) {
88-
ck_abort_msg("test0_1: res is NULL but s is not");
88+
ck_abort();
8989
return; /* Just a safeguard */
9090
}
9191
ret = strlen(res);
@@ -139,7 +139,7 @@ test1_0(const void *blk, int len, const char *res)
139139
bstring b0 = blk2bstr(blk, len);
140140
if (res) {
141141
if (b0 == NULL) {
142-
ck_abort_msg("test1_0: blk2bstr failed");
142+
ck_abort();
143143
return; /* Just a safeguard */
144144
}
145145
ck_assert_int_eq(b0->slen, len);
@@ -175,7 +175,7 @@ test2_0(const bstring b, char z, const unsigned char *res)
175175
char *s = bstr2cstr(b, z);
176176
if (res) {
177177
if (s == NULL) {
178-
ck_abort_msg("test2_0: bstr2cstr failed");
178+
ck_abort();
179179
return; /* Just a safeguard */
180180
}
181181
ret = strlen(s);
@@ -237,7 +237,7 @@ test3_0(const bstring b)
237237
ck_assert(b0 == NULL);
238238
} else {
239239
if (b0 == NULL) {
240-
ck_abort_msg("test3_0: bstrcpy failed");
240+
ck_abort();
241241
return; /* Just a safeguard */
242242
}
243243
ck_assert_int_eq(b0->slen, b->slen);
@@ -271,11 +271,11 @@ test4_0(const bstring b, int left, int len, const char *res)
271271
ck_assert(!b || !b->data || b->slen < 0 || len < 0);
272272
} else {
273273
if (b == NULL) {
274-
ck_abort_msg("test4_0: bmidstr failed with NULL b");
274+
ck_abort();
275275
return; /* Just a safeguard */
276276
}
277277
if (res == NULL) {
278-
ck_abort_msg("test4_0: res is NULL but b is not");
278+
ck_abort();
279279
return; /* Just a safeguard */
280280
}
281281
if (len >= 0) {
@@ -325,7 +325,7 @@ test5_0(bstring b0, const bstring b1, const char *res)
325325
b1 && b1->data && b1->slen >= 0) {
326326
b2 = bstrcpy(b0);
327327
if (b2 == NULL) {
328-
ck_abort_msg("test5_0: bstrcpy failed");
328+
ck_abort();
329329
return; /* Just a safeguard */
330330
}
331331
bwriteprotect(*b2);
@@ -337,7 +337,7 @@ test5_0(bstring b0, const bstring b1, const char *res)
337337
ret = bconcat(b2, b1);
338338
ck_assert_int_eq(b2->slen, b0->slen + b1->slen);
339339
if (res == NULL) {
340-
ck_abort_msg("test5_0: res is NULL but b0 and b1 are not");
340+
ck_abort();
341341
return; /* Just a safeguard */
342342
}
343343
ret = memcmp(b2->data, res, b2->slen);
@@ -360,12 +360,12 @@ test5_1(void)
360360
for (i = 0; i < longBstring.slen; i++) {
361361
b = bstrcpy(&longBstring);
362362
if (b == NULL) {
363-
ck_abort_msg("test5_1: bstrcpy failed");
363+
ck_abort();
364364
return; /* Just a safeguard */
365365
}
366366
c = bstrcpy(&longBstring);
367367
if (c == NULL) {
368-
ck_abort_msg("test5_1: bstrcpy failed");
368+
ck_abort();
369369
return; /* Just a safeguard */
370370
}
371371
bmid2tbstr(t, b, i, longBstring.slen);
@@ -382,12 +382,12 @@ test5_1(void)
382382
}
383383
b = bfromcstr("abcd");
384384
if (b == NULL) {
385-
ck_abort_msg("test5_1: bfromcstr failed");
385+
ck_abort();
386386
return; /* Just a safeguard */
387387
}
388388
c = bfromcstr("abcd");
389389
if (c == NULL) {
390-
ck_abort_msg("test5_1: bfromcstr failed");
390+
ck_abort();
391391
return; /* Just a safeguard */
392392
}
393393
for (i = 0; i < 100; i++) {
@@ -432,7 +432,7 @@ test6_0(bstring b, char c, const char *res)
432432
if (b && b->data && b->slen >= 0) {
433433
b0 = bstrcpy(b);
434434
if (b0 == NULL) {
435-
ck_abort_msg("test6_0: bstrcpy(b) failed");
435+
ck_abort();
436436
return; /* Just a safeguard */
437437
}
438438
bwriteprotect(*b0);
@@ -445,7 +445,7 @@ test6_0(bstring b, char c, const char *res)
445445
ck_assert_int_eq(ret, 0);
446446
ck_assert_int_eq(b0->slen, b->slen + 1);
447447
if (res == NULL) {
448-
ck_abort_msg("test6_0: res is NULL");
448+
ck_abort();
449449
return; /* Just a safeguard */
450450
}
451451
ret = strlen(res);
@@ -503,7 +503,7 @@ test7x8(int (* fnptr)(const bstring, const bstring),
503503
test7x8_0(fnptr, &shortBstring, &shortBstring, retEQ);
504504
bstring b = bstrcpy(&shortBstring);
505505
if (b == NULL) {
506-
ck_abort_msg("test7x8: bstrcpy(&shortBstring) failed");
506+
ck_abort();
507507
return; /* Just a safeguard */
508508
}
509509
b->data[1]++;
@@ -595,12 +595,12 @@ START_TEST(core_010)
595595
{
596596
bstring c = bstrcpy(&shortBstring);
597597
if (c == NULL) {
598-
ck_abort_msg("core_010: bstrcpy(&shortBstring) failed");
598+
ck_abort();
599599
return; /* Just a safeguard */
600600
}
601601
bstring b = bstrcpy(&emptyBstring);
602602
if (b == NULL) {
603-
ck_abort_msg("core_010: bstrcpy(&emptyBstring) failed");
603+
ck_abort();
604604
return; /* Just a safeguard */
605605
}
606606
/* tests with NULL */
@@ -888,7 +888,7 @@ test15_0(bstring b0, int pos, const bstring b1, unsigned char fill, char * res)
888888
b1 && b1->data && b1->slen >= 0) {
889889
b2 = bstrcpy(b0);
890890
if (b2 == NULL) {
891-
ck_abort_msg("test15_0: bstrcpy(b0) failed");
891+
ck_abort();
892892
return; /* Just a safeguard */
893893
}
894894
bwriteprotect(*b2);
@@ -909,7 +909,7 @@ test15_0(bstring b0, int pos, const bstring b1, unsigned char fill, char * res)
909909
}
910910
}
911911
if (res == NULL) {
912-
ck_abort_msg("test15_0: res is NULL");
912+
ck_abort();
913913
return; /* Just a safeguard */
914914
}
915915
ck_assert(!((ret == 0) != (pos >= 0)));
@@ -961,7 +961,10 @@ test16_0(bstring b0, int pos, const bstring b1, unsigned char fill, char *res)
961961
if (b0 && b0->data && b0->slen >= 0 &&
962962
b1 && b1->data && b1->slen >= 0) {
963963
b2 = bstrcpy(b0);
964-
ck_assert(b2 != NULL);
964+
if (b2 == NULL) {
965+
ck_abort();
966+
return; /* Just a safeguard */
967+
}
965968
bwriteprotect(*b2);
966969
ret = binsert(b2, pos, b1, fill);
967970
ck_assert_int_ne(ret, 0);
@@ -975,7 +978,10 @@ test16_0(bstring b0, int pos, const bstring b1, unsigned char fill, char *res)
975978
ck_assert(!((ret == 0) != (pos >= 0 && pos <= b2->slen)));
976979
ck_assert(!((ret == 0) != (pos >= 0 && pos <= b2->slen)));
977980
}
978-
ck_assert(res != NULL);
981+
if (res == NULL) {
982+
ck_abort();
983+
return; /* Just a safeguard */
984+
}
979985
ret = strlen(res);
980986
ck_assert_int_eq(b2->slen, ret);
981987
ret = memcmp(b2->data, res, b2->slen);
@@ -1033,7 +1039,10 @@ test17_0(bstring s1, int pos, int len, char * res)
10331039
ret = bdelete(b2, pos, len);
10341040
ck_assert(!((len >= 0) != (ret == 0)));
10351041
ck_assert(!((b2->slen > s1->slen) || (b2->slen < pos && s1->slen >= pos)));
1036-
ck_assert(res != NULL);
1042+
if (res == NULL) {
1043+
ck_abort();
1044+
return; /* Just a safeguard */
1045+
}
10371046
ret = strlen(res);
10381047
ck_assert_int_eq(b2->slen, ret);
10391048
ret = memcmp(b2->data, res, b2->slen);
@@ -1147,7 +1156,10 @@ test19_0(bstring b, int len, const char *res, int erv)
11471156
bwriteallow(*b1);
11481157
ret = bpattern(b1, len);
11491158
ck_assert_int_eq(ret, erv);
1150-
ck_assert(res != NULL);
1159+
if (res == NULL) {
1160+
ck_abort();
1161+
return; /* Just a safeguard */
1162+
}
11511163
ret = strlen(res);
11521164
ck_assert_int_eq(b1->slen, ret);
11531165
ret = memcmp(b1->data, res, b1->slen);
@@ -1286,7 +1298,10 @@ test21_0(bstring b, char sc, int ns)
12861298
struct tagbstring t;
12871299
blk2tbstr(t, &sc, 1);
12881300
l = bsplit (b, sc);
1289-
ck_assert(l != NULL);
1301+
if (l == NULL) {
1302+
ck_abort();
1303+
return; /* Just a safeguard */
1304+
}
12901305
ck_assert_int_eq(ns, l->qty);
12911306
c = bjoin (l, &t);
12921307
ret = biseq(c, b);
@@ -1309,7 +1324,10 @@ test21_1(bstring b, const bstring sc, int ns)
13091324
if (b && b->data && b->slen >= 0) {
13101325
bstring c;
13111326
l = bsplitstr (b, sc);
1312-
ck_assert(l != NULL);
1327+
if (l == NULL) {
1328+
ck_abort();
1329+
return; /* Just a safeguard */
1330+
}
13131331
ck_assert_int_eq(ns, l->qty);
13141332
c = bjoin(l, sc);
13151333
ck_assert(c != NULL);
@@ -1816,7 +1834,10 @@ test26_0(bstring b0, int pos, int len, const bstring b1,
18161834
ck_assert(!(((ret == 0) !=
18171835
(pos >= 0 && pos <= b2->slen))));
18181836
}
1819-
ck_assert(res != NULL);
1837+
if (res == NULL) {
1838+
ck_abort();
1839+
return; /* Just a safeguard */
1840+
}
18201841
ret = strlen(res);
18211842
ck_assert(b2->slen >= ret);
18221843
ret = memcmp(b2->data, res, b2->slen);
@@ -1874,7 +1895,10 @@ test27_0(bstring b0, const bstring b1, const char *res)
18741895
}
18751896
ck_assert(!(((0 != ret) && (b1 != NULL)) ||
18761897
((0 == ret) && (b1 == NULL))));
1877-
ck_assert(res != NULL);
1898+
if (res == NULL) {
1899+
ck_abort();
1900+
return; /* Just a safeguard */
1901+
}
18781902
ret = strlen(res);
18791903
ck_assert_int_eq(b2->slen, ret);
18801904
ret = memcmp(b2->data, res, b2->slen);
@@ -2056,7 +2080,10 @@ test30_0(bstring b0, const unsigned char *s, int len, const char *res)
20562080
}
20572081
ck_assert(!(((0 != ret) && (s && len >= 0)) ||
20582082
((0 == ret) && (s == NULL || len < 0))));
2059-
ck_assert(res != NULL);
2083+
if (res == NULL) {
2084+
ck_abort();
2085+
return; /* Just a safeguard */
2086+
}
20602087
ret = strlen(res);
20612088
ck_assert_int_eq(ret, b2->slen);
20622089
ret = memcmp(b2->data, res, b2->slen);
@@ -2140,7 +2167,10 @@ test31_1(bstring b0, const bstring find, const bstring replace,
21402167
bwriteallow(*b2);
21412168
ret = bfindreplacecaseless(b2, find, replace, pos);
21422169
ck_assert_int_eq(ret, BSTR_OK);
2143-
ck_assert(res != NULL);
2170+
if (res == NULL) {
2171+
ck_abort();
2172+
return; /* Just a safeguard */
2173+
}
21442174
ret = strlen(res);
21452175
ck_assert(b2->slen >= ret);
21462176
ret = memcmp(b2->data, res, b2->slen);
@@ -2347,7 +2377,10 @@ test34_0(bstring b0, const char *res)
23472377
ret = btolower(b2);
23482378
ck_assert_int_eq(b2->slen, b0->slen);
23492379
ck_assert_int_eq(ret, BSTR_OK);
2350-
ck_assert(res != NULL);
2380+
if (res == NULL) {
2381+
ck_abort();
2382+
return; /* Just a safeguard */
2383+
}
23512384
ret = strlen(res);
23522385
ck_assert_int_eq(b2->slen, ret);
23532386
ret = memcmp(b2->data, res, b2->slen);
@@ -2680,7 +2713,10 @@ test40_0(bstring b0, const bstring b1, int left, int len, const char *res)
26802713
}
26812714
ck_assert(!(((0 != ret) && (b1 != NULL)) ||
26822715
((0 == ret) && (b1 == NULL))));
2683-
ck_assert(res != NULL);
2716+
if (res == NULL) {
2717+
ck_abort();
2718+
return; /* Just a safeguard */
2719+
}
26842720
ret = strlen(res);
26852721
ck_assert_int_eq(b2->slen, ret);
26862722
ret = memcmp(b2->data, res, b2->slen);
@@ -2851,7 +2887,10 @@ test44_0(const char *str)
28512887
{
28522888
int ret = 0;
28532889
bstring b = bfromcstr("");
2854-
ck_assert(b != NULL);
2890+
if (b == NULL) {
2891+
ck_abort();
2892+
return; /* Just a safeguard */
2893+
}
28552894
if (NULL == str) {
28562895
ret = bassigncstr(NULL, "test");
28572896
ck_assert_int_eq(ret, BSTR_ERR);
@@ -2902,7 +2941,10 @@ test45_0(const char *str)
29022941
{
29032942
int ret = 0, len;
29042943
bstring b = bfromcstr("");
2905-
ck_assert(b != NULL);
2944+
if (b == NULL) {
2945+
ck_abort();
2946+
return; /* Just a safeguard */
2947+
}
29062948
if (!str) {
29072949
ret = bassignblk(NULL, "test", 4);
29082950
ck_assert_int_eq(ret, BSTR_ERR);

0 commit comments

Comments
 (0)