rspec/rules/S1966/rule.adoc
2021-02-02 16:54:43 +01:00

46 lines
1.1 KiB
Plaintext

An alphanumeric value should not be moved to a numeric field. Because alphanumeric values are stored differently than numeric values, simply moving the bits from one field to the other will yield strange results at best, and crashes at worst.
Instead, ``++NUMVAL++`` should be used to explicitly convert the alphanumeric value to a numeric one.
== Noncompliant Code Example
----
01 MY-STR PIC X(3) VALUE SPACES.
01 MY-NUM PIC 9(3) VALUE ZEROES.
*> ...
MOVE '1' TO MY-STR
MOVE MY-STR TO MY-NUM *> Noncompliant
----
== Compliant Solution
----
01 MY-STR PIC X(3) VALUE SPACES.
01 MY-NUM PIC 9(3) VALUE ZEROES.
*> ...
MOVE '1' TO MY-STR
COMPUTE MY-NUM = FUNCTION NUMVAL(MY-STR)
----
or
----
01 MY-STR PIC X(3) VALUE SPACES.
01 MY-STR-RED REDEFINES MY-STR PIC 9(3).
01 MY-NUM PIC 9(3) VALUE ZEROES.
*> ...
IF MY-STR NUMERIC
MOVE MY-STR-RED TO MY-NUM
END-IF
----
== See
* http://cwe.mitre.org/data/definitions/704.html[MITRE, CWE-704] - Incorrect Type Conversion or Cast