Skip to content

Commit

Permalink
sprintf_s: strip leading zeros and dot on %g (not %#g)
Browse files Browse the repository at this point in the history
fixes parts of GH #68
  • Loading branch information
rurban committed Dec 26, 2024
1 parent 098566a commit 5a88600
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@

ChangeLog file for safeclib

Changes in 3.9
- Add long double support to vsnprintf_s. GH #103
With fixes for %a, %g, %G, %#g.
- Fixed %lc, %ls bug in vsnprintf_s.
- Fixes for non-POSIX systems missing fileno and ftruncate. GH #136
(ecospro compiler)

Changes in 3.8.2
- Fixed vsnprintf_s safec_etoa div-by-zero exceptions. GH #144
- Fixed C90 compliance, decl-after-stmt. Matthias Schulz.
Expand Down
22 changes: 17 additions & 5 deletions src/str/vsnprintf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ static size_t safec_ftoa(out_fct_type out, const char *funcname, char *buffer,
unsigned int prec, unsigned int width,
unsigned int flags) {
char buf[PRINTF_FTOA_BUFFER_SIZE];
size_t len = 0U;
size_t len = 0U, off = 0U;
double tmp;
double diff = 0.0;
unsigned long frac;
Expand Down Expand Up @@ -525,6 +525,19 @@ static size_t safec_ftoa(out_fct_type out, const char *funcname, char *buffer,
}
}

// strip leading zeros and dots
if ((flags & FLAGS_ADAPT_EXP) && !(flags & FLAGS_HASH)) {
size_t olen = len;
while (buf[off] == '0') {
off++; len--;
if (off >= olen)
break;
}
if (buf[off] == '.' && off < olen) {
off++; len--;
}
}

if (len < PRINTF_FTOA_BUFFER_SIZE) {
if (negative) {
buf[len++] = '-';
Expand All @@ -535,14 +548,14 @@ static size_t safec_ftoa(out_fct_type out, const char *funcname, char *buffer,
}
}

return safec_out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
return safec_out_rev(out, buffer, idx, maxlen, &buf[off], len, width, flags);
}

#ifdef PRINTF_SUPPORT_LONG_DOUBLE
#ifdef HAVE_ISINFL
#define _ISINFL(value) isinfl(value)
#else
#define _ISINFL(x) x == x &&x > 0x7fff8000000000000000
#define _ISINFL(x) x == x && x > 0x7fff8000000000000000ULL
#endif

// internal ftoa for fixed decimal long double
Expand Down Expand Up @@ -699,7 +712,6 @@ static size_t safec_etoa(out_fct_type out, const char *funcname, char *buffer,
// in "%g" mode, "prec" is the number of *significant figures* not decimals
if (flags & FLAGS_ADAPT_EXP) {
// do we want to fall-back to "%f" mode?
// TODO: strip trailing zeros and dot
if ((flags & FLAGS_HASH) || ((value >= 1e-4) && (value < 1e6))) {
if ((int)prec > expval) {
prec = (unsigned)((int)prec - expval - 1);
Expand Down Expand Up @@ -743,7 +755,7 @@ static size_t safec_etoa(out_fct_type out, const char *funcname, char *buffer,
const size_t start_idx = idx;
idx = safec_ftoa(out, funcname, buffer, idx, maxlen,
negative ? -value : value, prec, fwidth,
flags & ~FLAGS_ADAPT_EXP);
flags);

// output the exponent part
if (minwidth) {
Expand Down
10 changes: 5 additions & 5 deletions tests/test_sprintf_s.c
Original file line number Diff line number Diff line change
Expand Up @@ -393,22 +393,22 @@ int test_sprintf_s(void) {
EXPSTR(str2, "0.100000")
rc = sprintf_s(str2, LEN, "%g", 0.1);
NOERRNULL()
EXPSTR(str2, "0.1") // FIXME 0.100000 trailing-zeros not stripped
EXPSTR(str2, "0.1")
rc = sprintf_s(str2, LEN, "%#g", 0.1); // alternate form
NOERRNULL()
EXPSTR(str2, "0.100000")
rc = sprintf_s(str2, LEN, "%G", 0.1);
NOERRNULL()
EXPSTR(str2, "0.1") // FIXME 0.100000 trailing-zeros not stripped
EXPSTR(str2, "0.1")
rc = sprintf_s(str2, LEN, "%g", 0.1f);
NOERRNULL()
EXPSTR(str2, "0.1") // FIXME 0.100000 trailing-zeros not stripped
EXPSTR(str2, "0.1")
rc = sprintf_s(str2, LEN, "%g", 1.0);
NOERRNULL()
EXPSTR(str2, "1") // FIXME 0.100000 trailing-zeros and dot not stripped
EXPSTR(str2, "1")
rc = sprintf_s(str2, LEN, "%lg", 0.1);
NOERRNULL()
EXPSTR(str2, "0.1") // FIXME 0.100000 trailing-zeros not stripped
EXPSTR(str2, "0.1")
rc = sprintf_s(str2, LEN, "%a", 0.1f);
NOERRNULL()
EXPSTR(str2, "0x1.99999ap-4")
Expand Down

0 comments on commit 5a88600

Please sign in to comment.