rspec/rules/S2992/cfamily/rule.adoc

39 lines
960 B
Plaintext
Raw Normal View History

``++typedef++``s are a convenient way of encapsulating complex types in simpler names. However, ``++typedef++``ing a pointer type is problematic because when ``++const++`` is applied to the defined type, it makes the pointer constant, not the pointed-to value. Even when that is what's intended, maintainers may misunderstand.
== Noncompliant Code Example
2022-02-04 17:28:24 +01:00
[source,cpp]
----
struct book {
int isbn;
int publishYear;
};
typedef book;
typedef *pBook; // Noncompliant
void mangleBook(const pBook pb) {
pb->isbn = 4; // this is legal; its the pointer, not the book that's const
pb->publishYear=1900;
}
----
== Exceptions
Function pointers are ignored by this rule.
== See
* https://www.securecoding.cert.org/confluence/x/14At[CERT, DCL05-C.] - Use typedefs of non-pointer types only
ifdef::env-github,rspecator-view[]
'''
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]