Thread: aliasing in nginx code?

  1. #1
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105

    aliasing in nginx code?

    I'm pondering a bit about the code I'm seeing in nginx...

    It's a code for checking if a string equals a sequence of characters:
    Code:
    #define ngx_str3_cmp(m, c0, c1, c2, c3)                         \
      *(uint32_t *) m == ((c3 << 24) | (c2 << 16) | (c1 << 8) | c0)
    The code is calling this macro, such that:
    - m is a u_char*
    -c0,c1,c2,c3 are chars


    so, to me it seems the code is casting u_char* to uint32_t*, and de-referencing it..? Is this legal C99, with respect to strict aliasing and such? I thought it wouldnt be legal, but my compiler doesen't compain

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The only issue I see here is the cast of a pointer from a narrow type (a char) to a wide type (long).
    Such code will draw a bus error on machines which do not permit unaligned data accesses.

    Which is generally fine on say an x86, where half the silicon on the chip is dedicated to fixing all the historic baggage of the 1970's, 1980's and 1990's.

    But on a slim and sleek RISC processor say, then your program is either dead, or there is a far far far more expensive trap into the OS to repair the damage, which will burn far more time than the largely imaginary saving over a strcmp call.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User Drogin's Avatar
    Join Date
    Oct 2005
    Location
    Norway
    Posts
    105
    Quote Originally Posted by Salem View Post
    The only issue I see here is the cast of a pointer from a narrow type (a char) to a wide type (long).
    Such code will draw a bus error on machines which do not permit unaligned data accesses.

    Which is generally fine on say an x86, where half the silicon on the chip is dedicated to fixing all the historic baggage of the 1970's, 1980's and 1990's.

    But on a slim and sleek RISC processor say, then your program is either dead, or there is a far far far more expensive trap into the OS to repair the damage, which will burn far more time than the largely imaginary saving over a strcmp call.
    Great answer
    I noticed that they do check if you're on x86_64 before defining this macro... so I guess it's not directly dangerous. But is it valid code according to the C standard? Do you know if the C standard explicitly forbids casting from a narrow pointer to a larger one?

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Drogin View Post
    Great answer
    I noticed that they do check if you're on x86_64 before defining this macro... so I guess it's not directly dangerous. But is it valid code according to the C standard? Do you know if the C standard explicitly forbids casting from a narrow pointer to a larger one?
    Quick search bring the following link The New C Standard: 6.3.2.3

    Looks like next items are relevant
    759 If the resulting pointer is not correctly aligned57) for the pointed-to type, the behavior is undefined.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strict aliasing
    By MOS-6581 in forum C Programming
    Replies: 4
    Last Post: 05-17-2014, 05:18 PM
  2. about aliasing
    By Kempelen in forum C Programming
    Replies: 1
    Last Post: 11-11-2009, 08:39 AM
  3. Anti aliasing
    By arjunajay in forum Windows Programming
    Replies: 7
    Last Post: 08-15-2006, 08:24 PM
  4. pointer aliasing
    By moi in forum C Programming
    Replies: 2
    Last Post: 02-21-2006, 11:08 AM
  5. anti aliasing
    By muttski in forum Game Programming
    Replies: 2
    Last Post: 03-16-2002, 06:55 AM