Bug 12926 - embedded "wc" command has overflow issues
Summary: embedded "wc" command has overflow issues
Status: RESOLVED FIXED
Alias: None
Product: Busybox
Classification: Unclassified
Component: Other (show other bugs)
Version: unspecified
Hardware: PC Linux
: P5 normal
Target Milestone: ---
Assignee: unassigned
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-05-20 11:47 UTC by Benjamin Brecht
Modified: 2020-05-25 07:49 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Benjamin Brecht 2020-05-20 11:47:38 UTC
We use BusyBox within Alpine Linux 3.10 in a docker container.
Today we found out, that the "wc" utility is counting wrong above a certain size.
It looks to me like a overflow issue.


Example on Ubuntu Linux (ok):
# 1000 Byte (ok)
cat /dev/zero | pv --stop-at-size -s 1000 | wc -c
>> 1000

# 4 Gib (ok)
cat /dev/zero | pv --stop-at-size -s 4294967296 | wc -c
>> 4294967296


Example on Busybox (overflow at ~4 Gib):

#  4 Gib minus 1 Byte (ok)
cat /dev/zero | pv --stop-at-size -s 4294967295 | wc -c
>> 4294967295

# 4 Gib (overflow)
cat /dev/zero | pv --stop-at-size -s 4294967296 | wc -c
>> 0
Comment 1 Benjamin Brecht 2020-05-25 06:28:34 UTC
Code from: coreutils/wc.c

#if ENABLE_FEATURE_WC_LARGE
# define COUNT_T unsigned long long
# define COUNT_FMT "llu"
#else
# define COUNT_T unsigned
# define COUNT_FMT "u"
#endif

Can you enable this flag, so that the bigger variable type is used for counting?
Comment 2 Ron Yorston 2020-05-25 07:11:34 UTC
FEATURE_WC_LARGE is enabled by default. It looks as though the Alpine Linux developers have turned it off in their build. If you want it enabled there you'd have to ask them.
Comment 3 Benjamin Brecht 2020-05-25 07:28:16 UTC
Thanks for the pointer.

The bug was fixed in https://gitlab.alpinelinux.org/alpine/aports

alpine/aports$ git annotate main/busybox/busyboxconfig | grep WC_LARGE
bcf80db9586	(Sören Tempel	2020-03-13 12:51:36 +0100	336)CONFIG_FEATURE_WC_LARGE=y

git show bcf80db9586
...
@@ -333,7 +333,7 @@ CONFIG_UUDECODE=y
 CONFIG_BASE64=y
 CONFIG_UUENCODE=y
 CONFIG_WC=y
-# CONFIG_FEATURE_WC_LARGE is not set
+CONFIG_FEATURE_WC_LARGE=y

I will try a newer version of alpine.
Comment 4 Benjamin Brecht 2020-05-25 07:49:36 UTC
Bug is fixed in alpine >= 3.11.6.