Okay, weird.

This is a discussion on Okay, weird. within the Linux Programming forums, part of the Platform Specific Boards category; I'm doing this: Code: if(read(mMemFd, buf, PageSize()) != 4096) throw ProcException(); It's throwing. Why? Because read() is returning 4097?! mMemFd ...

  1. #1
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158

    Okay, weird.

    I'm doing this:

    Code:
    if(read(mMemFd, buf, PageSize()) != 4096)
        throw ProcException();
    It's throwing. Why? Because read() is returning 4097?!

    mMemFd is an open file descriptor on /proc/XXX/mem. Obviously, read() doesn't normally do this or the whole system would be breaking.

    Kernel is Linux 2.6.20
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Did you check what PageSize() returns?

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Perspective View Post
    Did you check what PageSize() returns?
    PageSize() returns 4096. I committed the sin of not posting the original code. It's actually:

    Code:
    if(read(mMemFd, buf, PageSize()) != PageSize())
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    maybe check if errno is being set after the call? That's pretty odd though.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Perspective View Post
    maybe check if errno is being set after the call? That's pretty odd though.
    errno is 0, as expected. My only theory at this point is kernel bug, so I'm compiling a newer kernel right now...
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Okay. So here's what was happening. I was calling read(), and if it failed, throwing an exception. Apparently, something during exception throw was resetting errno to 0. Silly mistake. Checking errno immediately after the call to read() shows EINVAL.

    The man page said that this could be due to unaligned access. But the access is aligned. So something else must be causing it.

    Turns out, I was able to read() from regions of memory less than 2 GB, but not > 2 GB (in this case, the stack of another process). That's because I was calling lseek(), which takes an off_t, which is a signed type. So my address was sign-extending and becoming negative.

    Oddly lseek() does not report it as an error if you seek before the beginning of the file. I don't know if that's always true.

    Solution? Switch over to lseek64() which has enough room in its off64_t for values larger than 2 GB.

    I still think there is a bug somewhere, either the kernel or the C library. If errno was being set (and I proved it by making sure it was 0 beforehand), then read() should be returning -1. But it's not, it's returning some value which seems to be "random" yet somehow related to the parameters passed.

    If I had been checking for error with:

    Code:
    if(read(...) < 0)
    Then I would have completely missed this problem and probably would have wasted several more hours figuring it out.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird things with my linked list of queue
    By -EquinoX- in forum C Programming
    Replies: 3
    Last Post: 11-22-2008, 10:23 PM
  2. weird
    By kiz in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:16 AM
  3. Weird Characters With GetDlgItemText
    By execute in forum Windows Programming
    Replies: 4
    Last Post: 05-04-2006, 04:53 PM
  4. weird error
    By gandalf_bar in forum Linux Programming
    Replies: 2
    Last Post: 07-17-2005, 07:32 AM
  5. Getting weird characters in Strings
    By steve8820 in forum C Programming
    Replies: 3
    Last Post: 09-18-2001, 02:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21