From 81457b26b9ce0283d346b7d8650e4f8467699012 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Tue, 20 Feb 2024 08:07:52 -0500
Subject: [PATCH] Add iniGetSString() that takes the size of the buffer

---
 src/xpdev/ini_file.c | 23 +++++++++++++++++++++++
 src/xpdev/ini_file.h |  5 +++++
 2 files changed, 28 insertions(+)

diff --git a/src/xpdev/ini_file.c b/src/xpdev/ini_file.c
index 6b2ed6285a..f3aaa40ea8 100644
--- a/src/xpdev/ini_file.c
+++ b/src/xpdev/ini_file.c
@@ -1038,6 +1038,29 @@ char* iniGetString(str_list_t list, const char* section, const char* key, const
 	return(vp);
 }
 
+char* iniGetSString(str_list_t list, const char* section, const char* key, const char* deflt, char* value, size_t sz)
+{
+	char fval[INI_MAX_VALUE_LEN];
+	char *ret;
+	size_t pos;
+
+	ret = iniGetString(list, section, key, deflt, value);
+	if (ret == NULL)
+		return ret;
+	if (sz < 1 || value == NULL)
+		return ret;
+	for (pos = 0; fval[pos]; pos++) {
+		if (pos == sz - 1) {
+			value[pos] = 0;
+			break;
+		}
+		value[pos] = fval[pos];
+	}
+	if (ret == fval)
+		return value;
+	return ret;
+}
+
 /* Does NOT support string literals: */
 char* iniGetValue(str_list_t list, const char* section, const char* key, const char* deflt, char* value)
 {
diff --git a/src/xpdev/ini_file.h b/src/xpdev/ini_file.h
index 0179019c2e..2049f4ad60 100644
--- a/src/xpdev/ini_file.h
+++ b/src/xpdev/ini_file.h
@@ -149,6 +149,11 @@ DLLEXPORT char* 		iniGetExistingValue(str_list_t, const char* section, const cha
 /* Return the string value (potentially string literals separated by colon rather than equal): */
 DLLEXPORT char* 		iniGetString(str_list_t, const char* section, const char* key
 							,const char* deflt, char* value /* may be NULL */);
+
+/* As above but specify the size of value */
+DLLEXPORT char*         iniGetSString(str_list_t listr, const char* section, const char* key
+                            ,const char* deflt, char* value /* may be NULL */, size_t sz);
+
 /* If the key doesn't exist, iniGetExistingString just returns NULL */
 DLLEXPORT char* 		iniGetExistingString(str_list_t, const char* section, const char* key
 							,const char* deflt, char* value /* may be NULL */);
-- 
GitLab