Skip to content

Comments

Fix UBSan with -i0: avoid division by zero#2002

Open
jackyyf wants to merge 1 commit intoesnet:masterfrom
jackyyf:master
Open

Fix UBSan with -i0: avoid division by zero#2002
jackyyf wants to merge 1 commit intoesnet:masterfrom
jackyyf:master

Conversation

@jackyyf
Copy link

@jackyyf jackyyf commented Feb 9, 2026

PLEASE NOTE the following text from the iperf3 license. Submitting a
pull request to the iperf3 repository constitutes "[making]
Enhancements available...publicly":

You are under no obligation whatsoever to provide any bug fixes, patches, or
upgrades to the features, functionality or performance of the source code
("Enhancements") to anyone; however, if you choose to make your Enhancements
available either publicly, or directly to Lawrence Berkeley National
Laboratory, without imposing a separate written license agreement for such
Enhancements, then you hereby grant the following license: a non-exclusive,
royalty-free perpetual license to install, use, modify, prepare derivative
works, incorporate into other computer software, distribute, and sublicense
such enhancements or derivative works thereof, in binary and source code form.

The complete iperf3 license is available in the LICENSE file in the
top directory of the iperf3 source tree.

  • Version of iperf3 (or development branch, such as master or
    3.1-STABLE) to which this pull request applies: master

  • Issues fixed (if any):

  • Brief description of code changes (suitable for use as a commit message):

Fix UBSan with `-i0`: avoid division by zero

The interval is explicity allowed to be zero, but we are trying to
divide by the interval without checking if it's zero, which will cause
an UBSan failure.

This commit fixed the issue by verify the interval is not zero before
division.
  • Actual UBSan report:
iperf3/src/iperf_api.c:1962:6: runtime error: inf is outside the range of representable values of type 'int'
    #0 0x555b386f6125 in iperf_parse_arguments iperf3/src/iperf_api.c:1962:6
    #1 0x555b386ef30a in main iperf3/src/main.c:117:9
<snip>

SUMMARY: UndefinedBehaviorSanitizer: float-cast-overflow iperf3/src/iperf_api.c:1962:6

The interval is explicity allowed to be zero, but we are trying to
divide by the interval without checking if it's zero, which will cause
an UBSan failure.

This commit fixed the issue by verify the interval is not zero before
division.
@jackyyf
Copy link
Author

jackyyf commented Feb 9, 2026

Sorry, forgot to include the command line used:

iperf3 -clocalhost -i0

and iperf3 will fail when compiled with -fsanitize=float-cast-overflow

@bmah888
Copy link
Contributor

bmah888 commented Feb 21, 2026

Thanks for the PR!

Quick test note: Configure iperf3 and then do something like this to get the C compiler flags (looks like this on macOS):

bmah@dock2 iperf % grep CFLAGS Makefile
CFLAGS = -g -O2 -Wall -pthread
PTHREAD_CFLAGS = -pthread

Then build with an extra argument in CFLAGS like this:

bmah@dock2 iperf % make CFLAGS="-g -O2 -Wall -pthread -fsanitize=float-cast-overflow"

THen run iperf3 like so:

src/iperf3 -c localhost -i0

@bmah888 bmah888 self-assigned this Feb 21, 2026
@bmah888 bmah888 added the bug label Feb 21, 2026
Copy link
Contributor

@bmah888 bmah888 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Just one minor suggestion.

test->settings->bitrate_limit_stats_per_interval =
(test->settings->bitrate_limit_interval <= test->stats_interval ?
(test->settings->bitrate_limit_interval <= test->stats_interval ||
test->stats_interval == 0 ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test->stats_interval is of type double, so it's probably a little more correct to check against the floating point constant 0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants