Skip to content

Commit 91d2dcf

Browse files
kaabiajhedberg
authored andcommitted
drivers: gnss: Fix snprintk return value check in gnss_dump
The snprintk function returns the number of characters that *would have been* written if the buffer was large enough. This means if the return value `ret` is greater than or equal to the buffer size `strsize`, the output was truncated. The existing check `(strsize < ret)` was incorrect as it did not handle the case where `ret == strsize` (output fits exactly, but no room for null terminator) and did not check for negative return values which indicate an encoding error. This commit corrects the check to `(ret < 0 || ret >= strsize)` to properly detect truncation and errors, returning -ENOMEM in these cases. This fix is applied to `gnss_dump_nav_data`, `gnss_dump_time`, and `gnss_dump_satellite` functions. Signed-off-by: Badr Bacem KAABIA <badrbacemkaabia@gmail.com>
1 parent f5f258f commit 91d2dcf

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

drivers/gnss/gnss_dump.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ int gnss_dump_nav_data(char *str, uint16_t strsize, const struct navigation_data
112112
nav_data->speed / 1000, nav_data->speed % 1000,
113113
alt_sign, abs(nav_data->altitude) / 1000, abs(nav_data->altitude) % 1000);
114114

115-
return (strsize < ret) ? -ENOMEM : 0;
115+
return (ret < 0 || ret >= strsize) ? -ENOMEM : 0;
116116
}
117117

118118
int gnss_dump_time(char *str, uint16_t strsize, const struct gnss_time *utc)
@@ -124,7 +124,7 @@ int gnss_dump_time(char *str, uint16_t strsize, const struct gnss_time *utc)
124124
ret = snprintk(str, strsize, fmt, utc->hour, utc->minute, utc->millisecond,
125125
utc->month_day, utc->month, utc->century_year);
126126

127-
return (strsize < ret) ? -ENOMEM : 0;
127+
return (ret < 0 || ret >= strsize) ? -ENOMEM : 0;
128128
}
129129

130130
#if CONFIG_GNSS_SATELLITES
@@ -138,7 +138,7 @@ int gnss_dump_satellite(char *str, uint16_t strsize, const struct gnss_satellite
138138
satellite->azimuth, gnss_system_to_str(satellite->system),
139139
satellite->is_tracked);
140140

141-
return (strsize < ret) ? -ENOMEM : 0;
141+
return (ret < 0 || ret >= strsize) ? -ENOMEM : 0;
142142
}
143143
#endif
144144

0 commit comments

Comments
 (0)