Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: gcc: Operation on 'xxx' may be undefined

  1. #1
    Join Date
    Jan 2009
    Location
    Switzerland
    Posts
    1,556

    Default gcc: Operation on 'xxx' may be undefined

    This is a classic situation (example taken from Michel Xhaards jpegenc library):

    Code:
      for (i = rows; i > 0; i--)
        {
          for (j = cols; j > 0; j--)
            *Y1_Ptr++ = *input_ptr++ - 128;
          for (j = 8 - cols; j > 0; j--)
            *Y1_Ptr++ = *(Y1_Ptr - 1);
          input_ptr += incr;
        }
    Look at line 6 of this code snippet. It throws a warning like this:

    Code:
    encoder.c:392: warning: operation on 'Y1_Ptr' may be undefined
    The warning is justified because things can go wrong. It is entirely up to the compiler what the value of Y1_Ptr would be in the right part of the line. It is undefined according to the standard. Only the original coder knows what the code should do in his opinion.

    Code like this can not be packaged. I have to patch it. Would you think that this fix meets the original intentions:

    Code:
    *Y1_Ptr = *(Y1_Ptr - 1); Y1_Ptr++;
    Any comments would be helpful.
    Technology is 'stuff that doesn't work yet.' -- Bran Ferren

  2. #2
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    15,701

    Default re: gcc: Operation on 'xxx' may be undefined

    Would you mind telling what language this is about. Preferable in the thread title. This will attrackt the gurus of that language (better for you) and in the same time not waste the time of those having no aqaintance with that particualar language.
    Henk van Velden

  3. #3
    Join Date
    Jan 2009
    Location
    Switzerland
    Posts
    1,556

    Default re: gcc: Operation on 'xxx' may be undefined

    Would you mind telling what language this is about.
    I forgot the obvious: It is C, compiled with gcc.
    Technology is 'stuff that doesn't work yet.' -- Bran Ferren

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    15,701

    Default re: gcc: Operation on 'xxx' may be undefined

    Shalll I change the title to:
    Operation on 'xxx' may be undefined in C
    Henk van Velden

  5. #5
    Join Date
    Jan 2009
    Location
    Switzerland
    Posts
    1,556

    Default re: gcc: Operation on 'xxx' may be undefined

    Shall I change the title to:
    Operation on 'xxx' may be undefined in C
    Yes, please; or perhaps make it:

    gcc: Operation on 'xxx' may be undefined

    as this is a typical gcc warning. I don't know what other compilers would do.
    Technology is 'stuff that doesn't work yet.' -- Bran Ferren

  6. #6
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    4,693

    Default re: gcc: Operation on 'xxx' may be undefined

    vodoo wrote:

    > Code like this can not be packaged. I have to patch it. Would you think
    > that this fix meets the original intentions:
    >
    >
    > Code:
    > --------------------
    > *Y1_Ptr = *(Y1_Ptr - 1); Y1_Ptr++;
    > --------------------
    >

    Maybe I am blind but how does this fix it?

    If Y1_Ptr is undefined (the situation the warning is about) then

    Y1_Ptr - 1 is also undefined

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    15,701

    Default Re: gcc: Operation on 'xxx' may be undefined

    Quote Originally Posted by vodoo View Post
    Yes, please; or perhaps make it:

    gcc: Operation on 'xxx' may be undefined

    as this is a typical gcc warning. I don't know what other compilers would do.
    Done ............
    Henk van Velden

  8. #8
    Join Date
    Jan 2009
    Location
    Switzerland
    Posts
    1,556

    Default Re: Operation on 'xxx' may be undefined

    It's not the kind of 'undefined' you have in mind in the sense of not initialized. Look at line 4 of the code in post#1.

    "Undefined" here means that the compiler may (or may not -- this is undefined) increment Y1_Ptr before evaluation of the right side of the assignment.

    My fix avoids the compiler warning and forces a defined behaviour. The first statement is evaluated and then, in the second statement the pointer is increased. If the compiler did exactly this with the flaky code, then my fix is valid. Otherwise not.

    The reason why I'm insisting here is that I am building a package and the code is for a type of webcam (gspcav1) which I don't own. So I can't test it and would like to stay on the safe side, to avoid injecting broken packages into the community.
    Technology is 'stuff that doesn't work yet.' -- Bran Ferren

  9. #9
    Join Date
    Jan 2009
    Location
    Switzerland
    Posts
    1,556

    Default Re: gcc: Operation on 'xxx' may be undefined

    Done ............
    @hcvv: thank you; looks much better now.
    Technology is 'stuff that doesn't work yet.' -- Bran Ferren

  10. #10
    Join Date
    Feb 2010
    Location
    Germany
    Posts
    4,693

    Default Re: gcc: Operation on 'xxx' may be undefined

    Sorry I misread your post and the exact warning.

    It is C not C++ so I am the wrong person to comment on it at all (otherwise
    my opinion would be not to use unsafe pointer arithmetic at all but safe
    iterators). I think the last time I used pointer arithmetic was about 14-15
    years ago.

    Why not ask the original author of the code, is he/she no longer available.
    Because then the issue could be solved upstream by the author and is
    available to everyone using it.


Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •