necessary cast?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • j0mbolar

    #1

    necessary cast?

    I was looking over some code and came across this:

    new_time = mktime(&tm);

    if(new_time == (time_t)-1)
    goto err;


    is this cast necessary in a strictly conforming
    program?
  • Richard Bos

    #2
    Re: necessary cast?

    j0mbolar@engine er.com (j0mbolar) wrote:
    [color=blue]
    > I was looking over some code and came across this:
    >
    > new_time = mktime(&tm);
    >
    > if(new_time == (time_t)-1)
    > goto err;
    >
    > is this cast necessary in a strictly conforming
    > program?[/color]

    I think so. If time_t is a small type, the comparison (new_time == -1)
    will result in new_time being promoted to int, and then compared to -1;
    if time_t is also unsigned, this comparison will be false even if
    new_time is actually equal to (time_t)-1.
    For this to happen, time_t must be unsigned short, unsigned char, or a
    small C99 unsigned type, and the range of that type must be smaller than
    that of unsigned int. This is, of course, not very likely, but when
    you're after strict conformance, "unlikely but legal" is bad enough.

    Richard

    Comment

    • Thomas Stegen

      #3
      Re: necessary cast?

      j0mbolar wrote:[color=blue]
      > I was looking over some code and came across this:
      >
      > new_time = mktime(&tm);
      >
      > if(new_time == (time_t)-1)
      > goto err;
      >
      >
      > is this cast necessary in a strictly conforming
      > program?[/color]

      No cast can make this a strictly conforming program.
      The only guarantee given about time_t is that it is an
      arithmetic type. This means it can be a float or a double,
      signed or unsigned integer type. The above seems to make the
      assumption that time_t is an unsigned integer type. This
      is relying on implementation defined behaviour and so the
      program is not strictly conforming.

      --
      Thomas.

      Comment

      • Thomas Stegen

        #4
        Re: necessary cast?

        Richard Bos wrote:
        [color=blue]
        > j0mbolar@engine er.com (j0mbolar) wrote:
        >
        >[color=green]
        >>I was looking over some code and came across this:
        >>
        >>new_time = mktime(&tm);
        >>
        >>if(new_time == (time_t)-1)
        >> goto err;
        >>
        >>is this cast necessary in a strictly conforming
        >>program?[/color]
        >
        >
        > I think so.[/color]

        The above will not make the program strictly conforming as time_t
        is an arithmetic type. Which means it can be any integer type or any
        floating point type.

        So even though the above is never invalid C its behaviour does
        depend on the actual type of time_t.

        --
        Thomas.

        Comment

        • Richard Bos

          #5
          Re: necessary cast?

          Thomas Stegen <tstegen@cis.st rath.ac.uk> wrote:
          [color=blue]
          > Richard Bos wrote:
          >[color=green]
          > > j0mbolar@engine er.com (j0mbolar) wrote:
          > >[color=darkred]
          > >>I was looking over some code and came across this:
          > >>
          > >>new_time = mktime(&tm);
          > >>
          > >>if(new_time == (time_t)-1)
          > >> goto err;
          > >>
          > >>is this cast necessary in a strictly conforming
          > >>program?[/color]
          > >
          > > I think so.[/color]
          >
          > The above will not make the program strictly conforming as time_t
          > is an arithmetic type. Which means it can be any integer type or any
          > floating point type.[/color]

          Even so, (time_t)-1 must be the value returned by mktime() on error:

          # [#4] The mktime function returns the specified calendar time
          # encoded as a value of type time_t. If the calendar time
          # cannot be represented, the function returns the value
          # (time_t)-1. [From n869.txt, 7.23.2.3.]

          The example following this excerpt even uses that very comparison.

          Richard

          Comment

          • Thomas Stegen

            #6
            Re: necessary cast?

            Richard Bos wrote:[color=blue]
            >
            > Even so, (time_t)-1 must be the value returned by mktime() on error:
            >
            > # [#4] The mktime function returns the specified calendar time
            > # encoded as a value of type time_t. If the calendar time
            > # cannot be represented, the function returns the value
            > # (time_t)-1. [From n869.txt, 7.23.2.3.]
            >
            > The example following this excerpt even uses that very comparison.[/color]

            Good to know, cheers.

            --
            Thomas.

            Comment

            Working...