From: brarcher Date: Sat, 25 Jan 2014 18:18:17 +0000 (+0000) Subject: simplify path and file name handling and honor const rules X-Git-Tag: 0.10.0~151 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b0834d12ffdcc1d25bbc17474563b0b35860feec;p=check simplify path and file name handling and honor const rules the path and file name variables are not const, but were assigned const strings. However, both variables could be reassigned non const data, and the path_name could be allocated data. To prevent this mix, they are both non-const and initially NULL. If they are NULL during the print, they are replaced before printing. Further, the path_name is now always free'd at the end, as it is either NULL or allocated memory. git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@1066 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- diff --git a/src/check_print.c b/src/check_print.c index c0881ba..83f51e9 100644 --- a/src/check_print.c +++ b/src/check_print.c @@ -136,8 +136,8 @@ void tr_fprint (FILE *file, TestResult *tr, enum print_output print_mode) void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode CK_ATTRIBUTE_UNUSED) { char result[10]; - char *path_name = (char*)""; - char *file_name = (char*)""; + char *path_name = NULL; + char *file_name = NULL; char *slash = NULL; switch (tr->rtype) { @@ -164,7 +164,7 @@ void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode CK_AT } if (slash == NULL) { - path_name = (char*)"."; + path_name = strdup("."); file_name = tr->file; } else { path_name = strdup(tr->file); @@ -175,8 +175,8 @@ void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode CK_AT fprintf(file, " \n", result); - fprintf(file, " %s\n", path_name); - fprintf(file, " %s:%d\n", file_name, tr->line); + fprintf(file, " %s\n", (path_name == NULL ? "" : path_name)); + fprintf(file, " %s:%d\n", (file_name == NULL ? "" : file_name), tr->line); fprintf(file, " %s\n", tr->tname); fprintf(file, " %d\n", tr->iter); fprintf(file, " %d.%06d\n", @@ -190,9 +190,7 @@ void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode CK_AT fprintf(file,"\n"); fprintf(file, " \n"); - if (slash != NULL) { - free(path_name); - } + free(path_name); } enum print_output get_env_printmode (void)