From e4ca8536c49142ac994f914fc4b84ce1c4ed2252 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Deuc=D0=B5?= <shurd@sasktel.net>
Date: Tue, 5 Mar 2024 00:14:01 -0500
Subject: [PATCH] Do lprint() pointer instead of lprintf() pointer.

This doesn't make anything better or worse for the C API, but doing
variadic pointer things in C++ is fraught.
---
 src/sbbs3/sftp.cpp     | 12 +++---------
 src/sftp/sftp.h        |  4 ++--
 src/sftp/sftp_server.c | 38 ++++++++++++++++++++++++++------------
 src/sftp/sftp_str.c    |  2 +-
 4 files changed, 32 insertions(+), 24 deletions(-)

diff --git a/src/sbbs3/sftp.cpp b/src/sbbs3/sftp.cpp
index 7f2a1f61b2..a3a3934ff0 100644
--- a/src/sbbs3/sftp.cpp
+++ b/src/sbbs3/sftp.cpp
@@ -1320,11 +1320,9 @@ sftp_send(uint8_t *buf, size_t len, void *cb_data)
 }
 
 static void
-sftp_lprintf(void *arg, uint32_t errcode, const char *fmt, ...)
+sftp_lprint(void *arg, uint32_t errcode, const char *msg)
 {
 	sbbs_t *sbbs = (sbbs_t *)arg;
-	va_list argptr;
-	char sbuf[1024];
 	int level = LOG_DEBUG;
 
 	switch (errcode) {
@@ -1337,11 +1335,7 @@ sftp_lprintf(void *arg, uint32_t errcode, const char *fmt, ...)
 			break;
 	}
 
-	va_start(argptr,fmt);
-	vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
-	sbuf[sizeof(sbuf)-1]=0;
-	va_end(argptr);
-	sbbs->lprintf(level, "SFTP error code %" PRIu32 " (%s) %s", errcode, sftp_get_errcode_name(errcode), sbuf);
+	sbbs->lprintf(level, "SFTP error code %" PRIu32 " (%s) %s", errcode, sftp_get_errcode_name(errcode), msg);
 }
 
 static void
