44 * This file is part of clar, distributed under the ISC license.
55 * For full terms see the included COPYING file.
66 */
7- #include <assert.h>
7+
8+ #define _BSD_SOURCE
9+ #define _DARWIN_C_SOURCE
10+ #define _DEFAULT_SOURCE
11+
12+ #include <errno.h>
813#include <setjmp.h>
914#include <stdlib.h>
1015#include <stdio.h>
1318#include <stdarg.h>
1419#include <wchar.h>
1520#include <time.h>
21+ #include <inttypes.h>
1622
1723/* required for sandboxing */
1824#include <sys/types.h>
1925#include <sys/stat.h>
2026
27+ #if defined(__UCLIBC__ ) && ! defined(__UCLIBC_HAS_WCHAR__ )
28+ /*
29+ * uClibc can optionally be built without wchar support, in which case
30+ * the installed <wchar.h> is a stub that only defines the `whar_t`
31+ * type but none of the functions typically declared by it.
32+ */
33+ #else
34+ # define CLAR_HAVE_WCHAR
35+ #endif
36+
2137#ifdef _WIN32
2238# define WIN32_LEAN_AND_MEAN
2339# include <windows.h>
2844
2945# ifndef stat
3046# define stat (path , st ) _stat(path, st)
47+ typedef struct _stat STAT_T ;
48+ # else
49+ typedef struct stat STAT_T ;
3150# endif
3251# ifndef mkdir
3352# define mkdir (path , mode ) _mkdir(path)
6079# else
6180# define p_snprintf snprintf
6281# endif
63-
64- # ifndef PRIuZ
65- # define PRIuZ "Iu"
66- # endif
67- # ifndef PRIxZ
68- # define PRIxZ "Ix"
69- # endif
70-
71- # if defined(_MSC_VER ) || (defined(__MINGW32__ ) && !defined(__MINGW64_VERSION_MAJOR ))
72- typedef struct stat STAT_T ;
73- # else
74- typedef struct _stat STAT_T ;
75- # endif
7682#else
7783# include <sys/wait.h> /* waitpid(2) */
7884# include <unistd.h>
7985# define _MAIN_CC
8086# define p_snprintf snprintf
81- # ifndef PRIuZ
82- # define PRIuZ "zu"
83- # endif
84- # ifndef PRIxZ
85- # define PRIxZ "zx"
86- # endif
8787 typedef struct stat STAT_T ;
8888#endif
8989
@@ -102,7 +102,7 @@ fixture_path(const char *base, const char *fixture_name);
102102struct clar_error {
103103 const char * file ;
104104 const char * function ;
105- size_t line_number ;
105+ uintmax_t line_number ;
106106 const char * error_msg ;
107107 char * description ;
108108
@@ -195,11 +195,12 @@ static void clar_print_shutdown(int test_count, int suite_count, int error_count
195195static void clar_print_error (int num , const struct clar_report * report , const struct clar_error * error );
196196static void clar_print_ontest (const char * suite_name , const char * test_name , int test_number , enum cl_test_status failed );
197197static void clar_print_onsuite (const char * suite_name , int suite_index );
198+ static void clar_print_onabortv (const char * msg , va_list argp );
198199static void clar_print_onabort (const char * msg , ...);
199200
200201/* From clar_sandbox.c */
201202static void clar_unsandbox (void );
202- static int clar_sandbox (void );
203+ static void clar_sandbox (void );
203204
204205/* From summary.h */
205206static struct clar_summary * clar_summary_init (const char * filename );
@@ -218,6 +219,15 @@ static int clar_summary_shutdown(struct clar_summary *fp);
218219 _clar.trace_payload); \
219220 } while (0)
220221
222+ static void clar_abort (const char * msg , ...)
223+ {
224+ va_list argp ;
225+ va_start (argp , msg );
226+ clar_print_onabortv (msg , argp );
227+ va_end (argp );
228+ exit (-1 );
229+ }
230+
221231void cl_trace_register (cl_trace_cb * cb , void * payload )
222232{
223233 _clar .pfn_trace_cb = cb ;
@@ -271,9 +281,7 @@ static double clar_time_diff(clar_time *start, clar_time *end)
271281
272282static void clar_time_now (clar_time * out )
273283{
274- struct timezone tz ;
275-
276- gettimeofday (out , & tz );
284+ gettimeofday (out , NULL );
277285}
278286
279287static double clar_time_diff (clar_time * start , clar_time * end )
@@ -386,7 +394,8 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
386394
387395 _clar .active_test = test [i ].name ;
388396
389- report = calloc (1 , sizeof (struct clar_report ));
397+ if ((report = calloc (1 , sizeof (* report ))) == NULL )
398+ clar_abort ("Failed to allocate report.\n" );
390399 report -> suite = _clar .active_suite ;
391400 report -> test = _clar .active_test ;
392401 report -> test_number = _clar .tests_ran ;
@@ -479,9 +488,10 @@ clar_parse_args(int argc, char **argv)
479488
480489 switch (action ) {
481490 case 's' : {
482- struct clar_explicit * explicit =
483- calloc (1 , sizeof (struct clar_explicit ));
484- assert (explicit );
491+ struct clar_explicit * explicit ;
492+
493+ if ((explicit = calloc (1 , sizeof (* explicit ))) == NULL )
494+ clar_abort ("Failed to allocate explicit test.\n" );
485495
486496 explicit -> suite_idx = j ;
487497 explicit -> filter = argument ;
@@ -505,10 +515,8 @@ clar_parse_args(int argc, char **argv)
505515 }
506516 }
507517
508- if (!found ) {
509- clar_print_onabort ("No suite matching '%s' found.\n" , argument );
510- exit (-1 );
511- }
518+ if (!found )
519+ clar_abort ("No suite matching '%s' found.\n" , argument );
512520 break ;
513521 }
514522
@@ -540,11 +548,17 @@ clar_parse_args(int argc, char **argv)
540548 case 'r' :
541549 _clar .write_summary = 1 ;
542550 free (_clar .summary_filename );
543- _clar .summary_filename = * (argument + 2 ) ? strdup (argument + 2 ) : NULL ;
551+ if (* (argument + 2 )) {
552+ if ((_clar .summary_filename = strdup (argument + 2 )) == NULL )
553+ clar_abort ("Failed to allocate summary filename.\n" );
554+ } else {
555+ _clar .summary_filename = NULL ;
556+ }
544557 break ;
545558
546559 default :
547- assert (!"Unexpected commandline argument!" );
560+ clar_abort ("Unexpected commandline argument '%s'.\n" ,
561+ argument [1 ]);
548562 }
549563 }
550564}
@@ -566,22 +580,18 @@ clar_test_init(int argc, char **argv)
566580 if (!_clar .summary_filename &&
567581 (summary_env = getenv ("CLAR_SUMMARY" )) != NULL ) {
568582 _clar .write_summary = 1 ;
569- _clar .summary_filename = strdup (summary_env );
583+ if ((_clar .summary_filename = strdup (summary_env )) == NULL )
584+ clar_abort ("Failed to allocate summary filename.\n" );
570585 }
571586
572587 if (_clar .write_summary && !_clar .summary_filename )
573- _clar .summary_filename = strdup ("summary.xml" );
588+ if ((_clar .summary_filename = strdup ("summary.xml" )) == NULL )
589+ clar_abort ("Failed to allocate summary filename.\n" );
574590
575- if (_clar .write_summary &&
576- !(_clar .summary = clar_summary_init (_clar .summary_filename ))) {
577- clar_print_onabort ("Failed to open the summary file\n" );
578- exit (-1 );
579- }
591+ if (_clar .write_summary )
592+ _clar .summary = clar_summary_init (_clar .summary_filename );
580593
581- if (clar_sandbox () < 0 ) {
582- clar_print_onabort ("Failed to sandbox the test runner.\n" );
583- exit (-1 );
584- }
594+ clar_sandbox ();
585595}
586596
587597int
@@ -615,10 +625,9 @@ clar_test_shutdown(void)
615625
616626 clar_unsandbox ();
617627
618- if (_clar .write_summary && clar_summary_shutdown (_clar .summary ) < 0 ) {
619- clar_print_onabort ("Failed to write the summary file\n" );
620- exit (-1 );
621- }
628+ if (_clar .write_summary && clar_summary_shutdown (_clar .summary ) < 0 )
629+ clar_abort ("Failed to write the summary file '%s: %s.\n" ,
630+ _clar .summary_filename , strerror (errno ));
622631
623632 for (explicit = _clar .explicit ; explicit ; explicit = explicit_next ) {
624633 explicit_next = explicit -> next ;
@@ -649,7 +658,7 @@ static void abort_test(void)
649658{
650659 if (!_clar .trampoline_enabled ) {
651660 clar_print_onabort (
652- "Fatal error: a cleanup method raised an exception." );
661+ "Fatal error: a cleanup method raised an exception.\n " );
653662 clar_report_errors (_clar .last_report );
654663 exit (-1 );
655664 }
@@ -673,7 +682,10 @@ void clar__fail(
673682 const char * description ,
674683 int should_abort )
675684{
676- struct clar_error * error = calloc (1 , sizeof (struct clar_error ));
685+ struct clar_error * error ;
686+
687+ if ((error = calloc (1 , sizeof (* error ))) == NULL )
688+ clar_abort ("Failed to allocate error.\n" );
677689
678690 if (_clar .last_report -> errors == NULL )
679691 _clar .last_report -> errors = error ;
@@ -688,8 +700,9 @@ void clar__fail(
688700 error -> line_number = line ;
689701 error -> error_msg = error_msg ;
690702
691- if (description != NULL )
692- error -> description = strdup (description );
703+ if (description != NULL &&
704+ (error -> description = strdup (description )) == NULL )
705+ clar_abort ("Failed to allocate description.\n" );
693706
694707 _clar .total_errors ++ ;
695708 _clar .last_report -> status = CL_TEST_FAILURE ;
@@ -763,6 +776,7 @@ void clar__assert_equal(
763776 }
764777 }
765778 }
779+ #ifdef CLAR_HAVE_WCHAR
766780 else if (!strcmp ("%ls" , fmt )) {
767781 const wchar_t * wcs1 = va_arg (args , const wchar_t * );
768782 const wchar_t * wcs2 = va_arg (args , const wchar_t * );
@@ -798,8 +812,9 @@ void clar__assert_equal(
798812 }
799813 }
800814 }
801- else if (!strcmp ("%" PRIuZ , fmt ) || !strcmp ("%" PRIxZ , fmt )) {
802- size_t sz1 = va_arg (args , size_t ), sz2 = va_arg (args , size_t );
815+ #endif /* CLAR_HAVE_WCHAR */
816+ else if (!strcmp ("%" PRIuMAX , fmt ) || !strcmp ("%" PRIxMAX , fmt )) {
817+ uintmax_t sz1 = va_arg (args , uintmax_t ), sz2 = va_arg (args , uintmax_t );
803818 is_equal = (sz1 == sz2 );
804819 if (!is_equal ) {
805820 int offset = p_snprintf (buf , sizeof (buf ), fmt , sz1 );
0 commit comments