From 8207cffcad07d5c8023fabe803599bf704dd67b3 Mon Sep 17 00:00:00 2001 From: Mike Hommey Date: Fri, 12 Feb 2021 20:21:49 +0000 Subject: [PATCH] Bug 1690167 - Add support for the hh length modifier in Printf.cpp. r=nika,Gankra Test cases from TestIntegerPrintfMacros will cover this in the next commit. Differential Revision: https://phabricator.services.mozilla.com/D103729 --- mozglue/misc/Printf.cpp | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/mozglue/misc/Printf.cpp b/mozglue/misc/Printf.cpp index 338fc54d72bf..2a19688b9f99 100644 --- a/mozglue/misc/Printf.cpp +++ b/mozglue/misc/Printf.cpp @@ -70,6 +70,8 @@ using NumArgStateVector = #if defined(XP_WIN) # define TYPE_WSTRING 12 #endif +#define TYPE_SCHAR 14 +#define TYPE_UCHAR 15 #define TYPE_UNKNOWN 20 #define FLAG_LEFT 0x1 @@ -534,6 +536,10 @@ static bool BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) { if (c == 'h') { nas[cn].type = TYPE_SHORT; c = *p++; + if (c == 'h') { + nas[cn].type = TYPE_SCHAR; + c = *p++; + } } else if (c == 'L') { nas[cn].type = TYPE_LONGLONG; c = *p++; @@ -642,6 +648,8 @@ static bool BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) { VARARGS_ASSIGN(nas[cn].ap, ap); switch (nas[cn].type) { + case TYPE_SCHAR: + case TYPE_UCHAR: case TYPE_SHORT: case TYPE_USHORT: case TYPE_INTN: @@ -821,6 +829,10 @@ bool mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) { if (c == 'h') { type = TYPE_SHORT; c = *fmt++; + if (c == 'h') { + type = TYPE_SCHAR; + c = *fmt++; + } } else if (c == 'L') { type = TYPE_LONGLONG; c = *fmt++; @@ -882,8 +894,18 @@ bool mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) { fetch_and_convert: switch (type) { + case TYPE_SCHAR: + u.l = (signed char)va_arg(ap, int); + if (u.l < 0) { + u.l = -u.l; + flags |= FLAG_NEG; + } + goto do_long; + case TYPE_UCHAR: + u.l = (unsigned char)va_arg(ap, unsigned int); + goto do_long; case TYPE_SHORT: - u.l = va_arg(ap, int); + u.l = (short)va_arg(ap, int); if (u.l < 0) { u.l = -u.l; flags |= FLAG_NEG; @@ -1069,6 +1091,8 @@ bool mozilla::PrintfTarget::print(const char* format, ...) { #undef TYPE_POINTER #undef TYPE_WSTRING #undef TYPE_UNKNOWN +#undef TYPE_SCHAR +#undef TYPE_UCHAR #undef FLAG_LEFT #undef FLAG_SIGNED