Page MenuHomeFreeBSD

expand_number: Tighten check of unit.
ClosedPublic

Authored by delphij on Jun 10 2023, 4:34 AM.
Tags
None
Referenced Files
F122377358: D40482.id123052.diff
Fri, Jul 4, 7:34 PM
F122350667: D40482.diff
Fri, Jul 4, 2:16 PM
F122309070: D40482.id123052.diff
Fri, Jul 4, 6:09 AM
Unknown Object (File)
Thu, Jul 3, 9:01 PM
Unknown Object (File)
Wed, Jul 2, 5:08 PM
Unknown Object (File)
Jun 1 2025, 1:31 AM
Unknown Object (File)
Jun 1 2025, 12:56 AM
Unknown Object (File)
May 31 2025, 8:47 PM
Subscribers

Details

Summary

The current code silently ignores characters after the unit as long
the unit themselves were recognized. This commit makes expand_number(3)
to fail with EINVAL if buf did not terminate after the unit character.

While I am there, also write a few test cases to validate the behavior.

MFC-after: 2 weeks

Test Plan

Run the test cases

Diff Detail

Repository
rG FreeBSD src repository
Lint
Lint Passed
Unit
No Test Coverage
Build Status
Buildable 52001
Build 48892: arc lint + arc unit

Event Timeline

lib/libutil/expand_number.c
78–81

Preexisting issue, but looks like we need to mention b in the man page.
I'm also not sure if b should mean shift = 0 or should be an optional ignored character (i.e., so that 1k and 1kb are treated identically)

91–92

it looks like this does not need a line break?

lib/libutil/tests/expand_number_test.c
49

further to my comment above I'd say this correctly tests existing behaviour, but it seems as if 1kb ought to be an accepted value

delphij added inline comments.
lib/libutil/expand_number.c
78–81

Actually I wasn't very sure why we supported b, but that was a good point. In the manual page change I'll say that the usage of b is discouraged (because some other utilities treats b as "block" and the result may be confusing).

delphij marked an inline comment as done.

Reflect reviewer comments.

Make license for test explicit per guidelines.

emaste added inline comments.
lib/libutil/tests/expand_number_test.c
53

should we check both b and B perhaps?

This revision is now accepted and ready to land.Jun 12 2023, 2:34 PM
This revision was automatically updated to reflect the committed changes.