From 248955da4c6b3fb629c1925cfcbce298cd0fea38 Mon Sep 17 00:00:00 2001 From: Armin Novak Date: Fri, 23 May 2014 13:07:37 +0200 Subject: [PATCH] Fixed memory leak in manpage generation script. --- client/X11/generate_argument_docbook.c | 64 +++++++++++--------------- 1 file changed, 26 insertions(+), 38 deletions(-) diff --git a/client/X11/generate_argument_docbook.c b/client/X11/generate_argument_docbook.c index dd7b9d937..aaab2239c 100644 --- a/client/X11/generate_argument_docbook.c +++ b/client/X11/generate_argument_docbook.c @@ -9,33 +9,28 @@ * the argument struct. */ #include "../common/cmdline.c" -LPSTR tmp = NULL; - LPSTR tr_esc_str(LPCSTR arg) { + LPSTR tmp = NULL; size_t cs = 0, x, ds; size_t s; - - if( NULL == arg ) + if(NULL == arg) return NULL; - s = strlen(arg); - /* Find trailing whitespaces */ - while( (s > 0) && isspace(arg[s-1])) + while((s > 0) && isspace(arg[s-1])) s--; - /* Prepare a initial buffer with the size of the result string. */ - if (s) - tmp = (LPSTR)malloc(s * sizeof(CHAR)); - if( NULL == tmp ) + ds = s + 1; + if(s) + tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); + if(NULL == tmp) { fprintf(stderr, "Could not allocate string buffer."); exit(-2); } - /* Copy character for character and check, if it is necessary to escape. */ - ds = s + 1; + memset(tmp, 0, ds * sizeof(CHAR)); for(x=0; x': ds += 3; tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if( NULL == tmp ) + if(NULL == tmp) { fprintf(stderr, "Could not reallocate string buffer."); exit(-4); @@ -69,7 +64,7 @@ LPSTR tr_esc_str(LPCSTR arg) case '\'': ds += 5; tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if( NULL == tmp ) + if(NULL == tmp) { fprintf(stderr, "Could not reallocate string buffer."); exit(-5); @@ -84,7 +79,7 @@ LPSTR tr_esc_str(LPCSTR arg) case '"': ds += 5; tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if( NULL == tmp ) + if(NULL == tmp) { fprintf(stderr, "Could not reallocate string buffer."); exit(-6); @@ -99,7 +94,7 @@ LPSTR tr_esc_str(LPCSTR arg) case '&': ds += 4; tmp = (LPSTR)realloc(tmp, ds * sizeof(CHAR)); - if( NULL == tmp ) + if(NULL == tmp) { fprintf(stderr, "Could not reallocate string buffer."); exit(-7); @@ -114,11 +109,9 @@ LPSTR tr_esc_str(LPCSTR arg) tmp[cs++] = arg[x]; break; } - /* Assure, the string is '\0' terminated. */ tmp[ds-1] = '\0'; } - return tmp; } @@ -128,51 +121,46 @@ int main(int argc, char *argv[]) size_t x; const char *fname = "xfreerdp-argument.1.xml"; FILE *fp = NULL; - /* Open output file for writing, truncate if existing. */ fp = fopen(fname, "w"); - if( NULL == fp ) + if(NULL == fp) { fprintf(stderr, "Could not open '%s' for writing.", fname); return -1; } - /* The tag used as header in the manpage */ fprintf(fp, "\n"); fprintf(fp, "\tOptions\n"); fprintf(fp, "\t\t\n"); - - /* Iterate over argument struct and write data to docbook 4.5 + /* Iterate over argument struct and write data to docbook 4.5 * compatible XML */ - if( elements < 2 ) + if(elements < 2) { fprintf(stderr, "The argument array 'args' is empty, writing an empty file."); elements = 1; } - for(x=0; xName); + const char *format = tr_esc_str(arg->Format); + const char *text = tr_esc_str((LPSTR) arg->Text); fprintf(fp, "\t\t\t\n"); - if ( COMMAND_LINE_VALUE_REQUIRED == arg->Flags) - fprintf(fp, "\t\t\t\t %s\n", tr_esc_str((LPSTR) arg->Name), tr_esc_str(arg->Format) ); + if(COMMAND_LINE_VALUE_REQUIRED == arg->Flags) + fprintf(fp, "\t\t\t\t %s\n", name, format); else - fprintf(fp, "\t\t\t\t\n", tr_esc_str((LPSTR) arg->Name)); + fprintf(fp, "\t\t\t\t\n", name); fprintf(fp, "\t\t\t\t\n"); - fprintf(fp, "\t\t\t\t\t%s\n", tr_esc_str((LPSTR) arg->Text)); - + fprintf(fp, "\t\t\t\t\t%s\n", format); fprintf(fp, "\t\t\t\t\n"); fprintf(fp, "\t\t\t\n"); + free(name); + free(format); + free(text); } - fprintf(fp, "\t\t\n"); fprintf(fp, "\t\n"); fclose(fp); - - if(NULL != tmp) - free(tmp); - return 0; }