aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortaiyu <taiyu.len@gmail.com>2015-08-18 01:32:54 -0700
committertaiyu <taiyu.len@gmail.com>2015-08-18 01:32:54 -0700
commiteff55d0de1eee416bc4669a4d17270c80e95fab4 (patch)
tree1561981ed071c09f9549e28451baf517594f7eff
parent780893a9338fe948cbb12c3b0ce3942ec8001ccf (diff)
fixed doubling memory bug for config lines longer then 128
-rw-r--r--sway/readline.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/sway/readline.c b/sway/readline.c
index be8c35cc..dfdc3fe8 100644
--- a/sway/readline.c
+++ b/sway/readline.c
@@ -3,7 +3,7 @@
#include <stdio.h>
char *read_line(FILE *file) {
- int i = 0, length = 0, size = 128;
+ int length = 0, size = 128;
char *string = malloc(size);
if (!string) {
return NULL;
@@ -16,21 +16,20 @@ char *read_line(FILE *file) {
if (c == '\r') {
continue;
}
- if (i == size) {
- string = realloc(string, length *= 2);
+ if (length == size) {
+ string = realloc(string, size *= 2);
if (!string) {
return NULL;
}
}
- string[i++] = (char)c;
- length++;
+ string[length++] = c;
}
- if (i + 1 != size) {
+ if (length + 1 == size) {
string = realloc(string, length + 1);
if (!string) {
return NULL;
}
}
- string[i] = '\0';
+ string[length] = '\0';
return string;
}