incorrect 'const' declaration with pointer-type typedefs in header files
The clang-tidy
program detected a couple of issues in public header files.
Here's an example message emitted when compiling/linting user-code:
/usr/local/libxml2-2.9.10/include/libxml2/libxml/list.h:122:33: warning: 'old' declared with a const-qualified typedef type; results in the type being 'struct _xmlList *const' instead of 'const struct _xmlList *' [misc-misplaced-const]
xmlListDup (const xmlListPtr old);
^
/usr/local/libxml2-2.9.10/include/libxml2/libxml/list.h:24:18: note: typedef declared here
typedef xmlList *xmlListPtr;
The problem is, that const xmlListPtr
is actually making the pointer variable (parameter) constant rather than the memory it points to! This is certainly not what the programmer had in mind when writing these lines of code. I think the fix is to declare the parameter 'old' as const xmlList*
instead of const xmlListPtr
. I can find places in the library code that are actually following my suggestion, e.g. in tree.h
, line 120:
XMLPUBFUN xmlChar* XMLCALL xmlBufContent (const xmlBuf* buf);
Here, the implementation of xmlBufContent
cannot modify the memory where buf is pointing to (compiler enforced). But the buf
variable can be re-used as needed, e.g. reassigned to another (const) xmlBuf memory block.
But two lines below, I see a problem again:
XMLPUBFUN size_t XMLCALL xmlBufUse (const xmlBufPtr buf);
In xmlBufUse
implementation you can modify the content of the buffer, but you cannot assign buf
another value, which is probably unintended.
The code is heavily using this idiom of const xml.*Ptr
:
# ~/libxml2> grep -r 'const xml.*Ptr' | wc -l
118
How are you going to fix this problem without breaking compatibility?