forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sscanf: Fix integer overflow with sscanf field width
Fixes 5380975 ("sscanf: don't ignore field widths for numeric conversions"). sscanf overflows integers with simple strings such as dates. As an example, consider int r = sscanf("20190523123456", "%4d%2d%2d%2d%2d%2d", &year, &month, &day, &hour, &minute, &second); printk("%d %04d-%02d-%2d %02d:%02d:%02d\n", r, year, month, day, hour, minute, second); On a 32-bit machine this prints 6 0000-05-23 12:34:56 where the year is zero, and not 2019 as expected. The reason is that sscanf attempts to read 20190523123456 as a whole integer, and then divide it with 10^10 to obtain 2019, which obviously fails. Of course, 64-bit machines fail similarly on longer numerical strings. The idea for a fix is to have a variant of _parse_integer() called _parse_integer_end(), with the ability to stop consuming digits. The functions simple_{strtol,strtoll,strtoul,strtoull}() now have the corresponding sscanf_{strtol,strtoll,strtoul,strtoull}() taking a field width into account. Perhaps this fix could be a starting- point to clean-up the integer parsers? Also, it seems to be a good idea to make a test suite for sscanf.
- Loading branch information
Showing
3 changed files
with
56 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters