util: Add data ptr check to avoid crash
When the eth device is down, the data ptr is NULL,
so we should add a check to avoid crash.
Tested:
1. Let ncsi device down
`~# ip link set eth0 down`
2. Send a oem command to ncsi device, no crash.
```
ncsi-netlink -x 3 -p 0 -c 0 --oem-payload 00
Send OEM Command, CHANNEL : 0, PACKAGE : 0, IFINDEX: 3
Payload : 00
Response: No data
Failed to receive the message , RC : -1
```
Signed-off-by: Jian Zhang <zhangjian.3032@bytedance.com>
Change-Id: I1e1119f7891732a87b56a345dad8ec3549868bf7
diff --git a/src/ncsi_util.cpp b/src/ncsi_util.cpp
index 06eb254..aa31a18 100644
--- a/src/ncsi_util.cpp
+++ b/src/ncsi_util.cpp
@@ -227,6 +227,12 @@
return ret;
}
+ if (tb[NCSI_ATTR_DATA] == nullptr)
+ {
+ std::cerr << "Response: No data" << std::endl;
+ return -1;
+ }
+
auto data_len = nla_len(tb[NCSI_ATTR_DATA]) - sizeof(NCSIPacketHeader);
unsigned char* data =
(unsigned char*)nla_data(tb[NCSI_ATTR_DATA]) + sizeof(NCSIPacketHeader);