From 19022ee7547839690948a7a9807930a7891f3f15 Mon Sep 17 00:00:00 2001
From: midipix <writeonce@midipix.org>
Date: Sat, 10 Feb 2024 02:50:51 +0000
Subject: code base: simplify checks against value returned from snprintf() via
 wrapper.

---
 src/internal/slibtool_snprintf_impl.c | 39 +++++++++++++++++++++++++++++++++++
 src/internal/slibtool_snprintf_impl.h |  6 ++++++
 src/internal/slibtool_symlink_impl.c  | 10 +++++----
 3 files changed, 51 insertions(+), 4 deletions(-)
 create mode 100644 src/internal/slibtool_snprintf_impl.c
 create mode 100644 src/internal/slibtool_snprintf_impl.h

(limited to 'src/internal')

diff --git a/src/internal/slibtool_snprintf_impl.c b/src/internal/slibtool_snprintf_impl.c
new file mode 100644
index 0000000..d883af6
--- /dev/null
+++ b/src/internal/slibtool_snprintf_impl.c
@@ -0,0 +1,39 @@
+/*******************************************************************/
+/*  slibtool: a skinny libtool implementation, written in C        */
+/*  Copyright (C) 2016--2021  SysDeer Technologies, LLC            */
+/*  Released under the Standard MIT License; see COPYING.SLIBTOOL. */
+/*******************************************************************/
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
+#include "slibtool_snprintf_impl.h"
+
+
+/*****************************************************************/
+/* snprintf() wrapper that simplifies usage via the following:  */
+/*                                                             */
+/* (1) fail (return a negative result) in case the buffer is  */
+/*     not sufficiently large for the formatted string plus  */
+/*     the terminating null character.                      */
+/*                                                         */
+/**********************************************************/
+
+
+int slbt_snprintf(char * buf, size_t buflen, const char * fmt, ...)
+{
+	va_list	ap;
+	size_t  nbytes;
+
+	va_start(ap,fmt);
+	nbytes = vsnprintf(buf,buflen,fmt,ap);
+	va_end(ap);
+
+	if (nbytes >= buflen)
+		return -1;
+
+	return nbytes;
+}
diff --git a/src/internal/slibtool_snprintf_impl.h b/src/internal/slibtool_snprintf_impl.h
new file mode 100644
index 0000000..87fa65a
--- /dev/null
+++ b/src/internal/slibtool_snprintf_impl.h
@@ -0,0 +1,6 @@
+#ifndef SLIBTOOL_SNPRINTF_IMPL_H
+#define SLIBTOOL_SNPRINTF_IMPL_H
+
+int slbt_snprintf(char * buf, size_t buflen, const char * fmt, ...);
+
+#endif
diff --git a/src/internal/slibtool_symlink_impl.c b/src/internal/slibtool_symlink_impl.c
index 27f8171..19ec8ee 100644
--- a/src/internal/slibtool_symlink_impl.c
+++ b/src/internal/slibtool_symlink_impl.c
@@ -13,6 +13,7 @@
 #include "slibtool_errinfo_impl.h"
 #include "slibtool_symlink_impl.h"
 #include "slibtool_readlink_impl.h"
+#include "slibtool_snprintf_impl.h"
 
 #define SLBT_DEV_NULL_FLAGS	(SLBT_DRIVER_ALL_STATIC      \
 				| SLBT_DRIVER_DISABLE_SHARED \
@@ -70,13 +71,14 @@ int slbt_create_symlink(
 	dotdot = fwrapper ? "../" : "";
 
 	/* atarget */
-	if ((size_t)snprintf(atarget,sizeof(atarget),"%s%s",
-			dotdot,slash) >= sizeof(atarget))
+	if (slbt_snprintf(atarget,sizeof(atarget),
+			"%s%s",dotdot,slash) < 0)
 		return SLBT_BUFFER_ERROR(dctx);
 
 	/* tmplnk */
-	if ((size_t)snprintf(tmplnk,sizeof(tmplnk),"%s.symlink.tmp",
-			lnkname) >= sizeof(tmplnk))
+	if (slbt_snprintf(tmplnk,sizeof(tmplnk),
+			"%s.symlink.tmp",
+			lnkname) <0)
 		return SLBT_BUFFER_ERROR(dctx);
 
 	/* placeholder? */
-- 
cgit v1.2.3