CWE Rule 20
Description
Rule Description
The product receives input or data, but it does not validate or incorrectly validates that the input has the properties that are required to process the data safely and correctly.
Polyspace Implementation
The rule checker checks for Unsafe conversion from string to numerical value.
Examples
Unsafe conversion from string to numerical value
This issue occurs when you perform conversions from strings to integer or floating-point values and your conversion method does not include robust error handling.
Converting a string to numerical value can cause data loss or misinterpretation. Without validation of the conversion or error handling, your program continues with invalid values.
Add additional checks to validate the numerical value.
Use a more robust string-to-numeric conversion function such as
strtol
,strtoll
,strtoul
, orstrtoull
.
#include <stdio.h> #include <stdlib.h> #include <string.h> static int demo_check_string_not_empty(char *s) { if (s != NULL) return strlen(s) > 0; /* check string null-terminated and not empty */ else return 0; } int unsafestrtonumeric(char* argv1) { int s = 0; if (demo_check_string_not_empty(argv1)) { s = atoi(argv1); //Noncompliant } return s; }
In this example, argv1
is converted to an
integer with atoi
. atoi
does
not provide errors for an invalid integer string. The conversion can
fail unexpectedly.
strtol
insteadOne possible correction is to use strtol
to
validate the input string and the converted integer.
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <limits.h> #include <errno.h> static int demo_check_string_not_empty(char *s) { if (s != NULL) return strlen(s) > 0; /* check string null-terminated and not empty */ else return 0; } int unsafestrtonumeric(char *argv1) { char *c_str = argv1; char *end; long sl; if (demo_check_string_not_empty(c_str)) { errno = 0; /* set errno for error check */ sl = strtol(c_str, &end, 10); if (end == c_str) { (void)fprintf(stderr, "%s: not a decimal number\n", c_str); } else if ('\0' != *end) { (void)fprintf(stderr, "%s: extra characters: %s\n", c_str, end); } else if ((LONG_MIN == sl || LONG_MAX == sl) && ERANGE == errno) { (void)fprintf(stderr, "%s out of range of type long\n", c_str); } else if (sl > INT_MAX) { (void)fprintf(stderr, "%ld greater than INT_MAX\n", sl); } else if (sl < INT_MIN) { (void)fprintf(stderr, "%ld less than INT_MIN\n", sl); } else { return (int)sl; } } return 0; }
Check Information
Category: Others |
Version History
Introduced in R2024a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)