1
0
mirror of git://git.gnupg.org/gnupg.git synced 2025-01-03 12:11:33 +01:00

common: Do not deref vars in tests after a fail().

* common/t-convert.c (test_bin2hex): Turn if conditions into if-else
chains to avoid accessing unchecked data.
(test_bin2hexcolon): Ditto.
* common/t-mapstrings.c (test_map_static_macro_string): Ditto.
* common/t-stringhelp.c (test_percent_escape): Ditto.
(test_make_filename_try): Ditto.
(test_make_absfilename_try): Ditto.
* common/t-timestuff.c (test_timegm): Ditto.
--

Note that these dereference only occur after failed regression tests.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2016-01-06 08:31:38 +01:00
parent e70f7a54f2
commit 0a00115ee2
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
4 changed files with 40 additions and 37 deletions

View File

@ -232,13 +232,13 @@ test_bin2hex (void)
p = bin2hex (stuff, 20, NULL); p = bin2hex (stuff, 20, NULL);
if (!p) if (!p)
fail (0); fail (0);
if (strcmp (p, hexstuff)) else if (strcmp (p, hexstuff))
fail (0); fail (0);
p = bin2hex (stuff, (size_t)(-1), NULL); p = bin2hex (stuff, (size_t)(-1), NULL);
if (p) if (p)
fail (0); fail (0);
if (errno != ENOMEM) else if (errno != ENOMEM)
fail (1); fail (1);
} }
@ -264,13 +264,13 @@ test_bin2hexcolon (void)
p = bin2hexcolon (stuff, 20, NULL); p = bin2hexcolon (stuff, 20, NULL);
if (!p) if (!p)
fail (0); fail (0);
if (strcmp (p, hexstuff)) else if (strcmp (p, hexstuff))
fail (0); fail (0);
p = bin2hexcolon (stuff, (size_t)(-1), NULL); p = bin2hexcolon (stuff, (size_t)(-1), NULL);
if (p) if (p)
fail (0); fail (0);
if (errno != ENOMEM) else if (errno != ENOMEM)
fail (1); fail (1);
} }

View File

@ -68,7 +68,7 @@ test_map_static_macro_string (void)
result = map_static_macro_string (tests[testno].string); result = map_static_macro_string (tests[testno].string);
if (!result) if (!result)
fail (testno); fail (testno);
if (strcmp (result, tests[testno].expected)) else if (strcmp (result, tests[testno].expected))
fail (testno); fail (testno);
if (!tests[testno].lastresult) if (!tests[testno].lastresult)
tests[testno].lastresult = result; tests[testno].lastresult = result;
@ -80,7 +80,7 @@ test_map_static_macro_string (void)
result = map_static_macro_string (tests[testno].string); result = map_static_macro_string (tests[testno].string);
if (!result) if (!result)
fail (testno); fail (testno);
if (strcmp (result, tests[testno].expected)) else if (strcmp (result, tests[testno].expected))
fail (testno); fail (testno);
if (result != tests[testno].lastresult) if (result != tests[testno].lastresult)
fail (testno); fail (testno);

View File

