Skip to content

bugs caused by misuse of from_str_radix #462

@Rudxain

Description

@Rudxain

There's at least 1 instance of this pitfall (rust-lang/rust-clippy#16213):

match i64::from_str_radix(hex, 16) {

The previous lines seem to imply it's safe:

// Handle sign
let (is_negative, num_str) = if let Some(rest) = arg_str.strip_prefix('-') {
(true, rest)
} else if let Some(rest) = arg_str.strip_prefix('+') {
(false, rest)
} else {
(false, arg_str)
};
// Parse the number
let (value, fully_consumed) = if let Some(hex) = num_str
.strip_prefix("0x")
.or_else(|| num_str.strip_prefix("0X"))
{
// Hexadecimal

But it would still parse stuff like "+0x+55" as 0x55.

I suspect there's many other places where from_str_radix is used wrong:

Output of git grep -Fwl from_str_radix:

calc/bc_util/number.rs
cc/parse/parser.rs
cc/token/lexer.rs
cc/token/preprocess.rs
dev/lex/lexfile.rs
dev/yacc/lexer.rs
display/printf.rs
file/find.rs
file/magic.rs
file/od.rs
i18n/iconv.rs
man/man_util/formatter.rs
pax/formats/cpio.rs
pax/formats/pax.rs
pax/formats/ustar.rs
pax/main.rs
pax/modes/append.rs
pax/multivolume.rs
plib/src/modestr.rs
process/batch.rs
process/fuser.rs
sh/builtin/umask.rs
sh/wordexp/arithmetic.rs
sys/ipcrm.rs
text/tr.rs
xform/uudecode.rs

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions