I am getting the following warnings when I run splint on some code I am studying:
help.c: (in function fill_strings) help.c:30:3: Implicitly only storage options->min_string (type char *) not released before assignment: options->min_string = min_string A memory leak has been detected. Only-qualified storage is not released before the last reference to it is lost. (Use -mustfreeonly to inhibit warning) help.c:32:8: Kept storage min_string passed as only param: free (min_string) storage is transferred to a non-temporary reference after being passed as keep parameter. The storage may be released or new aliases created. (Use -kepttrans to inhibit warning) help.c:30:3: Storage min_string becomes kept help.c: (in function main) help.c:60:3: Implicitly only storage options->low_charset (type char *) not released before assignment: options->low_charset = charset help.c:62:3: Implicitly only storage options->startstring (type char *) not released before assignment (starthere aliases argv[2]): options->startstring = starthere I am not sure what the warnings mean. The program works but it crashes at the end. When I run valgrind I get all sorts of memory issues. I figure the crash is related to the memory issues and the warnings. All of the memory issues are copies of the above messages. If you can help me understand the problem with the code below I fix all of the other issues and then maybe I can get the program to work the way I want it to. I have simplified the code to: #include <stdio.h> #include <stdlib.h> #include <string.h> static const char lowercase[] = "alphaomega"; typedef struct opts_struct { char *low_charset; size_t clen; char *startstring; size_t starthere; char *min_string; } options_type; static void fill_strings(options_type *options) { size_t i; char *min_string; min_string = calloc(options->starthere + 1,sizeof(char)); if (min_string == NULL) { printf("can't allocate memory for min_string\n"); exit(EXIT_FAILURE); } min_string[options->starthere] = '\0'; if (options->startstring) for (i = 0; i < options->starthere; i++) min_string[i] = options->startstring[i]; options->min_string = min_string; free(min_string); } int main(int argc, char **argv) { char *charset; char *starthere = NULL; options_type *options = NULL; static const options_type options_type0; options = calloc(200, sizeof(*options)); if (options == NULL) { printf("unable to allocate memory for options\n"); exit(EXIT_FAILURE); } *options = options_type0; charset = calloc(27, sizeof(char)); if (charset == NULL) { printf("can't allocate memory\n"); exit(EXIT_FAILURE); } memcpy(charset, lowercase, 26); options->starthere = (size_t)atoi(argv[1]); starthere = argv[2]; options->low_charset = charset; options->clen = charset ? strlen(charset) : 0; options->startstring = starthere; fill_strings(options); free(options); return 0; } Thanks,
_______________________________________________ splint-discuss mailing list splint-discuss@mail.cs.virginia.edu http://www.cs.virginia.edu/mailman/listinfo/splint-discuss