changeset 1934:83b6a47759a8

Add m_snprintf() that won't return negative
author Matt Johnston <matt@ucc.asn.au>
date Fri, 01 Apr 2022 12:10:48 +0800
parents e093ddc5b585
children a7ad060707b6
files dbutil.c dbutil.h
diffstat 2 files changed, 15 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dbutil.c	Fri Apr 01 11:56:10 2022 +0800
+++ b/dbutil.c	Fri Apr 01 12:10:48 2022 +0800
@@ -771,3 +771,16 @@
 		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;
+}
--- a/dbutil.h	Fri Apr 01 11:56:10 2022 +0800
+++ b/dbutil.h	Fri Apr 01 12:10:48 2022 +0800
@@ -73,6 +73,8 @@
 void setnonblocking(int fd);
 void disallow_core(void);
 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 */
 #define DEF_MP_INT(X) mp_int X = {0, 0, 0, NULL}