Use snprintf() to format SSID in patty_ax25_ntop()

This commit is contained in:
XANTRONIX Development 2020-09-17 22:00:00 -05:00 committed by XANTRONIX Industrial
parent 7041b73e0e
commit 29fbea2e18

View file

@ -1,3 +1,4 @@
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
@ -77,22 +78,6 @@ error_invalid_callsign:
return -1; return -1;
} }
static inline int expt(int base, int e) {
int ret;
if (e == 0) {
return 1;
}
ret = base;
while (--e) {
ret *= ret;
}
return ret;
}
int patty_ax25_ntop(const patty_ax25_addr *addr, int patty_ax25_ntop(const patty_ax25_addr *addr,
char *dest, char *dest,
size_t len) { size_t len) {
@ -118,29 +103,16 @@ int patty_ax25_ntop(const patty_ax25_addr *addr,
} }
if (ssid) { if (ssid) {
int place = 2; int formatted;
if (o == len) { if ((formatted = snprintf(&dest[o], 1 + len - o, "-%d", ssid)) < 0) {
goto error_overflow; goto error_format_ssid;
} } else {
o += formatted;
dest[o++] = '-';
while (place--) {
int div = expt(10, place),
num = ssid / div;
if (num) {
if (o == len) {
goto error_overflow;
}
dest[o++] = '0' + num;
}
} }
} }
if (o == len) { if (o > len) {
goto error_overflow; goto error_overflow;
} }
@ -148,6 +120,7 @@ int patty_ax25_ntop(const patty_ax25_addr *addr,
return 0; return 0;
error_format_ssid:
error_overflow: error_overflow:
errno = EOVERFLOW; errno = EOVERFLOW;