Skip to content

src/print_battery_info.c 1024 char buffer is too small and can lead to numbers being cut in half #558

Description

@adippl

Tested i3status versions: 2.15 and main d53da19a79d68b1dc9abdb953c3d2e0497b33a7a
Kernel: 6.18.8-p1-asahi-dist
Architecture aarch64
GCC:

gcc (Gentoo 15.2.1_p20260214 p5) 15.2.1 20260214
Copyright (C) 2025 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I've encountered this issue while setting up i3status on a new machine.
Estimated time on battery was surprisingly short and incorrect.

macsmc-battery driver reports it correctly

cat /sys/class/power_supply/macsmc-battery/uevent  | grep EMPTY
POWER_SUPPLY_TIME_TO_EMPTY_NOW=44640

But value read from this line into i3status memory was incorrect. i3status only read first 2 digits discarding the rest of the line.
44640 was turning into 44.

I've found that just changing buffer size used by this part of the program was enough to allow i3status to read the whole number correctly.

char buf[1024];

for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {

This patch fixed the issue for me.
It's workaround at best and not a real solution, but it may be useful for somebody.

diff --git a/src/print_battery_info.c b/src/print_battery_info.c
index 1f56723..1ab0efc 100644
--- a/src/print_battery_info.c
+++ b/src/print_battery_info.c
@@ -147,7 +147,7 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat
     char *outwalk = buffer;
 
 #if defined(__linux__)
-    char buf[1024];
+    char buf[2048];
     const char *walk, *last;
     bool watt_as_unit = false;
     int voltage = -1;
@@ -160,7 +160,7 @@ static bool slurp_battery_info(battery_info_ctx_t *ctx, struct battery_info *bat
         return false;
     }
 
-    for (walk = buf, last = buf; (walk - buf) < 1024; walk++) {
+    for (walk = buf, last = buf; (walk - buf) < 2048; walk++) {
         // `*walk` (slice of `buf`) is only initialised until `null` written by `slurp()`
         if (*walk == '\0') {
             break;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions