...
Wiki Markup |
---|
This non-compliant code example is based on the managed string library developed by CERT \[[Burch 06|AA. C References#Seacord 06]\]. In this non-compliant example, the managed string type is defined in the include file {{"string_m.h"}} as follows: |
Code Block | ||
---|---|---|
| ||
struct string_mx { size_t size; size_t maxsize; unsigned char strtype; char *cstr; }; typedef struct string_mx *string_m; |
The implementation of the string_m
type is fully visible to the user of the data type after including the "string_m.h"
file. Programmers are consequently more likely to directly manipulate the fields within the structure, violating the software engineering principles of information hiding and data encapsulation and increasing the probability of developing incorrect or non-portable code.
...
This compliant solution reimplements the string_m
type as a private type, hiding the implementation of the data type from the user of the managed string library. To accomplish this, the developer of the private data type creates two include files: an external "string_m.h"
include file that is included by the user of the data type and an internal file that is only included in files that implement the managed string abstract data type.
In the external "string_m.h"
, the string_m
type is declared as a pointer to a struct string_mx
, which in turn is declared as an incomplete type.
...
In the internal include file, struct string_mx
is fully defined , but not visible to a user of the data abstraction.
...
Modules that implement the abstract data type include both the external and internal definitions, while users of the data abstraction include only the external "string_m.h"
file. This allows the implementation of the string_m
to remain private.
...
The use of abstract data types, while not essential to secure programming, can significantly reduce the number of defects and vulnerabilities introduced in code, particularly during on-going ongoing maintenance.
Recommendation | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
DCL12-A | 1 (low) | 1 (unlikely) | 1 (high) | P1 | L3 |
...