Skip to content
  • Jiri Slaby's avatar
    ipc: msg, make msgrcv work with LONG_MIN · 542cde0e
    Jiri Slaby authored
    commit 99989835 upstream.
    
    When LONG_MIN is passed to msgrcv, one would expect to recieve any
    message.  But convert_mode does *msgtyp = -*msgtyp and -LONG_MIN is
    undefined.  In particular, with my gcc -LONG_MIN produces -LONG_MIN
    again.
    
    So handle this case properly by assigning LONG_MAX to *msgtyp if
    LONG_MIN was specified as msgtyp to msgrcv.
    
    This code:
      long msg[] = { 100, 200 };
      int m = msgget(IPC_PRIVATE, IPC_CREAT | 0644);
      msgsnd(m, &msg, sizeof(msg), 0);
      msgrcv(m, &msg, sizeof(msg), LONG_MIN, 0);
    
    produces currently nothing:
    
      msgget(IPC_PRIVATE, IPC_CREAT|0644)     = 65538
      msgsnd(65538, {100, "\310\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16, 0) = 0
      msgrcv(65538, ...
    
    Except a UBSAN warning:
    
      UBSAN: Undefined behaviour in ipc/msg.c:745:13
      negation of -9223372036854775808 cannot be represented in type 'long int':
    
    With the patch, I see what I expect:
    
      msgget(IPC_PRIVATE, IPC_CREAT|0644)     = 0
      msgsnd(0, {100, "\310\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16, 0) = 0
      msgrcv(0, {100, "\310\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16, -9223372036854775808, 0) = 16
    
    Link: http://lkml.kernel.org/r/20161024082633.10148-1-jslaby@suse.cz
    
    
    Signed-off-by: default avatarJiri Slaby <jslaby@suse.cz>
    Cc: Davidlohr Bueso <dave@stgolabs.net>
    Cc: Manfred Spraul <manfred@colorfullife.com>
    Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
    Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
    Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
    542cde0e