Skip to content

Commit 6caf696

Browse files
miyatsuAnas Nashif
authored andcommitted
net: pkt: Fix possible division by zero
When net debugging is enabled, the count variable is initialized to -1. This may cause division by zero if there is only one fragment in pkt. Solve this by setting the count to 0 and checking the value before the print at the end of the function. Successfully tested on STM32F407 SoC. Signed-off-by: Ding Tao <[email protected]>
1 parent 21b94ba commit 6caf696

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

subsys/net/ip/net_pkt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ void net_pkt_print_frags(struct net_pkt *pkt)
254254
{
255255
struct net_buf *frag;
256256
size_t total = 0;
257-
int count = -1, frag_size = 0, ll_overhead = 0;
257+
int count = 0, frag_size = 0, ll_overhead = 0;
258258

259259
if (!pkt) {
260260
NET_INFO("pkt %p", pkt);
@@ -287,7 +287,7 @@ void net_pkt_print_frags(struct net_pkt *pkt)
287287
NET_INFO("Total data size %zu, occupied %d bytes, ll overhead %d, "
288288
"utilization %zu%%",
289289
total, count * frag_size - count * ll_overhead,
290-
count * ll_overhead, (total * 100) / (count * frag_size));
290+
count * ll_overhead, count ? (total * 100) / (count * frag_size) : 0);
291291
}
292292

293293
struct net_pkt *net_pkt_get_reserve_debug(struct k_mem_slab *slab,

0 commit comments

Comments
 (0)