Skip to content

Commit 027760b

Browse files
jukkarkartben
authored andcommitted
net: if: Replace asserts by proper runtime checks
The asserts were not proper here, replace those by runtime checks as the functions can be called from applications and asserts are not meant for error checking. Signed-off-by: Jukka Rissanen <[email protected]>
1 parent 692310d commit 027760b

File tree

2 files changed

+136
-74
lines changed

2 files changed

+136
-74
lines changed

include/zephyr/net/net_if.h

Lines changed: 88 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -786,8 +786,9 @@ static inline void net_if_tx_unlock(struct net_if *iface)
786786
static inline void net_if_flag_set(struct net_if *iface,
787787
enum net_if_flag value)
788788
{
789-
NET_ASSERT(iface);
790-
NET_ASSERT(iface->if_dev);
789+
if (iface == NULL || iface->if_dev == NULL) {
790+
return;
791+
}
791792

792793
atomic_set_bit(iface->if_dev->flags, value);
793794
}
@@ -803,8 +804,9 @@ static inline void net_if_flag_set(struct net_if *iface,
803804
static inline bool net_if_flag_test_and_set(struct net_if *iface,
804805
enum net_if_flag value)
805806
{
806-
NET_ASSERT(iface);
807-
NET_ASSERT(iface->if_dev);
807+
if (iface == NULL || iface->if_dev == NULL) {
808+
return false;
809+
}
808810

809811
return atomic_test_and_set_bit(iface->if_dev->flags, value);
810812
}
@@ -818,8 +820,9 @@ static inline bool net_if_flag_test_and_set(struct net_if *iface,
818820
static inline void net_if_flag_clear(struct net_if *iface,
819821
enum net_if_flag value)
820822
{
821-
NET_ASSERT(iface);
822-
NET_ASSERT(iface->if_dev);
823+
if (iface == NULL || iface->if_dev == NULL) {
824+
return;
825+
}
823826

824827
atomic_clear_bit(iface->if_dev->flags, value);
825828
}
@@ -835,8 +838,9 @@ static inline void net_if_flag_clear(struct net_if *iface,
835838
static inline bool net_if_flag_test_and_clear(struct net_if *iface,
836839
enum net_if_flag value)
837840
{
838-
NET_ASSERT(iface);
839-
NET_ASSERT(iface->if_dev);
841+
if (iface == NULL || iface->if_dev == NULL) {
842+
return false;
843+
}
840844

841845
return atomic_test_and_clear_bit(iface->if_dev->flags, value);
842846
}
@@ -852,10 +856,7 @@ static inline bool net_if_flag_test_and_clear(struct net_if *iface,
852856
static inline bool net_if_flag_is_set(struct net_if *iface,
853857
enum net_if_flag value)
854858
{
855-
NET_ASSERT(iface);
856-
NET_ASSERT(iface->if_dev);
857-
858-
if (iface == NULL) {
859+
if (iface == NULL || iface->if_dev == NULL) {
859860
return false;
860861
}
861862

@@ -873,8 +874,9 @@ static inline bool net_if_flag_is_set(struct net_if *iface,
873874
static inline enum net_if_oper_state net_if_oper_state_set(
874875
struct net_if *iface, enum net_if_oper_state oper_state)
875876
{
876-
NET_ASSERT(iface);
877-
NET_ASSERT(iface->if_dev);
877+
if (iface == NULL || iface->if_dev == NULL) {
878+
return NET_IF_OPER_UNKNOWN;
879+
}
878880

879881
BUILD_ASSERT((enum net_if_oper_state)(-1) > 0 && NET_IF_OPER_UNKNOWN == 0);
880882
if (oper_state <= NET_IF_OPER_UP) {
@@ -893,8 +895,9 @@ static inline enum net_if_oper_state net_if_oper_state_set(
893895
*/
894896
static inline enum net_if_oper_state net_if_oper_state(struct net_if *iface)
895897
{
896-
NET_ASSERT(iface);
897-
NET_ASSERT(iface->if_dev);
898+
if (iface == NULL || iface->if_dev == NULL) {
899+
return NET_IF_OPER_UNKNOWN;
900+
}
898901

899902
return iface->if_dev->oper_state;
900903
}
@@ -918,7 +921,7 @@ enum net_verdict net_if_send_data(struct net_if *iface, struct net_pkt *pkt);
918921
*/
919922
static inline const struct net_l2 *net_if_l2(struct net_if *iface)
920923
{
921-
if (!iface || !iface->if_dev) {
924+
if (iface == NULL || iface->if_dev == NULL) {
922925
return NULL;
923926
}
924927

@@ -944,8 +947,9 @@ enum net_verdict net_if_recv_data(struct net_if *iface, struct net_pkt *pkt);
944947
*/
945948
static inline void *net_if_l2_data(struct net_if *iface)
946949
{
947-
NET_ASSERT(iface);
948-
NET_ASSERT(iface->if_dev);
950+
if (iface == NULL || iface->if_dev == NULL) {
951+
return NULL;
952+
}
949953

950954
return iface->if_dev->l2_data;
951955
}
@@ -959,8 +963,9 @@ static inline void *net_if_l2_data(struct net_if *iface)
959963
*/
960964
static inline const struct device *net_if_get_device(struct net_if *iface)
961965
{
962-
NET_ASSERT(iface);
963-
NET_ASSERT(iface->if_dev);
966+
if (iface == NULL || iface->if_dev == NULL) {
967+
return NULL;
968+
}
964969

965970
return iface->if_dev->dev;
966971
}
@@ -1011,8 +1016,9 @@ bool net_if_is_offloaded(struct net_if *iface);
10111016
static inline struct net_offload *net_if_offload(struct net_if *iface)
10121017
{
10131018
#if defined(CONFIG_NET_OFFLOAD)
1014-
NET_ASSERT(iface);
1015-
NET_ASSERT(iface->if_dev);
1019+
if (iface == NULL || iface->if_dev == NULL) {
1020+
return NULL;
1021+
}
10161022

10171023
return iface->if_dev->offload;
10181024
#else
@@ -1032,8 +1038,9 @@ static inline struct net_offload *net_if_offload(struct net_if *iface)
10321038
static inline bool net_if_is_socket_offloaded(struct net_if *iface)
10331039
{
10341040
#if defined(CONFIG_NET_SOCKETS_OFFLOAD)
1035-
NET_ASSERT(iface);
1036-
NET_ASSERT(iface->if_dev);
1041+
if (iface == NULL || iface->if_dev == NULL) {
1042+
return false;
1043+
}
10371044

10381045
return (iface->if_dev->socket_offload != NULL);
10391046
#else
@@ -1053,8 +1060,9 @@ static inline void net_if_socket_offload_set(
10531060
struct net_if *iface, net_socket_create_t socket_offload)
10541061
{
10551062
#if defined(CONFIG_NET_SOCKETS_OFFLOAD)
1056-
NET_ASSERT(iface);
1057-
NET_ASSERT(iface->if_dev);
1063+
if (iface == NULL || iface->if_dev == NULL) {
1064+
return;
1065+
}
10581066

10591067
iface->if_dev->socket_offload = socket_offload;
10601068
#else
@@ -1073,8 +1081,9 @@ static inline void net_if_socket_offload_set(
10731081
static inline net_socket_create_t net_if_socket_offload(struct net_if *iface)
10741082
{
10751083
#if defined(CONFIG_NET_SOCKETS_OFFLOAD)
1076-
NET_ASSERT(iface);
1077-
NET_ASSERT(iface->if_dev);
1084+
if (iface == NULL || iface->if_dev == NULL) {
1085+
return NULL;
1086+
}
10781087

10791088
return iface->if_dev->socket_offload;
10801089
#else
@@ -1093,8 +1102,9 @@ static inline net_socket_create_t net_if_socket_offload(struct net_if *iface)
10931102
*/
10941103
static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface)
10951104
{
1096-
NET_ASSERT(iface);
1097-
NET_ASSERT(iface->if_dev);
1105+
if (iface == NULL || iface->if_dev == NULL) {
1106+
return NULL;
1107+
}
10981108

10991109
return &iface->if_dev->link_addr;
11001110
}
@@ -1108,7 +1118,9 @@ static inline struct net_linkaddr *net_if_get_link_addr(struct net_if *iface)
11081118
*/
11091119
static inline struct net_if_config *net_if_get_config(struct net_if *iface)
11101120
{
1111-
NET_ASSERT(iface);
1121+
if (iface == NULL) {
1122+
return NULL;
1123+
}
11121124

11131125
return &iface->config;
11141126
}
@@ -1252,12 +1264,10 @@ static inline int net_if_set_link_addr(struct net_if *iface,
12521264
*/
12531265
static inline uint16_t net_if_get_mtu(struct net_if *iface)
12541266
{
1255-
if (iface == NULL) {
1267+
if (iface == NULL || iface->if_dev == NULL) {
12561268
return 0U;
12571269
}
12581270

1259-
NET_ASSERT(iface->if_dev);
1260-
12611271
return iface->if_dev->mtu;
12621272
}
12631273

@@ -1270,12 +1280,10 @@ static inline uint16_t net_if_get_mtu(struct net_if *iface)
12701280
static inline void net_if_set_mtu(struct net_if *iface,
12711281
uint16_t mtu)
12721282
{
1273-
if (iface == NULL) {
1283+
if (iface == NULL || iface->if_dev == NULL) {
12741284
return;
12751285
}
12761286

1277-
NET_ASSERT(iface->if_dev);
1278-
12791287
iface->if_dev->mtu = mtu;
12801288
}
12811289

@@ -1288,7 +1296,9 @@ static inline void net_if_set_mtu(struct net_if *iface,
12881296
static inline void net_if_addr_set_lf(struct net_if_addr *ifaddr,
12891297
bool is_infinite)
12901298
{
1291-
NET_ASSERT(ifaddr);
1299+
if (ifaddr == NULL) {
1300+
return;
1301+
}
12921302

12931303
ifaddr->is_infinite = is_infinite;
12941304
}
@@ -1320,7 +1330,9 @@ struct net_if *net_if_lookup_by_dev(const struct device *dev);
13201330
*/
13211331
static inline struct net_if_config *net_if_config_get(struct net_if *iface)
13221332
{
1323-
NET_ASSERT(iface);
1333+
if (iface == NULL) {
1334+
return NULL;
1335+
}
13241336

13251337
return &iface->config;
13261338
}
@@ -1651,7 +1663,9 @@ void net_if_ipv6_maddr_join(struct net_if *iface,
16511663
*/
16521664
static inline bool net_if_ipv6_maddr_is_joined(struct net_if_mcast_addr *addr)
16531665
{
1654-
NET_ASSERT(addr);
1666+
if (addr == NULL) {
1667+
return false;
1668+
}
16551669

16561670
return addr->is_joined;
16571671
}
@@ -1765,7 +1779,9 @@ bool net_if_ipv6_addr_onlink(struct net_if **iface, struct in6_addr *addr);
17651779
#if defined(CONFIG_NET_NATIVE_IPV6)
17661780
static inline struct in6_addr *net_if_router_ipv6(struct net_if_router *router)
17671781
{
1768-
NET_ASSERT(router);
1782+
if (router == NULL) {
1783+
return NULL;
1784+
}
17691785

17701786
return &router->address.in6_addr;
17711787
}
@@ -1932,7 +1948,9 @@ static inline void net_if_ipv6_set_base_reachable_time(struct net_if *iface,
19321948
uint32_t reachable_time)
19331949
{
19341950
#if defined(CONFIG_NET_NATIVE_IPV6)
1935-
NET_ASSERT(iface);
1951+
if (iface == NULL) {
1952+
return;
1953+
}
19361954

19371955
if (!iface->config.ip.ipv6) {
19381956
return;
@@ -1956,7 +1974,9 @@ static inline void net_if_ipv6_set_base_reachable_time(struct net_if *iface,
19561974
static inline uint32_t net_if_ipv6_get_reachable_time(struct net_if *iface)
19571975
{
19581976
#if defined(CONFIG_NET_NATIVE_IPV6)
1959-
NET_ASSERT(iface);
1977+
if (iface == NULL) {
1978+
return 0;
1979+
}
19601980

19611981
if (!iface->config.ip.ipv6) {
19621982
return 0;
@@ -2007,7 +2027,9 @@ static inline void net_if_ipv6_set_retrans_timer(struct net_if *iface,
20072027
uint32_t retrans_timer)
20082028
{
20092029
#if defined(CONFIG_NET_NATIVE_IPV6)
2010-
NET_ASSERT(iface);
2030+
if (iface == NULL) {
2031+
return;
2032+
}
20112033

20122034
if (!iface->config.ip.ipv6) {
20132035
return;
@@ -2030,7 +2052,9 @@ static inline void net_if_ipv6_set_retrans_timer(struct net_if *iface,
20302052
static inline uint32_t net_if_ipv6_get_retrans_timer(struct net_if *iface)
20312053
{
20322054
#if defined(CONFIG_NET_NATIVE_IPV6)
2033-
NET_ASSERT(iface);
2055+
if (iface == NULL) {
2056+
return 0;
2057+
}
20342058

20352059
if (!iface->config.ip.ipv6) {
20362060
return 0;
@@ -2367,7 +2391,9 @@ void net_if_ipv4_maddr_join(struct net_if *iface,
23672391
*/
23682392
static inline bool net_if_ipv4_maddr_is_joined(struct net_if_mcast_addr *addr)
23692393
{
2370-
NET_ASSERT(addr);
2394+
if (addr == NULL) {
2395+
return false;
2396+
}
23712397

23722398
return addr->is_joined;
23732399
}
@@ -2390,7 +2416,9 @@ void net_if_ipv4_maddr_leave(struct net_if *iface,
23902416
#if defined(CONFIG_NET_NATIVE_IPV4)
23912417
static inline struct in_addr *net_if_router_ipv4(struct net_if_router *router)
23922418
{
2393-
NET_ASSERT(router);
2419+
if (router == NULL) {
2420+
return NULL;
2421+
}
23942422

23952423
return &router->address.in_addr;
23962424
}
@@ -2837,7 +2865,9 @@ int net_if_up(struct net_if *iface);
28372865
*/
28382866
static inline bool net_if_is_up(struct net_if *iface)
28392867
{
2840-
NET_ASSERT(iface);
2868+
if (iface == NULL) {
2869+
return false;
2870+
}
28412871

28422872
return net_if_flag_is_set(iface, NET_IF_UP) &&
28432873
net_if_flag_is_set(iface, NET_IF_RUNNING);
@@ -2861,7 +2891,9 @@ int net_if_down(struct net_if *iface);
28612891
*/
28622892
static inline bool net_if_is_admin_up(struct net_if *iface)
28632893
{
2864-
NET_ASSERT(iface);
2894+
if (iface == NULL) {
2895+
return false;
2896+
}
28652897

28662898
return net_if_flag_is_set(iface, NET_IF_UP);
28672899
}
@@ -2895,7 +2927,9 @@ void net_if_carrier_off(struct net_if *iface);
28952927
*/
28962928
static inline bool net_if_is_carrier_ok(struct net_if *iface)
28972929
{
2898-
NET_ASSERT(iface);
2930+
if (iface == NULL) {
2931+
return false;
2932+
}
28992933

29002934
return net_if_flag_is_set(iface, NET_IF_LOWER_UP);
29012935
}
@@ -2931,7 +2965,9 @@ void net_if_dormant_off(struct net_if *iface);
29312965
*/
29322966
static inline bool net_if_is_dormant(struct net_if *iface)
29332967
{
2934-
NET_ASSERT(iface);
2968+
if (iface == NULL) {
2969+
return false;
2970+
}
29352971

29362972
return net_if_flag_is_set(iface, NET_IF_DORMANT);
29372973
}

0 commit comments

Comments
 (0)