fixed doubling memory bug for config lines longer then 128

This commit is contained in:
taiyu 2015-08-18 01:32:54 -07:00
parent 780893a933
commit eff55d0de1

View file

@ -3,7 +3,7 @@
#include <stdio.h> #include <stdio.h>
char *read_line(FILE *file) { char *read_line(FILE *file) {
int i = 0, length = 0, size = 128; int length = 0, size = 128;
char *string = malloc(size); char *string = malloc(size);
if (!string) { if (!string) {
return NULL; return NULL;
@ -16,21 +16,20 @@ char *read_line(FILE *file) {
if (c == '\r') { if (c == '\r') {
continue; continue;
} }
if (i == size) { if (length == size) {
string = realloc(string, length *= 2); string = realloc(string, size *= 2);
if (!string) { if (!string) {
return NULL; return NULL;
} }
} }
string[i++] = (char)c; string[length++] = c;
length++;
} }
if (i + 1 != size) { if (length + 1 == size) {
string = realloc(string, length + 1); string = realloc(string, length + 1);
if (!string) { if (!string) {
return NULL; return NULL;
} }
} }
string[i] = '\0'; string[length] = '\0';
return string; return string;
} }