Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

[XS] : "snprintf" portability options

by syphilis (Archbishop)
on Sep 15, 2024 at 09:46 UTC ( [id://11161736]=perlquestion: print w/replies, xml ) Need Help??

syphilis has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

In Cpanel-JSON-XS-4.38, the XS.xs file contains a few lines like:
snprintf (enc->cur, IVUV_MAXCHARS, "%" UVuf, uv)
which is fine on *nix systems but on Windows builds of perl, whenever IVSIZE is 8, it warns:
XS.xs:2449:66: warning: format '%u' expects argument of type 'unsigned + int', but argument 4 has type 'long long unsigned int' [-Wformat=]
I can fix that for those Windows perls by replacing the occurrence of "%" with "%llu" but I doubt the portability of that fix. (I guess I could also add separate renditions for #if defined(WIN32) && IVSIZE == 8 .... yuk.)

What's the best portable way of dealing with this ? ... and where are the options for this formatting style documented ?
I can never remember where it that documentation is, and I can never find it when I need it :-(

Cheers,
Rob

Replies are listed 'Best First'.
Re: [XS] : "snprintf" portability options
by tonyc (Friar) on Sep 15, 2024 at 22:32 UTC

    I suspect you're running into gcc -Wformat not supporting the Windows specific "%I64u" format.

    This is why win32/GNNUmakefile adds -Wno-format after -Wall and -Wextra.

      This is why win32/GNNUmakefile adds -Wno-format after -Wall and -Wextra

      And the Cpanel-JSON-XS-4.38 Makefile.PL then oh-so-thoughtfully re-introduces the issue by appending "-Wall -Wextra -W" .... I'll submit a PR for that Makefile.PL, along with a PR to XS.xs that allows Cpanel::JSON::XS to build on Windows quadmath perls (and also silences some other warnings).

      Thanks tonyc - I remember that issue, now.
      At one stage yesterday it crossed my mind that I don't usually encounter [-Wformat=] warnings ... but I still failed to make the connection :-(

      Cheers,
      Rob

        oh-so-thoughtfully re-introduces the issue

        No it doesn't. The issue is there with or without the -Wformat. Your sarcasm is misplaced since the warning isn't spurious. The issue is that the wrong format is being used. This is a real error that needs fixing.

Re: [XS] : "snprintf" portability options
by etj (Priest) on Sep 15, 2024 at 11:12 UTC
    Surely the UVuf is entirely supposed to give the right format to match the type of UV. If it's not doing so, that's a bug in Perl, and should be fixed there?
      Surely the UVuf is entirely supposed to give the right format to match the type of UV.

      Sounds reasonable and I can't re-create the warnings in an Inline::C script - there might be more to this than I initially realized.
      Here's a bit more detail regarding one of those warnings:
      XS.xs: In function 'encode_sv': XS.xs:2449:66: warning: format '%u' expects argument of type 'unsigned + int', but argument 4 has type 'long long unsigned int' [-Wformat=] 2449 | enc->cur += snprintf (enc->cur, IVUV_MAXCHAR +S, "%" UVuf, UV_MAX); | + ^~~
      (It's getting late over here.)

      Cheers,
      Rob
        I hear a lot about new versions of gcc making it a fatal error to declare "void main", which tends to break short test programs during configure scripts and results in tests coming out false when they should return true. They can be hard to catch if a test is false on a majority of systems anyway. Maybe perl is affected?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11161736]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-10-09 01:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The PerlMonks site front end has:





    Results (44 votes). Check out past polls.

    Notices?
    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.