|
From: <kin...@us...> - 2025-10-02 05:16:32
|
Revision: 7561
http://sourceforge.net/p/teem/code/7561
Author: kindlmann
Date: 2025-10-02 05:16:30 +0000 (Thu, 02 Oct 2025)
Log Message:
-----------
using more snprintf instead of sprintf, and disclaiming sprintf use more
Modified Paths:
--------------
teem/trunk/src/air/enum.c
teem/trunk/src/air/miscAir.c
Modified: teem/trunk/src/air/enum.c
===================================================================
--- teem/trunk/src/air/enum.c 2025-10-02 04:46:15 UTC (rev 7560)
+++ teem/trunk/src/air/enum.c 2025-10-02 05:16:30 UTC (rev 7561)
@@ -142,18 +142,17 @@
}
/*
-******** airEnumFmtDesc()
-**
-** Formats a description line for one element "val" of airEnum "enm",
-** and puts the result in a NEWLY ALLOCATED string which is the return
-** of this function. The formatting is done via sprintf(), as governed
-** by "fmt", which should contain to "%s" conversion sequences, the
-** first for the string version "val", and the second for the
-** description If "canon", then the canonical string representation
-** will be used (the one in enm->str[]), otherwise the shortest string
-** representation will be used (which differs from the canonical one
-** when there is a strEqv[]/valEqv[] pair defining a shorter string)
-*/
+ ******* airEnumFmtDesc()
+ *
+ * Formats a description line for one element `val` of airEnum `enm`, and puts the result
+ * in a NEWLY ALLOCATED string which is the return of this function. The formatting is
+ * done via snprintf(), as governed by `fmt`, which should contain two `%s` conversion
+ * sequences, the first for the string version `val`, and the second for the description.
+ * If `canon`, then the canonical string representation will be used (the one in
+ * enm->str[]), otherwise the shortest string representation will be used (which differs
+ * from the canonical one when there is a strEqv[]/valEqv[] pair defining a shorter
+ * string)
+ */
char *
airEnumFmtDesc(const airEnum *enm, int val, int canon, const char *fmt) {
const char *desc;
@@ -160,7 +159,7 @@
char *buff, ident[AIR_STRLEN_SMALL + 1];
const char *_ident;
int i;
- size_t len;
+ size_t bsize, len;
if (!(enm && enm->desc && fmt)) {
return airStrdup("(airEnumDesc: invalid args)");
@@ -188,9 +187,11 @@
airToLower(ident);
}
desc = enm->desc[_airEnumIndex(enm, val)];
- buff = AIR_CALLOC(airStrlen(fmt) + airStrlen(ident) + airStrlen(desc) + 1, char);
+ bsize = airStrlen(fmt) + airStrlen(ident) + airStrlen(desc) + 1;
+ buff = AIR_CALLOC(bsize, char);
if (buff) {
- sprintf(buff, fmt, ident, desc);
+ /* snprintf just in case our math is wrong */
+ snprintf(buff, bsize, fmt, ident, desc);
}
return buff;
}
Modified: teem/trunk/src/air/miscAir.c
===================================================================
--- teem/trunk/src/air/miscAir.c 2025-10-02 04:46:15 UTC (rev 7560)
+++ teem/trunk/src/air/miscAir.c 2025-10-02 05:16:30 UTC (rev 7561)
@@ -48,9 +48,9 @@
*/
void
airTeemVersionSprint(char buff[AIR_STRLEN_LARGE + 1]) {
- sprintf(buff, "Teem version %s, %s%s%s", airTeemVersion,
- airTeemReleaseDone ? "released on " : "", airTeemReleaseDate,
- airTeemReleaseDone ? "" : " (not yet released)");
+ snprintf(buff, AIR_STRLEN_LARGE + 1, "Teem version %s, %s%s%s", airTeemVersion,
+ airTeemReleaseDone ? "released on " : "", airTeemReleaseDate,
+ airTeemReleaseDone ? "" : " (not yet released)");
return;
}
@@ -152,23 +152,25 @@
}
/*
-******** airSinglePrintf
-**
-** a complete stand-in for {f|s}printf(), as long as the given format
-** string contains exactly one conversion sequence. The utility of
-** this is to standardize the printing of IEEE 754 special values:
-** NAN (any kind) -> "NaN"
-** POS_INF -> "+inf"
-** NEG_INF -> "-inf"
-** The format string can contain other things besides just the
-** conversion sequence: airSingleFprintf(f, " (%f)\n", AIR_NAN)
-** will be the same as fprintf(f, " (%s)\n", "NaN");
-**
-** To get fprintf behavior, pass "str" as NULL
-** to get sprintf bahavior, pass "file" as NULL
-**
-** Finding a complete {f|s|}printf replacement is a priority for Teem 2.0
-*/
+ ******* airSinglePrintf
+ *
+ * a complete stand-in for {f|s}printf(), as long as the given format string contains
+ * exactly one conversion sequence, and does use any precision modifiers. The utility of
+ * this is to standardize the printing of IEEE 754 special values:
+ * NAN (any kind) -> "NaN"
+ * POS_INF -> "+inf"
+ * NEG_INF -> "-inf"
+ * The format string can contain other things besides just the conversion sequence:
+ * airSinglePrintf(f, NULL, " (%f)\n", AIR_NAN) will be the same as:
+ * fprintf(f, " (%s)\n", "NaN");
+ *
+ * To get fprintf behavior, pass "str" as NULL
+ * to get sprintf bahavior, pass "file" as NULL. AND NOTE THAT THIS DOES USE sprintf
+ * and not snprintf because we're not in a position to know what the buffer size is.
+ *
+ * Finding a complete {f|s|}printf replacement would be great, but finding one compatible
+ * with our LGPL+linking exception is hard.
+ */
int
airSinglePrintf(FILE *file, char *str, const char *_fmt, ...) {
char *fmt, buff[AIR_STRLEN_LARGE + 1];
@@ -368,7 +370,7 @@
break;
}
}
- sprintf(str, "%g %s", dval, suff[suffIdx]);
+ snprintf(str, AIR_STRLEN_SMALL + 1, "%g %s", dval, suff[suffIdx]);
return str;
}
@@ -566,15 +568,15 @@
}
/*
-******* airDoneStr()
-**
-** dinky little utility for generating progress messages of the form
-** " 1.9%" or " 35.3%" or "100.0%"
-**
-** The message will ALWAYS be six characters, and will ALWAYS be
-** preceeded by six backspaces. Thus, you pass in a string to print
-** into, and it had better be allocated for at least 6+6+1 = 13 chars.
-*/
+ ****** airDoneStr()
+ *
+ * dinky little utility for generating progress messages of the form
+ * " 1.9%" or " 35.3%" or "100.0%"
+ *
+ * The message will ALWAYS be six characters, and will ALWAYS be preceeded by six
+ * backspaces. Thus, you pass in a string to sprintf() into (yes sprintf not snprintf),
+ * and it had better be allocated for at least 6+6+1 = 13 chars.
+ */
char *
airDoneStr(double start, double here, double end, char *str) {
int perc = 0;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|