From 837b04f7c1411d7e51ce051478701154c9a4a004 Mon Sep 17 00:00:00 2001 From: suvendhu Date: Mon, 10 Apr 2023 17:01:03 +0530 Subject: [PATCH] Fix probable crash in ppp stat --- libbbf_api/dmapi.h | 1 + libbbf_dm/dmtree/tr181/ppp.c | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/libbbf_api/dmapi.h b/libbbf_api/dmapi.h index c04265f3..89c97232 100644 --- a/libbbf_api/dmapi.h +++ b/libbbf_api/dmapi.h @@ -70,6 +70,7 @@ do { \ #define DM_STRSTR(STR, MATCH) ((STR != NULL && MATCH != NULL) ? strstr(STR, MATCH) : NULL) #define DM_STRCHR(STR, CHR) ((STR != NULL) ? strchr(STR, CHR) : NULL) #define DM_STRTOL(SRC) ((SRC != NULL) ? strtol(SRC, NULL, 10) : 0) +#define DM_STRTOUL(SRC) ((SRC != NULL) ? strtoul(SRC, NULL, 10) : 0) #define DM_STRCMP(S1, S2) ((S1 != NULL && S2 != NULL) ? strcmp(S1, S2) : -1) #define DM_STRNCMP(S1, S2, LEN) ((S1 != NULL && S2 != NULL && LEN > 0) ? strncmp(S1, S2, LEN) : -1) #define DM_STRCASECMP(S1, S2) ((S1 != NULL && S2 != NULL) ? strcasecmp(S1, S2) : -1) diff --git a/libbbf_dm/dmtree/tr181/ppp.c b/libbbf_dm/dmtree/tr181/ppp.c index be713a83..19e44c5b 100644 --- a/libbbf_dm/dmtree/tr181/ppp.c +++ b/libbbf_dm/dmtree/tr181/ppp.c @@ -848,6 +848,8 @@ static int ppp_read_sysfs(void *data, const char *name, char **value) { struct uci_section *ppp_s = ((struct ppp_args *)data)->iface_s; + *value = "0"; + if (ppp_s) { char *proto; @@ -856,8 +858,6 @@ static int ppp_read_sysfs(void *data, const char *name, char **value) char *l3_device = get_l3_device(section_name(ppp_s)); get_net_device_sysfs(l3_device, name, value); } - } else { - *value = "0"; } return 0; @@ -973,14 +973,14 @@ static int get_PPPInterfaceStats_UnicastPacketsReceived(char *refparam, struct d { /* Unicast Packets Received = (Total packets received - Unknown Protocol Packets Received)*/ - char *rx_other, *rx_pkts; + char *rx_other = NULL, *rx_pkts = NULL; unsigned long other_rcv = 0, total_rcv = 0; get_PPPInterfaceStats_UnknownProtoPacketsReceived(refparam, ctx, data, instance, &rx_other); get_ppp_eth_pack_received(refparam, ctx, data, instance, &rx_pkts); - other_rcv = strtoul(rx_other, NULL, 10); - total_rcv = strtoul(rx_pkts, NULL, 10); + other_rcv = DM_STRTOUL(rx_other); + total_rcv = DM_STRTOUL(rx_pkts); unsigned long ucast_rcv = total_rcv - other_rcv; dmasprintf(value, "%lu", ucast_rcv);