Skip to content

Off by 1 Error in file.c #35

@jdunlap

Description

@jdunlap

In file.c, you write file_get_descriptor as

static struct file_descriptor* file_get_descriptor(int fd)
{
    if (fd <= 0 || fd >= PEACHOS_MAX_FILE_DESCRIPTORS)
    {
        return 0;
    }
 
    // Descriptors start at 1
    int index = fd - 1;
    return file_descriptors[index];
}

However, it should be

static struct file_descriptor* file_get_descriptor(int fd)
{
    if (fd <= 0 || fd > PEACHOS_MAX_FILE_DESCRIPTORS)
    {
        return 0;
    }
 
    // Descriptors start at 1
    int index = fd - 1;
    return file_descriptors[index];
}

because PEACHOS_MAX_FILE_DESCRIPTORS is an allowable file descriptor because of adding 1 to all of them. You have, in essence, made it so that when you reach the maximum, you can allocate the last one, but not access it with this function.

In the Q&A you had asked me to check the final commit because you thought the bug had been fixed, but this issue is still in the current commit of the repo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions