xaizek / stic (License: MIT) (since 2018-12-07)
Simple Tests In C with optional automatic test registration in C.
Commit 3164b5fa8cdfe58cc160d15757ad0f2ec6e3aab5

Fix buffer overflows in stic.c
Author: xaizek
Author date (UTC): 2023-07-23 12:44
Committer name: xaizek
Committer date (UTC): 2023-07-23 13:42
Parent(s): ff42f88a8aa57ffc796abbec6506a77593f0ac57
Signing key: 99DC5E4DB05F6BE2
Tree: 6f4e50b25448ae3f79dd4a0ea312e273c878da59
File Lines added Lines deleted
src/stic.c 23 23
File src/stic.c changed (mode: 100644) (index ad5abf2..8d854d9)
... ... void stic_assert_non_null(const void *value, const char function[], const char f
296 296 void stic_assert_int_equal(int expected, int actual, const char* function, const char file[], unsigned int line) void stic_assert_int_equal(int expected, int actual, const char* function, const char file[], unsigned int line)
297 297 { {
298 298 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
299 sprintf(s, "Expected %d but was %d", expected, actual);
299 snprintf(s, sizeof(s), "Expected %d but was %d", expected, actual);
300 300 stic_simple_test_result(expected==actual, s, function, file, line); stic_simple_test_result(expected==actual, s, function, file, line);
301 301 } }
302 302
303 303 void stic_assert_ulong_equal(unsigned long expected, unsigned long actual, const char* function, const char file[], unsigned int line) void stic_assert_ulong_equal(unsigned long expected, unsigned long actual, const char* function, const char file[], unsigned int line)
304 304 { {
305 305 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
306 sprintf(s, "Expected %lu but was %lu", expected, actual);
306 snprintf(s, sizeof(s), "Expected %lu but was %lu", expected, actual);
307 307 stic_simple_test_result(expected==actual, s, function, file, line); stic_simple_test_result(expected==actual, s, function, file, line);
308 308 } }
309 309
 
... ... void stic_assert_float_equal( float expected, float actual, float delta, const c
311 311 { {
312 312 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
313 313 float result = expected-actual; float result = expected-actual;
314 sprintf(s, "Expected %f but was %f", expected, actual);
314 snprintf(s, sizeof(s), "Expected %f but was %f", expected, actual);
315 315 if(result < 0.0) result = 0.0f - result; if(result < 0.0) result = 0.0f - result;
316 316 stic_simple_test_result( result <= delta, s, function, file, line); stic_simple_test_result( result <= delta, s, function, file, line);
317 317 } }
 
... ... void stic_assert_double_equal( double expected, double actual, double delta, con
320 320 { {
321 321 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
322 322 double result = expected-actual; double result = expected-actual;
323 sprintf(s, "Expected %f but was %f", expected, actual);
323 snprintf(s, sizeof(s), "Expected %f but was %f", expected, actual);
324 324 if(result < 0.0) result = 0.0 - result; if(result < 0.0) result = 0.0 - result;
325 325 stic_simple_test_result( result <= delta, s, function, file, line); stic_simple_test_result( result <= delta, s, function, file, line);
326 326 } }
 
... ... void stic_assert_string_equal(const char* expected, const char* actual, const ch
332 332
333 333 if ((expected == (char *)0) && (actual == (char *)0)) if ((expected == (char *)0) && (actual == (char *)0))
334 334 { {
335 sprintf(s, "Expected <NULL> but was <NULL>");
335 snprintf(s, sizeof(s), "Expected <NULL> but was <NULL>");
336 336 comparison = 1; comparison = 1;
337 337 } }
338 338 else if (expected == (char *)0) else if (expected == (char *)0)
339 339 { {
340 sprintf(s, "Expected <NULL> but was \"%s\"", actual);
340 snprintf(s, sizeof(s), "Expected <NULL> but was \"%s\"", actual);
341 341 comparison = 0; comparison = 0;
342 342 } }
343 343 else if (actual == (char *)0) else if (actual == (char *)0)
344 344 { {
345 sprintf(s, "Expected \"%s\" but was <NULL>", expected);
345 snprintf(s, sizeof(s), "Expected \"%s\" but was <NULL>", expected);
346 346 comparison = 0; comparison = 0;
347 347 } }
348 348 else else
349 349 { {
350 350 comparison = strcmp(expected, actual) == 0; comparison = strcmp(expected, actual) == 0;
351 sprintf(s, "Expected \"%s\" but was \"%s\"", expected, actual);
351 snprintf(s, sizeof(s), "Expected \"%s\" but was \"%s\"", expected, actual);
352 352 } }
353 353
354 354 stic_simple_test_result(comparison, s, function, file, line); stic_simple_test_result(comparison, s, function, file, line);
 
... ... void stic_assert_wstring_equal(const wchar_t expected[], const wchar_t actual[],
361 361
362 362 if ((expected == NULL) && (actual == NULL)) if ((expected == NULL) && (actual == NULL))
363 363 { {
364 sprintf(s, "Expected <NULL> but was <NULL>");
364 snprintf(s, sizeof(s), "Expected <NULL> but was <NULL>");
365 365 comparison = 1; comparison = 1;
366 366 } }
367 367 else if (expected == NULL) else if (expected == NULL)
368 368 { {
369 369 #ifdef STIC_C99 #ifdef STIC_C99
370 sprintf(s, "Expected <NULL> but was \"%ls\"", actual);
370 snprintf(s, sizeof(s), "Expected <NULL> but was \"%ls\"", actual);
371 371 #else #else
372 sprintf(s, "Expected <NULL> but was wide string");
372 snprintf(s, sizeof(s), "Expected <NULL> but was wide string");
373 373 #endif #endif
374 374 comparison = 0; comparison = 0;
375 375 } }
376 376 else if (actual == NULL) else if (actual == NULL)
377 377 { {
378 378 #ifdef STIC_C99 #ifdef STIC_C99
379 sprintf(s, "Expected \"%ls\" but was <NULL>", expected);
379 snprintf(s, sizeof(s), "Expected \"%ls\" but was <NULL>", expected);
380 380 #else #else
381 sprintf(s, "Expected wide string but was <NULL>");
381 snprintf(s, sizeof(s), "Expected wide string but was <NULL>");
382 382 #endif #endif
383 383 comparison = 0; comparison = 0;
384 384 } }
 
... ... void stic_assert_wstring_equal(const wchar_t expected[], const wchar_t actual[],
386 386 { {
387 387 comparison = wcscmp(expected, actual) == 0; comparison = wcscmp(expected, actual) == 0;
388 388 #ifdef STIC_C99 #ifdef STIC_C99
389 sprintf(s, "Expected \"%ls\" but was \"%ls\"", expected, actual);
389 snprintf(s, sizeof(s), "Expected \"%ls\" but was \"%ls\"", expected, actual);
390 390 #else #else
391 sprintf(s, "Expected wide string doesn't match");
391 snprintf(s, sizeof(s), "Expected wide string doesn't match");
392 392 #endif #endif
393 393 } }
394 394
 
... ... void stic_assert_wstring_equal(const wchar_t expected[], const wchar_t actual[],
398 398 void stic_assert_string_ends_with(const char* expected, const char* actual, const char* function, const char file[], unsigned int line) void stic_assert_string_ends_with(const char* expected, const char* actual, const char* function, const char file[], unsigned int line)
399 399 { {
400 400 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
401 sprintf(s, "Expected \"%s\" to end with \"%s\"", actual, expected);
401 snprintf(s, sizeof(s), "Expected \"%s\" to end with \"%s\"", actual, expected);
402 402 stic_simple_test_result(strcmp(expected, actual+(strlen(actual)-strlen(expected)))==0, s, function, file, line); stic_simple_test_result(strcmp(expected, actual+(strlen(actual)-strlen(expected)))==0, s, function, file, line);
403 403 } }
404 404
405 405 void stic_assert_string_starts_with(const char* expected, const char* actual, const char* function, const char file[], unsigned int line) void stic_assert_string_starts_with(const char* expected, const char* actual, const char* function, const char file[], unsigned int line)
406 406 { {
407 407 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
408 sprintf(s, "Expected \"%s\" to start with \"%s\"", actual, expected);
408 snprintf(s, sizeof(s), "Expected \"%s\" to start with \"%s\"", actual, expected);
409 409 stic_simple_test_result(strncmp(expected, actual, strlen(expected))==0, s, function, file, line); stic_simple_test_result(strncmp(expected, actual, strlen(expected))==0, s, function, file, line);
410 410 } }
411 411
412 412 void stic_assert_string_contains(const char* expected, const char* actual, const char* function, const char file[], unsigned int line) void stic_assert_string_contains(const char* expected, const char* actual, const char* function, const char file[], unsigned int line)
413 413 { {
414 414 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
415 sprintf(s, "Expected \"%s\" to be in \"%s\"", expected, actual);
415 snprintf(s, sizeof(s), "Expected \"%s\" to be in \"%s\"", expected, actual);
416 416 stic_simple_test_result(strstr(actual, expected)!=0, s, function, file, line); stic_simple_test_result(strstr(actual, expected)!=0, s, function, file, line);
417 417 } }
418 418
419 419 void stic_assert_string_doesnt_contain(const char* expected, const char* actual, const char* function, const char file[], unsigned int line) void stic_assert_string_doesnt_contain(const char* expected, const char* actual, const char* function, const char file[], unsigned int line)
420 420 { {
421 421 char s[STIC_PRINT_BUFFER_SIZE]; char s[STIC_PRINT_BUFFER_SIZE];
422 sprintf(s, "Expected \"%s\" not to have \"%s\" in it", actual, expected);
422 snprintf(s, sizeof(s), "Expected \"%s\" not to have \"%s\" in it", actual, expected);
423 423 stic_simple_test_result(strstr(actual, expected)==0, s, function, file, line); stic_simple_test_result(strstr(actual, expected)==0, s, function, file, line);
424 424 } }
425 425
 
... ... void stic_test_fixture_end()
470 470 printf("\n"); printf("\n");
471 471 } }
472 472
473 sprintf(s, "%d test%s run %d check%s failed", nrun, nrun == 1 ? "" : "s",
473 snprintf(s, sizeof(s), "%d test%s run %d check%s failed", nrun, nrun == 1 ? "" : "s",
474 474 nfailed, nfailed == 1 ? "" : "s"); nfailed, nfailed == 1 ? "" : "s");
475 475
476 476 stic_header_printer(s, stic_screen_width, ' '); stic_header_printer(s, stic_screen_width, ' ');
 
... ... int run_tests(stic_void_void tests)
560 560
561 561 if (end - start == 0) if (end - start == 0)
562 562 { {
563 sprintf(time,"< 1 ms");
563 snprintf(time, sizeof(time), "< 1 ms");
564 564 } }
565 565 else else
566 566 { {
567 sprintf(time,"%lu ms",end - start);
567 snprintf(time, sizeof(time), "%lu ms",end - start);
568 568 } }
569 569
570 sprintf(s,"%d check%s :: %d run test%s :: %d skipped test%s :: %s",
570 snprintf(s, sizeof(s), "%d check%s :: %d run test%s :: %d skipped test%s :: %s",
571 571 stic_checks_passed + stic_checks_failed, stic_checks_passed + stic_checks_failed,
572 572 stic_checks_passed + stic_checks_failed == 1 ? "" : "s", stic_checks_passed + stic_checks_failed == 1 ? "" : "s",
573 573 stic_tests_run, stic_tests_run == 1 ? "" : "s", stic_tests_run, stic_tests_run == 1 ? "" : "s",
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/stic

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/stic

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master