Fix static analyzer warnings for adding empty data/strings (#170)
CBOR allows empty text and byte strings and QBOR supports them with AddText() and AddBytes() with a size of zero.
This results in a call to memmove with a NULL pointer. It was always with a zero length. Implementations of memmove tolerate this, but it is not recommended and static analyzers complain.
This PR fixes this. It also adds tests for this condition. It also documents that adding strings of zero length is supported in UsefulBuf.
This addresses #164
* Correct support for adding empty text/byte strings
* Update change log
Co-authored-by: Laurence Lundblade <lgl@securitytheory.com>
diff --git a/src/UsefulBuf.c b/src/UsefulBuf.c
index 1a6b385..b36e5d0 100644
--- a/src/UsefulBuf.c
+++ b/src/UsefulBuf.c
@@ -41,7 +41,8 @@
when who what, where, why
-------- ---- ---------------------------------------------------
- 4/11/2022 llundblade Add GetOutPlace and Advance to UsefulOutBuf
+ 19/12/2022 llundblade Don't pass NULL to memmove when adding empty data.
+ 4/11/2022 llundblade Add GetOutPlace and Advance to UsefulOutBuf
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.
@@ -266,7 +267,9 @@
/* 4. Put the new data in */
uint8_t *pInsertionPoint = pSourceOfMove;
// To know memmove won't go off end of destination, see PtrMath #5
- memmove(pInsertionPoint, NewData.ptr, NewData.len);
+ if(NewData.ptr != NULL) {
+ memmove(pInsertionPoint, NewData.ptr, NewData.len);
+ }
}
pMe->data_len += NewData.len;