# HG changeset patch # User Mike Becker # Date 1306495215 -7200 # Node ID 9393eff3d2f9bc2133289fbb45591f5e263a8ef2 # Parent c3acfb3b4957c703c1c54907743df502e5f8f0f7 Fixed memory leak when exiting the programm ahead of time diff -r c3acfb3b4957 -r 9393eff3d2f9 cline.c --- a/cline.c Fri May 27 12:49:33 2011 +0200 +++ b/cline.c Fri May 27 13:20:15 2011 +0200 @@ -3,15 +3,17 @@ settings_t* new_settings_t() { settings_t *settings = malloc(sizeof(settings_t*)); -#ifdef _WIN32 - settings->fileSeparator = '\\'; -#else - settings->fileSeparator = '/'; -#endif /* _WIN32 */ - settings->suffixc = 1; - settings->recursive = false; - settings->includeSuffixes = false; - settings->matchesOnly = false; + if (settings != NULL) { + #ifdef _WIN32 + settings->fileSeparator = '\\'; + #else + settings->fileSeparator = '/'; + #endif /* _WIN32 */ + settings->suffixc = 1; + settings->recursive = false; + settings->includeSuffixes = false; + settings->matchesOnly = false; + } return settings; } @@ -53,6 +55,10 @@ // Settings settings_t *settings = new_settings_t(); + if (settings == NULL) { + fprintf(stderr, "Memory allocation failed.\n"); + return 1; + } // Program name char* prgName = strrchr(argv[0], settings->fileSeparator); @@ -84,13 +90,15 @@ if ((argflags & 2) > 0) { if ((checked & 1) > 0) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } settings->includeSuffixes = true; t++; if (t >= argc) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } suffix = argv[t]; checked |= 1; @@ -99,13 +107,15 @@ if ((argflags & 4) > 0) { if ((checked & 1) > 0) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } settings->includeSuffixes = false; t++; if (t >= argc) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } suffix = argv[t]; checked |= 1; @@ -114,7 +124,8 @@ if ((argflags & 1) > 0 || strcmp(argv[t], "--help") == 0) { if ((checked & 2) > 0) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } checked |= 2; showHelp = true; @@ -123,7 +134,8 @@ if ((argflags & 24) > 0) { if ((checked & 4) > 0) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } checked |= 4; settings->recursive = true; @@ -131,7 +143,8 @@ if ((argflags & 32) > 0) { if ((checked & 32) > 0) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } checked |= 32; settings->matchesOnly = true; @@ -140,7 +153,8 @@ if (argflags == 0) { if ((checked & 8) > 0) { printHelpText(prgName); - return -1; + destroy_settings_t(settings); + return 1; } checked |= 8; directory = argv[t]; @@ -150,6 +164,7 @@ // Show help and quit if (showHelp) { printHelpText(prgName); + destroy_settings_t(settings); return 0; } @@ -172,6 +187,7 @@ settings->suffixv = (char**) malloc(sizeof(char**)*settings->suffixc); if (settings->suffixv == NULL) { fprintf(stderr, "Memory allocation failed.\n"); + destroy_settings_t(settings); return 1; } finder = strtok(suffix, ",");