Commit 320c0928 authored by anthraxx's avatar anthraxx
Browse files

fixing memleak of input char*

- adding missing free() calls
- dont malloc ourselves as getline() also does a malloc
  (and anyway called realloc before if the size_t was
   smaller then the actual len(input-line) to behaviour
   is the same!)
parent 295b44af
...@@ -3,23 +3,26 @@ ...@@ -3,23 +3,26 @@
int main() { int main() {
int bytes_read; int bytes_read;
size_t nbytes = 10; size_t nbytes = 0;
char **my_string = malloc(sizeof(char**)); char *my_string = NULL;
char *password = "23door42\n"; char *password = "23door42\n";
puts("Please enter Password: "); puts("Please enter Password: ");
bytes_read = getline(my_string, &nbytes, stdin); bytes_read = getline(&my_string, &nbytes, stdin);
if(bytes_read == -1) { if(bytes_read == -1) {
puts("Error"); puts("Error");
free(my_string);
return -1; return -1;
} else { } else {
if (strcmp(*my_string, password) == 0) { if (strcmp(my_string, password) == 0) {
puts("Success"); puts("Success");
free(my_string);
return 0; return 0;
} else { } else {
puts("How about no?!"); puts("How about no?!");
free(my_string);
return -1; return -1;
} }
} }
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment