Skip to content

Commit be93048

Browse files
committed
[rt.perl.org #128893]: printf %a botches 0 flag for negative values
1 parent 75326c4 commit be93048

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

sv.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12421,6 +12421,7 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1242112421
int exponent = 0; /* exponent of the floating point input */
1242212422
bool hexradix = FALSE; /* should we output the radix */
1242312423
bool subnormal = FALSE; /* IEEE 754 subnormal/denormal */
12424+
bool negative = FALSE;
1242412425

1242512426
/* XXX: NaN, Inf -- though they are printed as "NaN" and "Inf".
1242612427
*
@@ -12448,9 +12449,8 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1244812449
# endif
1244912450
#endif
1245012451

12451-
if (fv < 0
12452-
|| Perl_signbit(nv)
12453-
)
12452+
negative = fv < 0 || Perl_signbit(nv);
12453+
if (negative)
1245412454
*p++ = '-';
1245512455
else if (plus)
1245612456
*p++ = plus;
@@ -12628,12 +12628,18 @@ Perl_sv_vcatpvfn_flags(pTHX_ SV *const sv, const char *const pat, const STRLEN p
1262812628
memset(PL_efloatbuf + elen, ' ', width - elen);
1262912629
}
1263012630
else if (fill == '0') {
12631-
/* Insert the zeros between the "0x" and
12632-
* the digits, otherwise we end up with
12633-
* "0000xHHH..." */
12631+
/* Insert the zeros after the "0x" and the
12632+
* the potential sign, but before the digits,
12633+
* otherwise we end up with "0000xH.HHH...",
12634+
* when we want "0x000H.HHH..." */
1263412635
STRLEN nzero = width - elen;
1263512636
char* zerox = PL_efloatbuf + 2;
12636-
Move(zerox, zerox + nzero, elen - 2, char);
12637+
STRLEN nmove = elen - 2;
12638+
if (negative || plus) {
12639+
zerox++;
12640+
nmove--;
12641+
}
12642+
Move(zerox, zerox + nzero, nmove, char);
1263712643
memset(zerox, fill, nzero);
1263812644
}
1263912645
else {

t/op/sprintf2.t

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ if ($Config{nvsize} == 8 &&
262262
print "# no hexfloat tests\n";
263263
}
264264

265-
plan tests => 1408 + ($Q ? 0 : 12) + @hexfloat + 64;
265+
plan tests => 1408 + ($Q ? 0 : 12) + @hexfloat + 71;
266266

267267
use strict;
268268
use Config;
@@ -837,3 +837,12 @@ is(sprintf("%.1a", 0x1.1fp+0), "0x1.2p+0");
837837

838838
is(sprintf("%.2a", 0x1.fffp+0), "0x2.00p+0");
839839
is(sprintf("%.2a", 0xf.fffp+0), "0x2.00p+3");
840+
841+
# [rt.perl.org #128893]
842+
is(sprintf("%020a", 1.5), "0x0000000000001.8p+0");
843+
is(sprintf("%020a", -1.5), "-0x000000000001.8p+0", "[rt.perl.org #128893]");
844+
is(sprintf("%+020a", 1.5), "+0x000000000001.8p+0", "[rt.perl.org #128893]");
845+
is(sprintf("% 020a", 1.5), " 0x000000000001.8p+0", "[rt.perl.org #128893]");
846+
is(sprintf("%20a", -1.5), " -0x1.8p+0");
847+
is(sprintf("%+20a", 1.5), " +0x1.8p+0");
848+
is(sprintf("% 20a", 1.5), " 0x1.8p+0");

0 commit comments

Comments
 (0)