Commit: 5b0c78febc4d50b75e7da549df9d12955d5d2135
Parent: 064c737276df2afe9dd2c93068275e1e3238d45a
Author: Randy Palamar
Date: Wed, 25 Dec 2024 10:03:25 -0700
linux/battery_info: make sure state is 0 terminated
Also be a little less haphazard about calculating needed path length.
Diffstat:
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/blocks/linux/battery_info.c b/blocks/linux/battery_info.c
@@ -40,7 +40,9 @@ static BLOCK_UPDATE_FN(battery_info_update)
.data = (u8 *)state_buffer}));
if (state.len <= 0) state = s8("Unknown");
lbd->path_base.write_index = sidx;
+ state.data[state.len] = 0;
+ i64 len;
/* NOTE(rnp): proper devices use negative power to indicate discharging but that
* is not always the case. The status string can mostly be trusted */
if (s8_equal(state, s8("Discharging"))) {
@@ -53,14 +55,13 @@ static BLOCK_UPDATE_FN(battery_info_update)
h = timeleft;
m = (timeleft - (f64)h) * 60;
- i64 len = snprintf(buffer, sizeof(buffer), "%s%d%% (%d:%02d)%s", warn? pre : "",
- (i32)percent, h, m, warn? suf : "");
- buffer[len] = 0;
+ len = snprintf(buffer, sizeof(buffer), "%s%d%% (%d:%02d)%s", warn? pre : "",
+ (i32)percent, h, m, warn? suf : "");
} else {
- i64 len = snprintf(buffer, sizeof(buffer), "%s%d%% (%s)%s", warn? pre : "",
- (i32)percent, (char *)state.data, warn? suf : "");
- buffer[len] = 0;
+ len = snprintf(buffer, sizeof(buffer), "%s%d%% (%s)%s", warn? pre : "",
+ (i32)percent, (char *)state.data, warn? suf : "");
}
+ buffer[len] = 0;
b->len = snprintf(b->data, sizeof(b->data), b->fmt, buffer);
return 1;
@@ -79,11 +80,11 @@ static BLOCK_INIT_FN(battery_info_init)
b->user_data = lbd = push_struct(a, struct linux_battery_data);
size max_length = 0;
- #define X(cstr) if (sizeof(cstr) > max_length) max_length = sizeof(cstr);
+ #define X(cstr) if ((sizeof(cstr) - 1) > max_length) max_length = sizeof(cstr) - 1;
LINUX_BAT_INFO_STRS
#undef X
- size needed_length = max_length + sizeof("/sys/class/power_supply/") - 1 + ba->bat.len;
+ size needed_length = max_length + ba->bat.len + sizeof("/sys/class/power_supply/");
lbd->path_base = stream_alloc(a, needed_length);
stream_push_s8(&lbd->path_base, s8("/sys/class/power_supply/"));