summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBoris Kolpackov <boris@codesynthesis.com>2020-07-22 07:47:22 +0200
committerBoris Kolpackov <boris@codesynthesis.com>2020-07-22 07:51:02 +0200
commit6ca0f4910364f4522ad91b973e5ce18cbbee0eed (patch)
treeb04d54923fb27ef7b5f3350d9a82b43cae6c8f2c
parentd29196d9e2a00cd556f9452259147e02692b6bb2 (diff)
Use snprintf() instead of sprintf() unless instructed otherwise
-rw-r--r--genx.c23
1 files changed, 21 insertions, 2 deletions
diff --git a/genx.c b/genx.c
index b2b999f..1da1f61 100644
--- a/genx.c
+++ b/genx.c
@@ -7,6 +7,11 @@
#define GENX_VERSION "cs-1"
+/* Use snprintf() unless instructed otherwise. */
+#ifndef GENX_SNPRINTF
+# define GENX_SNPRINTF 1
+#endif
+
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -362,7 +367,18 @@ static utf8 storePrefix(genxWriter w, constUtf8 prefix, Boolean force)
prefix = (utf8) "xmlns";
else
{
- sprintf((char *) buf, "xmlns:%s", prefix);
+ size_t pl = strlen((const char *) prefix);
+
+ if (pl > sizeof(buf) - (6 + 1))
+ {
+ w->status = GENX_BAD_NAMESPACE_NAME;
+ return NULL;
+ }
+
+ memcpy (buf, "xmlns:", 6);
+ memcpy (buf + 6, prefix, pl);
+ buf[pl + 6] = 0;
+
prefix = buf;
}
@@ -988,11 +1004,14 @@ genxNamespace genxDeclareNamespace(genxWriter w, constUtf8 uri,
/* wasn't already declared */
else
{
-
/* make a default prefix if none provided */
if (defaultPref == NULL)
{
+#if GENX_SNPRINTF
+ snprintf((char *) newPrefix, sizeof(newPrefix), "g%d", w->nextPrefix++);
+#else
sprintf((char *) newPrefix, "g%d", w->nextPrefix++);
+#endif
defaultPref = newPrefix;
}