zdb: Handle theoretical buffer overflow when printing float
commitf954ea26a615cecc8573bb439482d9fd88019854
authorRichard Yao <richard.yao@alumni.stonybrook.edu>
Sun, 4 Dec 2022 20:41:24 +0000 (4 15:41 -0500)
committerBrian Behlendorf <behlendorf1@llnl.gov>
Thu, 8 Dec 2022 22:15:15 +0000 (8 14:15 -0800)
tree955eebba05f17d872eb9e32710bc0c8c20c196fc
parentd30db519af44b905fc52b8c8ba34f6378aa03470
zdb: Handle theoretical buffer overflow when printing float

CodeQL pointed out that for extreme floating point values, `sprintf()`
will overwrite a 32 character buffer. It cited 1e304 as an example,
which causes `sprintf()` to print 308 characters.

In practice, the numbers should never exceed 100, so this should not
happen. To silence the warning and also handle unexpected situations, we
change the code to use `snprintf()`.

This was missed during my audit of our use of `sprintf()`, since I did
not think to consider extreme floating point representations. It also
really should not happen, so this change is purely defensive
programming.

This was found by CodeQL's cpp/overrunning-write-with-float check.

Reviewed-by: Damian Szuberski <szuberskidamian@gmail.com>
Reviewed-by: Alexander Motin <mav@FreeBSD.org>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Richard Yao <richard.yao@alumni.stonybrook.edu>
Closes #14264
cmd/zdb/zdb.c