Skip to contentSkip to conversation

qed: don't pass NULL to memcpy

PATCH Applied Pavel Borzenkov 4 participants 6 messages Oct 18, 2011 – Oct 21, 2011 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 qed: don't pass NULL to memcpy

Spotted by Clang Analyzer

---
 block/qed.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
block/qed.c +4 -2
index c3e45af..e6720db 100644
@@ -1424,8 +1424,10 @@ static int bdrv_qed_change_backing_file(BlockDriverState *bs,
     memcpy(buffer, &le_header, sizeof(le_header));
     buffer_len = sizeof(le_header);
 
-    memcpy(buffer + buffer_len, backing_file, backing_file_len);
-    buffer_len += backing_file_len;
+    if (backing_file) {
+        memcpy(buffer + buffer_len, backing_file, backing_file_len);
+        buffer_len += backing_file_len;
+    }
 
     /* Write new header */
     ret = bdrv_pwrite_sync(bs->file, 0, buffer, buffer_len);
Signature · 1 line
1.7.0.4
#3 lore raw
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Pavel Borzenkov

Spotted by Clang Analyzer

---
  block/qed.c |    6 ++++--
  1 files changed, 4 insertions(+), 2 deletions(-)
Quoted 5 lines
Quoted 2 lines

Thanks, applied to the trivial patches tree:
http://repo.or.cz/w/qemu/stefanha.git/shortlog/refs/heads/trivial-patches

I think there are other places in the tree where we assume that "memcpy(dest, NULL, 0);" works.

Looks like a fair assumption to me.

#4 lore raw
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Pavel Borzenkov

Spotted by Clang Analyzer

---
   block/qed.c |    6 ++++--
   1 files changed, 4 insertions(+), 2 deletions(-)
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Thanks, applied to the trivial patches tree:
http://repo.or.cz/w/qemu/stefanha.git/shortlog/refs/heads/trivial-patches

I think there are other places in the tree where we assume that "memcpy(dest, NULL, 0);" works.

Looks like a fair assumption to me.

Such calls work, but this is UB. Calling string functions with NULL pointers and zero length is a violation of the C99 standard:

7.21.1 String function conventions
Where an argument declared as size_t n specifies the length of the array for a function, n can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4.

Signature · 1 line
Pavel
#5 lore raw
In reply to Markus Armbruster
Quoted 11 lines · Markus Armbruster
Quoted 9 lines · Paolo Bonzini
Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Pavel Borzenkov

Spotted by Clang Analyzer

---
  block/qed.c |    6 ++++--

  1 files changed, 4 insertions(+), 2 deletions(-)
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Thanks, applied to the trivial patches tree:
http://repo.or.cz/w/qemu/stefanha.git/shortlog/refs/heads/trivial-patches

I think there are other places in the tree where we assume that "memcpy(dest, NULL, 0);" works.

Looks like a fair assumption to me.

Such calls work, but this is UB. Calling string functions with NULL pointers and zero length is a violation of the C99 standard:

7.21.1 String function conventions
Where an argument declared as size_t n specifies the length of the array for a function, n can have the value zero on a call to that function. Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values, as described in 7.1.4.

Signature · 1 line
Pavel