[metadata]
id = "ERR34-C"
type = "rule"
category = "ERR"
number = 34
title = "Detect errors when converting a string to a number"
description = """
The process of parsing an integer or floating-point number from a string can
produce many errors. The string might not contain a number. It might contain a
number of the correct type that is out of range (such as an integer that is
larger thanINT_MAX). The string may also contain extra information after the
number, which may or may not be useful after the conversion. These error
conditions must be detected and addressed when a string-to-number conversion is
performed using a C Standard Library function.
Thestrtol(),strtoll(),strtoimax(),strtoul(), strtoull(),strtoumax(),
strtof(),strtod(), andstrtold()functions convert the initial portion of a null-
terminated byte string to along int,long long int,intmax_t,unsigned long
int,unsigned long long int, uintmax_t, float, double, andlong
doublerepresentation, respectively. Use one of the C Standard
Librarystrto*()functions to parse an integer or floating-point number from a
string. These functions provide more robust error handling than alternative
solutions. Also, use thestrtol()function to convert to a smaller signed integer
type such assigned int,signed short, andsigned char, testing the result against
the range limits for that type. Likewise, use thestrtoul()function to convert to
a smaller unsigned integer type such asunsigned int,unsigned short, andunsigned
char, and test the result against the range limits for that type. These range
tests do nothing if the smaller type happens to have the same size and
representation for a particular implementation.
"""
severity = "Medium"
likelihood = "Unlikely"
priority = "P6"
level = "L2"
cert_version = "2016 Edition (Wiki)"
last_modified = "Oct 29, 2025"
[rules.cert_c.ERR34-C]
enabled = true
[references]
wiki = "https://wiki.sei.cmu.edu/confluence/display/c/ERR34-C.+Detect+errors+when+converting+a+string+to+a+number"
cwe = ["CWE-676", "CWE-758", "CWE-20", "CWE-391"]