net/tap: avoid memcpy with NULL arg

Message ID 20240814023426.124491-1-stephen@networkplumber.org (mailing list archive)
State Accepted
Delegated to: Ferruh Yigit
Headers
Series net/tap: avoid memcpy with NULL arg |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/loongarch-compilation success Compilation OK
ci/Intel-compilation success Compilation OK
ci/loongarch-unit-testing success Unit Testing PASS
ci/intel-Testing success Testing PASS
ci/github-robot: build success github build: passed
ci/intel-Functional success Functional PASS
ci/iol-mellanox-Performance success Performance Testing PASS
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-marvell-Functional success Functional Testing PASS
ci/iol-unit-amd64-testing success Testing PASS
ci/iol-broadcom-Performance success Performance Testing PASS
ci/iol-compile-amd64-testing success Testing PASS
ci/iol-sample-apps-testing success Testing PASS
ci/iol-unit-arm64-testing success Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/iol-intel-Functional success Functional Testing PASS
ci/iol-compile-arm64-testing success Testing PASS

Commit Message

Stephen Hemminger Aug. 14, 2024, 2:34 a.m. UTC
Calling memcpy with a null pointer even if zero length is
undefined, so check if data_length is zero.
Problem reported by Gcc analyzer.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
 drivers/net/tap/tap_netlink.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Ferruh Yigit Aug. 19, 2024, 11:03 a.m. UTC | #1
On 8/14/2024 3:34 AM, Stephen Hemminger wrote:
> Calling memcpy with a null pointer even if zero length is
> undefined, so check if data_length is zero.
> Problem reported by Gcc analyzer.
> 
> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
> 

Acked-by: Ferruh Yigit <ferruh.yigit@amd.com>
Applied to dpdk-next-net/main, thanks.
  
Ferruh Yigit Aug. 19, 2024, 11:04 a.m. UTC | #2
On 8/19/2024 12:03 PM, Ferruh Yigit wrote:
> On 8/14/2024 3:34 AM, Stephen Hemminger wrote:
>> Calling memcpy with a null pointer even if zero length is
>> undefined, so check if data_length is zero.
>> Problem reported by Gcc analyzer.
>>

Btw, how do you run the GCC analyzer?
Is this something can we add to our CI checks?


>>
>> Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
>>
> 
> Acked-by: Ferruh Yigit <ferruh.yigit@amd.com>
> Applied to dpdk-next-net/main, thanks.
>
  
Stephen Hemminger Aug. 19, 2024, 3:10 p.m. UTC | #3
On Mon, 19 Aug 2024 12:04:57 +0100
Ferruh Yigit <ferruh.yigit@amd.com> wrote:

> On 8/19/2024 12:03 PM, Ferruh Yigit wrote:
> > On 8/14/2024 3:34 AM, Stephen Hemminger wrote:  
> >> Calling memcpy with a null pointer even if zero length is
> >> undefined, so check if data_length is zero.
> >> Problem reported by Gcc analyzer.
> >>  
> 
> Btw, how do you run the GCC analyzer?
> Is this something can we add to our CI checks?

I just added it to cflags in meson build.
Tap only had this warning, but mlx5 had lots of warnings.
  

Patch

diff --git a/drivers/net/tap/tap_netlink.c b/drivers/net/tap/tap_netlink.c
index c7db78563a..8a57c9242c 100644
--- a/drivers/net/tap/tap_netlink.c
+++ b/drivers/net/tap/tap_netlink.c
@@ -302,7 +302,8 @@  tap_nlattr_add(struct nlmsghdr *nh, unsigned short type,
 	rta = (struct rtattr *)NLMSG_TAIL(nh);
 	rta->rta_len = RTA_LENGTH(data_len);
 	rta->rta_type = type;
-	memcpy(RTA_DATA(rta), data, data_len);
+	if (data_len > 0)
+		memcpy(RTA_DATA(rta), data, data_len);
 	nh->nlmsg_len = NLMSG_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
 }