Skip to contentSkip to conversation

lib: circbuf: avoid possible null pointer dereference

PATCH New Heinrich Schuchardt 2 participants 2 messages May 3, 2017 – May 7, 2017 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: circbuf: avoid possible null pointer dereference

We should not first dereference p and afterwards assert that is was not NULL. Instead do the assert first.

The problem was indicated by cppcheck.

---
 lib/circbuf.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/circbuf.c +6 -2
index 9848da3b7b..6ed0516430 100644
@@ -41,11 +41,13 @@ int buf_free (circbuf_t * buf)
 int buf_pop (circbuf_t * buf, char *dest, unsigned int len)
 {
 	unsigned int i;
-	char *p = buf->top;
+	char *p;
 
 	assert (buf != NULL);
 	assert (dest != NULL);
 
+	p = buf->top;
+
 	/* Cap to number of bytes in buffer */
 	if (len > buf->size)
 		len = buf->size;
@@ -69,11 +71,13 @@ int buf_push (circbuf_t * buf, const char *src, unsigned int len)
 {
 	/* NOTE:  this function allows push to overwrite old data. */
 	unsigned int i;
-	char *p = buf->tail;
+	char *p;
 
 	assert (buf != NULL);
 	assert (src != NULL);
 
+	p = buf->tail;
+
 	for (i = 0; i < len; i++) {
 		*p++ = src[i];
 		if (p == buf->end) {
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/1 lib: circbuf: avoid possible null pointer dereference

Quoted 6 lines · xypron.glpk at gmx.de

We should not first dereference p and afterwards assert that is was not NULL. Instead do the assert first.

The problem was indicated by cppcheck.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20170506/9788f024/attachment.sig>