@@ -2153,7 +2147,7 @@ sbbs_t::init_sftp(int cid)
 		return true;
 	sftp_state = sftps_begin(sftp_send, this);
 	if (sftp_state != nullptr) {
-		sftp_state->lprintf = sftp_lprintf;
+		sftp_state->lprint = sftp_lprint;
 		sftp_state->cleanup_callback = sftp_cleanup_callback;
 		sftp_state->realpath = sftp_realpath;
 		sftp_state->open = sftp_open;
diff --git a/src/sftp/sftp.h b/src/sftp/sftp.h
index 09d1da4bc3..d62b646594 100644
--- a/src/sftp/sftp.h
+++ b/src/sftp/sftp.h
@@ -193,7 +193,7 @@ typedef struct sftp_server_state {
 	sftp_rx_pkt_t rxp;
 	sftp_tx_pkt_t txp;
 	void *cb_data;
-	void (*lprintf)(void *cb_data, uint32_t errcode, const char *fmt, ...);
+	void (*lprint)(void *cb_data, uint32_t errcode, const char *msg);
 	void (*cleanup_callback)(void *cb_data);
 	bool (*open)(sftp_str_t filename, uint32_t flags, sftp_file_attr_t attributes, void *cb_data);
 	bool (*close)(sftp_str_t handle, void *cb_data);
@@ -250,7 +250,7 @@ bool sftp_rx_pkt_reclaim(sftp_rx_pkt_t *pktp);
 sftp_str_t sftp_alloc_str(uint32_t len);
 sftp_str_t sftp_strdup(const char *str);
 sftp_str_t sftp_asprintf(const char *format, ...);
-sftp_str_t sftp_memdup(uint8_t *buf, uint32_t sz);
+sftp_str_t sftp_memdup(const uint8_t *buf, uint32_t sz);
 void free_sftp_str(sftp_str_t str);
 
 /* sftp_client.c */
diff --git a/src/sftp/sftp_server.c b/src/sftp/sftp_server.c
index c08366c6fb..58d5dcdd46 100644
--- a/src/sftp/sftp_server.c
+++ b/src/sftp/sftp_server.c
@@ -1,5 +1,6 @@
 #include <assert.h>
 #include <genwrap.h>
+#include <stdarg.h>
 #include <stdlib.h>
 #include <threadwrap.h>
 #include <str_list.h>
@@ -326,11 +327,29 @@ sftps_send_packet(sftps_state_t state)
 	return true;
 }
 
+static void
+lprintf(sftps_state_t state, uint32_t code, const char *fmt, ...)
+{
+	char *msg;
+	va_list va;
+	int rc;
+
+	if (state->lprint == NULL)
+		return;
+	if (fmt == NULL)
+		return;
+	va_start(va, fmt);
+	rc = vasprintf(&msg, fmt, va);
+	va_end(va);
+	if (rc == -1)
+		return;
+	state->lprint(state->cb_data, code, msg);
+}
+
 bool
 sftps_send_error(sftps_state_t state, uint32_t code, const char *msg)
 {
-	if (state->lprintf)
-		state->lprintf(state->cb_data, code, "%s", msg);
+	lprintf(state, code, "%s", msg);
 	if (!appendheader(state, SSH_FXP_STATUS))
 		return false;
 	if (!append32(state, code))
@@ -375,8 +394,7 @@ sftps_recv(sftps_state_t state, uint8_t *buf, uint32_t sz)
 	if (sftp_have_pkt_sz(state->rxp)) {
 		uint32_t psz = sftp_pkt_sz(state->rxp);
 		if (psz > SFTP_MAX_PACKET_SIZE) {
-			if (state->lprintf)
-				state->lprintf(state->cb_data, SSH_FX_FAILURE, "Packet too large (%" PRIu32 " bytes)", psz);
+			lprintf(state, SSH_FX_FAILURE, "Packet too large (%" PRIu32 " bytes)", psz);
 			return exit_function(state, false);
 		}
 	}
@@ -528,8 +546,7 @@ sftps_recv(sftps_state_t state, uint8_t *buf, uint32_t sz)
 				break;
 		}
 		if (!handled) {
-			if (state->lprintf)
-				state->lprintf(state->cb_data, SSH_FX_FAILURE, "Unhandled request type: %s (%d)", sftp_get_type_name(state->rxp->type), state->rxp->type);
+			lprintf(state, SSH_FX_FAILURE, "Unhandled request type: %s (%d)", sftp_get_type_name(state->rxp->type), state->rxp->type);
 			state->id = get32(state);
 			if (!sftps_send_error(state, SSH_FX_OP_UNSUPPORTED, "Operation not implemented"))
 				return exit_function(state, false);
@@ -568,22 +585,19 @@ sftps_send_name(sftps_state_t state, uint32_t count, str_list_t fnames, str_list
 		return false;
 	for (uint32_t idx = 0; idx < count; idx++) {
 		if (fnames[idx] == NULL) {
-			if (state->lprintf)
-				state->lprintf(state->cb_data, SSH_FX_FAILURE, "Reached fnames terminator at position %" PRIu32 " of " PRIu32, idx, count);
+			lprintf(state, SSH_FX_FAILURE, "Reached fnames terminator at position %" PRIu32 " of " PRIu32, idx, count);
 			return false;
 		}
 		if (!appendcstring(state, fnames[idx]))
 			return false;
 		if (lnames[idx] == NULL) {
-			if (state->lprintf)
-				state->lprintf(state->cb_data, SSH_FX_FAILURE, "Reached lnames terminator at position %" PRIu32 " of " PRIu32, idx, count);
+			lprintf(state, SSH_FX_FAILURE, "Reached lnames terminator at position %" PRIu32 " of " PRIu32, idx, count);
 			return false;
 		}
 		if (!appendcstring(state, lnames[idx]))
 			return false;
 		if (attrs[idx] == NULL) {
-			if (state->lprintf)
-				state->lprintf(state->cb_data, SSH_FX_FAILURE, "Reached attrs terminator at position %" PRIu32 " of " PRIu32, idx, count);
+			lprintf(state, SSH_FX_FAILURE, "Reached attrs terminator at position %" PRIu32 " of " PRIu32, idx, count);
 			return false;
 		}
 		if (!appendfattr(state, attrs[idx]))
diff --git a/src/sftp/sftp_str.c b/src/sftp/sftp_str.c
index d9699d25f0..4d34ca7e9f 100644
--- a/src/sftp/sftp_str.c
+++ b/src/sftp/sftp_str.c
@@ -56,7 +56,7 @@ sftp_asprintf(const char *format, ...)
 }
 
 sftp_str_t
-sftp_memdup(uint8_t *buf, uint32_t sz)
+sftp_memdup(const uint8_t *buf, uint32_t sz)
 {
 	sftp_str_t ret = sftp_alloc_str(sz);
 	if (ret == NULL)
-- 
GitLab