Quiet warnings for stricter compilers configurations (#84)
Fix warnings from -Wcast-qual and -Wstrict-prototypes.
A few other warning fixes and documentation improvements are added.
Also fixes Makefile so the before-release build script can invoke both gcc and clang. That script is here: laurencelundblade/qdv@536f083
This is an alternative to #79. It does not fix warnings for -Wbad-function-cast
because those fixes cause warnings with other compilers (MacOS clang). This doesn't enable GitHub automated build checks. The plan is to do that separately.
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index a96f74e..e5be98a 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -1,6 +1,6 @@
/*==============================================================================
Copyright (c) 2016-2018, The Linux Foundation.
- Copyright (c) 2018-2020, Laurence Lundblade.
+ Copyright (c) 2018-2021, Laurence Lundblade.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
@@ -41,6 +41,7 @@
when who what, where, why
-------- ---- ---------------------------------------------------
+ 3/6/2021 mcr/llundblade Fix warnings related to --Wcast-qual
01/28/2020 llundblade Refine integer signedness to quiet static analysis.
01/08/2020 llundblade Documentation corrections & improved code formatting.
11/08/2019 llundblade Re check pointer math and update comments
@@ -106,12 +107,12 @@
return 0;
}
- const uint8_t * const pEnd = (uint8_t *)UB.ptr + UB.len;
+ const uint8_t * const pEnd = (const uint8_t *)UB.ptr + UB.len;
for(const uint8_t *p = UB.ptr; p < pEnd; p++) {
if(*p != uValue) {
/* Byte didn't match */
/* Cast from signed to unsigned . Safe because the loop increments.*/
- return (size_t)(p - (uint8_t *)UB.ptr);
+ return (size_t)(p - (const uint8_t *)UB.ptr);
}
}
@@ -130,7 +131,7 @@
}
for(size_t uPos = 0; uPos <= BytesToSearch.len - BytesToFind.len; uPos++) {
- if(!UsefulBuf_Compare((UsefulBufC){((uint8_t *)BytesToSearch.ptr) + uPos, BytesToFind.len}, BytesToFind)) {
+ if(!UsefulBuf_Compare((UsefulBufC){((const uint8_t *)BytesToSearch.ptr) + uPos, BytesToFind.len}, BytesToFind)) {
return uPos;
}
}
@@ -358,7 +359,7 @@
}
// This is going to succeed
- const void * const result = ((uint8_t *)pMe->UB.ptr) + pMe->cursor;
+ const void * const result = ((const uint8_t *)pMe->UB.ptr) + pMe->cursor;
// Will not overflow because of check using UsefulInputBuf_BytesAvailable()
pMe->cursor += uAmount;
return result;
diff --git a/src/qcbor_decode.c b/src/qcbor_decode.c
index b46908d..6b63eaa 100644
--- a/src/qcbor_decode.c
+++ b/src/qcbor_decode.c
@@ -46,12 +46,6 @@
-/*
- * This casts away the const-ness of a pointer, usually so it can be
- * freed or realloced.
- */
-#define UNCONST_POINTER(ptr) ((void *)(ptr))
-
#define SIZEOF_C_ARRAY(array,type) (sizeof(array)/sizeof(type))
@@ -504,19 +498,30 @@
===========================================================================*/
static inline void
-StringAllocator_Free(const QCBORInternalAllocator *pMe, void *pMem)
+StringAllocator_Free(const QCBORInternalAllocator *pMe, const void *pMem)
{
- (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, 0);
+ /* These pragmas allow the "-Wcast-qual" warnings flag to be set for
+ * gcc and clang. This is the one place where the const needs to be
+ * cast away so const can be use in the rest of the code.
+ */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
+ (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)pMem, 0);
+#pragma GCC diagnostic pop
}
// StringAllocator_Reallocate called with pMem NULL is
// equal to StringAllocator_Allocate()
static inline UsefulBuf
StringAllocator_Reallocate(const QCBORInternalAllocator *pMe,
- void *pMem,
+ const void *pMem,
size_t uSize)
{
- return (pMe->pfAllocator)(pMe->pAllocateCxt, pMem, uSize);
+ /* See comment in StringAllocator_Free() */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
+ return (pMe->pfAllocator)(pMe->pAllocateCxt, (void *)pMem, uSize);
+#pragma GCC diagnostic pop
}
static inline UsefulBuf
@@ -528,9 +533,13 @@
static inline void
StringAllocator_Destruct(const QCBORInternalAllocator *pMe)
{
+ /* See comment in StringAllocator_Free() */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wcast-qual"
if(pMe->pfAllocator) {
(pMe->pfAllocator)(pMe->pAllocateCxt, NULL, 0);
}
+#pragma GCC diagnostic pop
}
#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
@@ -1290,7 +1299,7 @@
* not NULL and a reallocation happens.
*/
UsefulBuf NewMem = StringAllocator_Reallocate(pAllocator,
- UNCONST_POINTER(FullString.ptr),
+ FullString.ptr,
FullString.len + StringChunkItem.val.string.len);
if(UsefulBuf_IsNULL(NewMem)) {
@@ -1304,7 +1313,7 @@
if(uReturn != QCBOR_SUCCESS && !UsefulBuf_IsNULLC(FullString)) {
/* Getting the item failed, clean up the allocated memory */
- StringAllocator_Free(pAllocator, UNCONST_POINTER(FullString.ptr));
+ StringAllocator_Free(pAllocator, FullString.ptr);
}
#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
uReturn = QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED;
@@ -1459,7 +1468,7 @@
memmove(&auItemsTags[1], auItemsTags, sizeof(auItemsTags) - sizeof(auItemsTags[0]));
/* Map the tag */
- uint16_t uMappedTagNumer;
+ uint16_t uMappedTagNumer = 0;
uReturn = MapTagNumber(pMe, pDecodedItem->val.uTagV, &uMappedTagNumer);
/* Continue even on error so as to consume all tags wrapping
* this data item so decoding can go on. If MapTagNumber()