Skip to contentSkip to conversation

misc: Add grub_sprintf() C99/POSIX non-conformance comment

PATCH New Glenn Washburn 1 participant 1 message Jul 14, 2023

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: Add grub_sprintf() C99/POSIX non-conformance comment

Make clear that grub_sprintf() does not conform to C99/POSIX. It returns the number of bytes written, even when n is less than the length of the fully formatted string.

---
 grub-core/kern/misc.c | 9 +++++++++
 1 file changed, 9 insertions(+)
grub-core/kern/misc.c +9 -0
index 2890aad49fa7..634bf1441c63 100644
@@ -983,6 +983,7 @@ write_number (char *str, grub_size_t *count, grub_size_t max_len, grub_size_t fo
       write_char (str, count, max_len, zerofill);
 }
 
+/* See grub_snprintf for details of non-compliance to C99 and POSIX */
 static int
 grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0,
 		     struct printf_args *args)
@@ -1204,6 +1205,14 @@ grub_vsnprintf (char *str, grub_size_t n, const char *fmt, va_list ap)
   return ret < n ? ret : n;
 }
 
+/*
+ * Note: grub_snprintf does not conform to C99 nor the POSIX specs.
+ * Specifically, it returns the number of bytes written, minus the NULL
+ * byte. Whereas, the specs say that the return value should be the
+ * number of bytes that *would* be written if n were sufficiently large
+ * to contain the formatted output. This only matters when n is smaller
+ * than the formatted string.
+ */
 int
 grub_snprintf (char *str, grub_size_t n, const char *fmt, ...)
 {
Signature · 1 line
2.34.1