diff mbox series

[usign] Use static buffer for default sigfile path

Message ID aOLahSIWurGdViOm@cloudsdale.lanodan.eu
State New
Headers show
Series [usign] Use static buffer for default sigfile path | expand

Commit Message

Haelwenn (lanodan) Monnier Oct. 5, 2025, 8:52 p.m. UTC
This allows to compile usign with TinyCC on architectures like AArch64
and riscv where it is missing an implementation of alloca.

VLA could also be used as this is C99 code, but would reduce portability.

malloc could be considered but musl's malloc is over 4KB of code and
dynamic/static analyzers would require a bunch of free().

Signed-off-by: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
---
 main.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)


base-commit: f1f65026a94137c91b5466b149ef3ea3f20091e9
diff mbox series

Patch

diff --git a/main.c b/main.c
index ebfdfb0..addbfb8 100644
--- a/main.c
+++ b/main.c
@@ -26,11 +26,16 @@ 
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
+#include <limits.h>
 
 #include "base64.h"
 #include "edsign.h"
 #include "ed25519.h"
 
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
 struct pubkey {
 	char pkalg[2];
 	uint8_t fingerprint[8];
@@ -409,14 +414,19 @@  int main(int argc, char **argv)
 	}
 
 	if (!sigfile && msgfile) {
-		char *buf = alloca(strlen(msgfile) + 5);
-
 		if (!strcmp(msgfile, "-")) {
 			fprintf(stderr, "Need signature file when reading message from stdin\n");
 			return 1;
 		}
 
-		sprintf(buf, "%s.sig", msgfile);
+		if ((strlen(msgfile) - 4) > PATH_MAX)
+		{
+			fprintf(stderr, "Need signature file when msgfile path is over %zd (PATH_MAX - 4)\n", (size_t)PATH_MAX);
+			return 1;
+		}
+
+		static char buf[PATH_MAX];
+		snprintf(buf, PATH_MAX, "%s.sig", msgfile);
 		sigfile = buf;
 	}