Porting some software to OpenBSD

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Porting some software to OpenBSD

Adam Steen
Hi All

I have a question about string (printf) formatting.

I have a variable

'uint64_t freq'

which is printed with

'log(DEBUG, "Solo5: clock_init(): freq=%lu\n", freq);'

but am getting the following error

'
error: format specifies type 'unsigned long' but the argument has type 'uint64_t' (aka 'unsigned long long') [-Werror,-Wformat]
        freq);
        ^~~~~~~~
1 error generated.
'

The easy fix is to change the format to '%llu', but this brakes FreeBSD and Linux. Am i missing something or should i be investigating the log implementation?


Cheers
Adam

Reply | Threaded
Open this post in threaded view
|

Re: Porting some software to OpenBSD

Nathan Hartman
On Sat, Jan 5, 2019 at 10:27 PM Adam Steen <[hidden email]> wrote:

> Hi All
>
> I have a question about string (printf) formatting.
>
> I have a variable
>
> 'uint64_t freq'
>
> which is printed with
>
> 'log(DEBUG, "Solo5: clock_init(): freq=%lu\n", freq);'
>
> but am getting the following error
>
> '
> error: format specifies type 'unsigned long' but the argument has type
> 'uint64_t' (aka 'unsigned long long') [-Werror,-Wformat]
>         freq);
>         ^~~~~~~~
> 1 error generated.
> '
>
> The easy fix is to change the format to '%llu', but this brakes FreeBSD
> and Linux. Am i missing something or should i be investigating the log
> implementation?
>
>
> Cheers
> Adam


There are often subtle differences like this between platforms.

One possibility is to define preprocessor macros that expand to the correct
printf format modifier for the platform. I've seen several implementations
over the years. One that comes to mind (only because I saw it recently) is
pstdint.h:

http://www.azillionmonkeys.com/qed/pstdint.h

(I don't know if that works correctly on OpenBSD. Also it defines a bunch
of other things; that may be helpful, or unhelpful!)

A more fully featured library that deals with platform differences is APR,
the Apache Portable Runtime. I think there are also such definitions there.
Reply | Threaded
Open this post in threaded view
|

Re: Porting some software to OpenBSD

Theo de Raadt-2
Nathan Hartman <[hidden email]> wrote:

> On Sat, Jan 5, 2019 at 10:27 PM Adam Steen <[hidden email]> wrote:
>
> > Hi All
> >
> > I have a question about string (printf) formatting.
> >
> > I have a variable
> >
> > 'uint64_t freq'
> >
> > which is printed with
> >
> > 'log(DEBUG, "Solo5: clock_init(): freq=%lu\n", freq);'
> >
> > but am getting the following error
> >
> > '
> > error: format specifies type 'unsigned long' but the argument has type
> > 'uint64_t' (aka 'unsigned long long') [-Werror,-Wformat]
> >         freq);
> >         ^~~~~~~~
> > 1 error generated.
> > '
> >
> > The easy fix is to change the format to '%llu', but this brakes FreeBSD
> > and Linux. Am i missing something or should i be investigating the log
> > implementation?
> >
> >
> > Cheers
> > Adam
>
>
> There are often subtle differences like this between platforms.

You mean errors.

Some of these errors are involved in what makes it so hard for them
to handle 64-bit time_t, and wait until you see the incredible scheme
they have planned for finally making 64-bit off_t the default.......

Reply | Threaded
Open this post in threaded view
|

Re: Porting some software to OpenBSD

Ted Unangst-6
In reply to this post by Adam Steen
Adam Steen wrote:

> 'log(DEBUG, "Solo5: clock_init(): freq=%lu\n", freq);'
>
> but am getting the following error
>
> '
> error: format specifies type 'unsigned long' but the argument has type 'uint64_t' (aka 'unsigned long long') [-Werror,-Wformat]
>         freq);
>         ^~~~~~~~
> 1 error generated.
> '
>
> The easy fix is to change the format to '%llu', but this brakes FreeBSD and Linux. Am i missing something or should i be investigating the log implementation?

The easy fix is log("%llu", (long long)freq) which should work everywhere.

Reply | Threaded
Open this post in threaded view
|

Re: Porting some software to OpenBSD

Philip Guenther-2
In reply to this post by Adam Steen
On Sat, Jan 5, 2019 at 7:25 PM Adam Steen <[hidden email]> wrote:

> I have a question about string (printf) formatting.
>
> I have a variable
>
> 'uint64_t freq'
>
> which is printed with
>
> 'log(DEBUG, "Solo5: clock_init(): freq=%lu\n", freq);'
>
> but am getting the following error
>
> '
> error: format specifies type 'unsigned long' but the argument has type
> 'uint64_t' (aka 'unsigned long long') [-Werror,-Wformat]
>         freq);
>         ^~~~~~~~
> 1 error generated.
> '
>
> The easy fix is to change the format to '%llu', but this brakes FreeBSD
> and Linux. Am i missing something or should i be investigating the log
> implementation?
>

Option 1)
    log(DEBUG, "Solo5: clock_init(): freq=%llu\n", (unsigned long
long)freq);

Option 2)
    #include <inttypes.h>
    log(DEBUG, "Solo5: clock_init(): freq=%"PRIu64"\n", freq);

Software native to OpenBSD uses option 1 when necessary.


Philip Guenther
Reply | Threaded
Open this post in threaded view
|

Re: Porting some software to OpenBSD

Andreas Kusalananda Kähäri-3
In reply to this post by Adam Steen
On Sun, Jan 06, 2019 at 03:22:50AM +0000, Adam Steen wrote:

> Hi All
>
> I have a question about string (printf) formatting.
>
> I have a variable
>
> 'uint64_t freq'
>
> which is printed with
>
> 'log(DEBUG, "Solo5: clock_init(): freq=%lu\n", freq);'
>
> but am getting the following error
>
> '
> error: format specifies type 'unsigned long' but the argument has type 'uint64_t' (aka 'unsigned long long') [-Werror,-Wformat]
>         freq);
>         ^~~~~~~~
> 1 error generated.
> '
>
> The easy fix is to change the format to '%llu', but this brakes FreeBSD and Linux. Am i missing something or should i be investigating the log implementation?
>
>
> Cheers
> Adam


If you want to print an int type from <stdint.h>, use the printf()
format defined for that type in <inttypes.h>:


    uint64_t freq;

    /* ... */

    printf("freq is %" PRIu64 " Hz\n", freq);


See e.g. https://en.cppreference.com/w/c/types/integer


Cheers,

--
Andreas Kusalananda Kähäri,
National Bioinformatics Infrastructure Sweden (NBIS),
Uppsala University, Sweden.