@ -143,7 +143,7 @@ test_percent_escape (void)
result = percent_escape (tests[testno].value, tests[testno].extra); result = percent_escape (tests[testno].value, tests[testno].extra);
if (!result) if (!result)
fail (testno); fail (testno);
if (strcmp (result, tests[testno].expected)) else if (strcmp (result, tests[testno].expected))
fail (testno); fail (testno);
xfree (result); xfree (result);
} }
@ -398,13 +398,13 @@ test_make_filename_try (void)
out = make_filename_try ("~/foo", "bar", NULL); out = make_filename_try ("~/foo", "bar", NULL);
if (!out) if (!out)
fail (2); fail (2);
if (home) else if (home)
{ {
if (strlen (out) < homelen + 7) if (strlen (out) < homelen + 7)
fail (2); fail (2);
if (strncmp (out, home, homelen)) else if (strncmp (out, home, homelen))
fail (2); fail (2);
if (strcmp (out+homelen, "/foo/bar")) else if (strcmp (out+homelen, "/foo/bar"))
fail (2); fail (2);
} }
else else
@ -417,13 +417,13 @@ test_make_filename_try (void)
out = make_filename_try ("~", "bar", NULL); out = make_filename_try ("~", "bar", NULL);
if (!out) if (!out)
fail (2); fail (2);
if (home) else if (home)
{ {
if (strlen (out) < homelen + 3) if (strlen (out) < homelen + 3)
fail (2); fail (2);
if (strncmp (out, home, homelen)) else if (strncmp (out, home, homelen))
fail (2); fail (2);
if (strcmp (out+homelen, "/bar")) else if (strcmp (out+homelen, "/bar"))
fail (2); fail (2);
} }
else else
@ -445,33 +445,33 @@ test_make_absfilename_try (void)
out = make_absfilename_try ("foo", "bar", NULL); out = make_absfilename_try ("foo", "bar", NULL);
if (!out) if (!out)
fail (0); fail (0);
if (strlen (out) < cwdlen + 7) else if (strlen (out) < cwdlen + 7)
fail (0); fail (0);
if (strncmp (out, cwd, cwdlen)) else if (strncmp (out, cwd, cwdlen))
fail (0); fail (0);
if (strcmp (out+cwdlen, "/foo/bar")) else if (strcmp (out+cwdlen, "/foo/bar"))
fail (0); fail (0);
xfree (out); xfree (out);
out = make_absfilename_try ("./foo", NULL); out = make_absfilename_try ("./foo", NULL);
if (!out) if (!out)
fail (1); fail (1);
if (strlen (out) < cwdlen + 5) else if (strlen (out) < cwdlen + 5)
fail (1); fail (1);
if (strncmp (out, cwd, cwdlen)) else if (strncmp (out, cwd, cwdlen))
fail (1); fail (1);
if (strcmp (out+cwdlen, "/./foo")) else if (strcmp (out+cwdlen, "/./foo"))
fail (1); fail (1);
xfree (out); xfree (out);
out = make_absfilename_try (".", NULL); out = make_absfilename_try (".", NULL);
if (!out) if (!out)
fail (2); fail (2);
if (strlen (out) < cwdlen) else if (strlen (out) < cwdlen)
fail (2); fail (2);
if (strncmp (out, cwd, cwdlen)) else if (strncmp (out, cwd, cwdlen))
fail (2); fail (2);
if (strcmp (out+cwdlen, "")) else if (strcmp (out+cwdlen, ""))
fail (2); fail (2);
xfree (out); xfree (out);

View File

@ -124,25 +124,28 @@ test_timegm (void)
tp = gmtime (&now); tp = gmtime (&now);
if (!tp) if (!tp)
fail (tidx); fail (tidx);
tbuf = *tp; else
tbuf2 = tbuf; {
tbuf = *tp;
tbuf2 = tbuf;
#ifdef HAVE_TIMEGM #ifdef HAVE_TIMEGM
atime = timegm (&tbuf); atime = timegm (&tbuf);
#else #else
atime = mktime (&tbuf); atime = mktime (&tbuf);
#endif #endif
if (atime == (time_t)(-1)) if (atime == (time_t)(-1))
fail (tidx); fail (tidx);
if (atime != now) else if (atime != now)
fail (tidx); fail (tidx);
tp = gmtime (&atime); tp = gmtime (&atime);
if (!tp) if (!tp)
fail (tidx); fail (tidx);
if (cmp_time_s (tp, &tbuf)) else if (cmp_time_s (tp, &tbuf))
fail (tidx); fail (tidx);
if (cmp_time_s (tp, &tbuf2)) else if (cmp_time_s (tp, &tbuf2))
fail (tidx); fail (tidx);
}
} }
} }