[ipxe-devel] [PATCH 1/2 v2] [core] vsnprintf: support %lld for formatting 64-bit integers

Johannes Thoma johannes at johannesthoma.com
Mon Feb 11 16:15:07 UTC 2019


In order to support sanboot-ing images larger than 2GB over http
we need to be able to format numbers larger than 2 ** 31 (for the
http range parameter).
---
 src/core/vsprintf.c       | 8 +++++---
 src/tests/vsprintf_test.c | 3 +++
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/core/vsprintf.c b/src/core/vsprintf.c
index 9d3a97c2..fc52842b 100644
--- a/src/core/vsprintf.c
+++ b/src/core/vsprintf.c
@@ -130,7 +130,7 @@ static char * format_hex ( char *end, unsigned long long num, int width,
  * There must be enough space in the buffer to contain the largest
  * number that this function can format.
  */
-static char * format_decimal ( char *end, signed long num, int width,
+static char * format_decimal ( char *end, signed long long num, int width,
 			       int flags ) {
 	char *ptr = end;
 	int negative = 0;
@@ -283,9 +283,11 @@ size_t vcprintf ( struct printf_context *ctx, const char *fmt, va_list args ) {
 			}
 			ptr = format_hex ( ptr, hex, width, flags );
 		} else if ( ( *fmt == 'd' ) || ( *fmt == 'i' ) ){
-			signed long decimal;
+			signed long long decimal;
 
-			if ( *length >= sizeof ( signed long ) ) {
+			if ( *length >= sizeof ( signed long long ) ) {
+				decimal = va_arg ( args, signed long long );
+			} else if ( *length >= sizeof ( signed long ) ) {
 				decimal = va_arg ( args, signed long );
 			} else {
 				decimal = va_arg ( args, signed int );
diff --git a/src/tests/vsprintf_test.c b/src/tests/vsprintf_test.c
index f388b3de..9ffec798 100644
--- a/src/tests/vsprintf_test.c
+++ b/src/tests/vsprintf_test.c
@@ -100,6 +100,9 @@ static void vsprintf_test_exec ( void ) {
 	snprintf_ok ( 16, "-072", "%04d", -72 );
 	snprintf_ok ( 16, "4", "%zd", sizeof ( uint32_t ) );
 	snprintf_ok ( 16, "123456789", "%d", 123456789 );
+	snprintf_ok ( 64, "123456789123456789", "%lld", 123456789123456789LL );
+	snprintf_ok ( 64, "-123456789123456789", "%lld",
+			   -123456789123456789LL );
 
 	/* Realistic combinations */
 	snprintf_ok ( 64, "DBG 0x1234 thingy at 0x0003f0c0+0x5c\n",
-- 
2.17.0




More information about the ipxe-devel mailing list