phoenix-rtos / phoenix-rtos-project

Sample project using Phoenix-RTOS
https://phoenix-rtos.com
BSD 3-Clause "New" or "Revised" License
43 stars 32 forks source link

Functions `getline` and `getdelim` does not add terminating NUL, when end-of-file is encountered #628

Closed PNieck closed 1 year ago

PNieck commented 1 year ago

Example

Suppose that we have such a file without new line character at the end of last line:

       ipsum
lorem

Then such a program:

int main()
{
    char path[] = "path/to/file";

    FILE *file;
    char *line = NULL;
    size_t lineLen = 0;

    file = fopen(path, "r");
    if (file == NULL) {
        fprintf(stderr, "Error on file \"%s\" opening.\n", path);
        return;
    }

    while (getline(&line, &lineLen, file) != -1) {
        printf("%s", line);
    }

    fclose(file);
    free(line);
}

Gives such an output, which is different than file content:

       ipsum
lorem  ipsum
gerard5 commented 1 year ago

@PNieck please provide more info about target on which you've observed the problem. From my side I use recent phoenix-rtos-project (https://github.com/phoenix-rtos/phoenix-rtos-project/commit/2dc876fed07118389c3ed506a1578ec68834af33) and it worksfine, for the example you provided, both results the same:

on host: 2023-03-03-130110_1017x263_scrot

Ia 32-generic: 2023-03-03-130159_1378x756_scrot

PNieck commented 1 year ago

@gerard5 Try without new line character after lorem

gerard5 commented 1 year ago

I confirm. I'll provide the fix in the pull-request.

gerard5 commented 1 year ago

@PNieck I added a quick-fix PR for that, please check if this solves the issue before merging.

PNieck commented 1 year ago

@gerard5 I have checked this PR and it seams to work