From 4caaa7ca0b583440eeabdceff2895a0668b50ffb Mon Sep 17 00:00:00 2001 From: Jeffrey Altman Date: Fri, 14 Jan 2022 12:17:39 -0500 Subject: [PATCH] include: bits conditionalize behavior on HAVE_SNPRINTF ee2a92c5474430bf839e0f698a47332ae6c1ad2a ("bits: Fix warnings") introduced unconditional use of snprintf(). Use of snprintf in most of the tree is acceptable whenever roken is available because roken provides snprintf() for platforms that do not provide it. However, bits.c cannot use roken and therefore must not unconditionally call snprintf(). This change restores the prior implementation of BITSIZE(TYPE) when HAVE_SNPRINTF is undefined. Change-Id: Ibbd2f003abe9f9fb612719266544cfd545809a18 --- include/bits.c | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/include/bits.c b/include/bits.c index 0ec2ff1f6..6abdb15c9 100644 --- a/include/bits.c +++ b/include/bits.c @@ -46,6 +46,7 @@ RCSID("$Id$"); #include #endif +#ifdef HAVE_SNPRINTF #define BITSIZE(TYPE) \ { \ int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \ @@ -62,7 +63,25 @@ RCSID("$Id$"); return; \ } \ } - +#else +#define BITSIZE(TYPE) \ +{ \ + int b = 0; TYPE x = 1, zero = 0; const char *pre = "u"; \ + char tmp[128], tmp2[128]; \ + while(x){ x <<= 1; b++; if(x < zero) pre=""; } \ + if(b >= len){ \ + size_t tabs; \ + sprintf(tmp, "%sint%d_t" , pre, len); \ + sprintf(tmp2, "typedef %s %s;", #TYPE, tmp); \ + tabs = 5 - strlen(tmp2) / 8; \ + fprintf(f, "%s", tmp2); \ + while(tabs-- > 0) \ + fprintf(f, "\t"); \ + fprintf(f, "/* %2d bits */\n", b); \ + return; \ + } \ +} +#endif #ifndef HAVE___ATTRIBUTE__ #define __attribute__(x) #endif