简体   繁体   中英

If statement returns wrong result

void test(char *buffer, int size)
{
    int length = strlen(buffer);
    for (int i = 0; i <= length; ++i)
    {
        if (buffer[i] == '"')
        {
            int _size = i + size;
            if (_size > length)
                continue;

            if (buffer[i + size] == '"')
            {

            }
        }
    }
}

This is how I read the file.

FILE *file = NULL;
size_t filesize = 0;
uint8_t *filebuffer = 0;
            
file = fopen("tokens.txt", "r");
if (file)
{
fseek(file, 0, SEEK_END);
                filesize = ftell(file);
                fseek(file, 0, SEEK_SET);

                filebuffer = calloc(filesize + 1, 1);
                if (filebuffer)
                {
                    fread(filebuffer, 1, filesize, file);

                    for (size_t i = 0; i < filesize; i++)
                    {
                        if (filebuffer[i] == 0)
                            filebuffer[i] = '.';
                    }

                    char array[filesize];
                    strncpy(array, filebuffer, filesize);
                    array[filesize] = '\0';
                    test(array, 59);
                }
            }

"array" is char array[filesize];, "filesize" is ftell(file); (the file is valid and not NULL) the content of the file is asd"12345678912345678912345678912345678911111231231231231231232"asdasdasdasdasdasdss

在此处输入图像描述

for some weird reason it reaches to the "continue;" when the statement is not true...

Edit: I tried printing the values in the block of the if statement and for some reason I receive ->
Size: 122
Length: 84

Someone have any idea on how to solve it?

array[filesize] = '\0'; // access outside of array boundaries 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM