Add m_snprintf() that won't return negative

This commit is contained in:
Matt Johnston 2022-04-01 12:10:48 +08:00
parent 552385280a
commit ac2433cb8d
2 changed files with 15 additions and 0 deletions

View File

@ -771,3 +771,16 @@ int fd_read_pending(int fd) {
return FD_ISSET(fd, &fds); return FD_ISSET(fd, &fds);
} }
} }
int m_snprintf(char *str, size_t size, const char *format, ...) {
va_list param;
int ret;
va_start(param, format);
ret = vsnprintf(str, size, format, param);
va_end(param);
if (ret < 0) {
dropbear_exit("snprintf failed");
}
return ret;
}

View File

@ -73,6 +73,8 @@ void m_close(int fd);
void setnonblocking(int fd); void setnonblocking(int fd);
void disallow_core(void); void disallow_core(void);
int m_str_to_uint(const char* str, unsigned int *val); int m_str_to_uint(const char* str, unsigned int *val);
/* The same as snprintf() but exits rather than returning negative */
int m_snprintf(char *str, size_t size, const char *format, ...);
/* Used to force mp_ints to be initialised */ /* Used to force mp_ints to be initialised */
#define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL} #define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL}