diff --git a/src/general.c b/src/general.c index 6036b695..5fbb668c 100644 --- a/src/general.c +++ b/src/general.c @@ -19,7 +19,8 @@ } /* - * Reads size bytes into the destination buffer from filename. + * Reads (size - 1) bytes into the destination buffer from filename, + * and null-terminate it. * * On success, true is returned. Otherwise, false is returned and the content * of destination is left untouched. diff --git a/src/print_battery_info.c b/src/print_battery_info.c index 096c3a90..1f567231 100644 --- a/src/print_battery_info.c +++ b/src/print_battery_info.c @@ -161,6 +161,11 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat } for (walk = buf, last = buf; (walk - buf) < 1024; walk++) { + // `*walk` (slice of `buf`) is only initialised until `null` written by `slurp()` + if (*walk == '\0') { + break; + } + if (*walk == '\n') { last = walk + 1; continue;