为什么调用lseek后读取的文件总是返回0?

时间:2012-06-10 03:46:05

标签: c linux ubuntu

我无法理解为什么在lseek之后调用read会返回0个字节读取。

//A function to find the next note for a given userID;
//returns -1 if at the end of file is reached;
//otherwise, it returns the length of the found note.
int find_user_note(int fd, int user_uid) {
    int note_uid = -1;
    unsigned char byte;
    int length;

    while(note_uid != user_uid) { // Loop until a note for user_uid is found.
        if(read(fd, &note_uid, 4) != 4) // Read the uid data.
            return -1; // If 4 bytes aren't read, return end of file code.
        if(read(fd, &byte, 1) != 1) // Read the newline separator.
            return -1;

        byte = length = 0;
        while(byte != '\n') { // Figure out how many bytes to the end of line.
            if(read(fd, &byte, 1) != 1) // Read a single byte.
                return -1; // If byte isn't read, return end of file code.

            //printf("%x ", byte);
            length++;
        }
    }
    long cur_position = lseek(fd, length * -1, SEEK_CUR ); // Rewind file reading by length bytes.

    printf("cur_position: %i\n", cur_position);

    // this is debug
    byte = 0;
    int num_byte = read(fd, &byte, 1);

    printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
    return length;
}

当外部while循环存在时,变量长度值为34,并且上面的代码产生cur_position 5(因此在lseek函数返回后肯定至少有34个字节),但是从函数read返回的变量num_byte总是返回0即使还有更多的字节需要阅读。

有没有人知道num_byte总是返回0的原因?如果我的代码中有错误,我就不会看到它是什么。

仅供参考,上述代码在以下机器上运行

$ uname -srvpio
Linux 3.2.0-24-generic #39-Ubuntu SMP Mon May 21 16:52:17 UTC 2012 x86_64 x86_64 GNU/Linux

更新

  • 我上传完整代码here
  • 这是我尝试阅读的文件内容
$ sudo hexdump -C /var/notes
00000000  e8 03 00 00 0a 74 68 69  73 20 69 73 20 61 20 74  |.....this is a t|
00000010  65 73 74 20 6f 66 20 6d  75 6c 74 69 75 73 65 72  |est of multiuser|
00000020  20 6e 6f 74 65 73 0a                              | notes.|
00000027

$

3 个答案:

答案 0 :(得分:3)

如果length是一个小于off_t的无符号类型(例如,32位机器上的size_t),那么length*-1将是一个巨大的价值(大约4GB左右)。这可能是问题所在。将lseek的结果存储到long(再次,如果它是32位)将应用实现定义的转换,可能是截断,这会再次留下一个小值。

我看到您的计算机是64位的,但也许您正在运行32位用户空间?

在任何情况下,为什么不在strace下运行您的程序以查看它正在调用的系统?这几乎肯定会很快解决问题。

答案 1 :(得分:2)

我终于找到了问题!!!我必须放#include <unistd.h>才能使用正确的lseek()。但是我不确定为什么不包括unistd.h它可编译但导致意外行为。我认为如果不包含函数的原型,它甚至不应该可编译。

代码是由Jon Erickson用Hacking: The Art of Exploitation 2nd Edition编写的,我已经在书中证实,没有#include <unistd.h>

答案 2 :(得分:1)

  

初始变量长度设置为34时,上面的代码会   产生cur_position 5(所以肯定至少有34个字节   在lseek函数返回后)

情况不一定如此,因为人们可以在文件末尾之外寻找而不会出现任何错误。

请参阅以下lseek()手册页的摘录:

  

lseek()函数允许将文件偏移量设置为超出   文件的结尾(但这不会改变文件的大小)。

因此,人们很可能会收到一个值lseek()的格式,它仍然指向文件的末尾。所以read()从这个位置仍然会返回0(超出文件末尾)。


我也同意R ..,在使用正确的类型(使用的方法所使用的类型)时更加谨慎,这不是一个坏主意。


更新:您也可以注意包含所调用系统功能的所有标头。要检查此类强烈建议使用gcc选项-Wall来启用所有编译器警告,它们是免费的......; - )