[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#897373: libc6: feof(file) always false when forking after read



Package: libc6
Version: 2.27-3
Severity: normal

Dear Maintainer,

We found a weird bug  working on a small shell interpreter  : feof(file)
was always 0 while we read the file entirely.

There is a minimal code showing the bug. If you run it with the name of
a file containing at least two chars as the first argument it will loop
indefinitely while it should stop at the end of the file and write
exactly one line per char read.

We manage to reproduce the issue on the libc6 2.27 (my computer) on an
Ubuntu 16.04.2 and a Mint 18.1 (libc6 2.23).

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>

int do_fork() {
    pid_t pid;

    switch (pid = fork()) {
        case -1:
            fprintf(stderr, "Fork failed\n");
            return -1;
        case 0:
            exit(-1);
        default:
            wait(NULL);
            return 0;
    }
}


int main(int argc, char *argv[]) {
    char c;
    int done=0;
    FILE *fichier;

    if (argc > 1) {
        fichier = fopen(argv[1], "r");
        if (fichier == NULL) {
            perror("Can't open iput file");
            exit(2);
        }
    } else {
        exit(1);
    }

    while(!done){
        fscanf(fichier, "%c", &c);
        done = feof(fichier);
        if (!done) {
            printf("Correct read\n");
            do_fork();
        }
    }

    return 0;
}

-- System Information:
Debian Release: buster/sid
  APT prefers testing
  APT policy: (600, 'testing'), (100, 'unstable'), (50, 'stable')
Architecture: amd64 (x86_64)

Kernel: Linux 4.15.0-3-amd64 (SMP w/4 CPU cores)
Locale: LANG=en_US.utf8, LC_CTYPE=en_US.utf8 (charmap=UTF-8), LANGUAGE=en_US:en (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)
LSM: AppArmor: enabled

Versions of packages libc6 depends on:
ii  libgcc1  1:8-20180414-1

libc6 recommends no packages.

Versions of packages libc6 suggests:
ii  debconf [debconf-2.0]  1.5.66
pn  glibc-doc              <none>
ii  libc-l10n              2.27-3
ii  locales                2.27-3

-- debconf information:
  glibc/restart-services:
  glibc/upgrade: true
  glibc/kernel-not-supported:
* glibc/disable-screensaver:
  glibc/restart-failed:
  glibc/kernel-too-old:
* libraries/restart-without-asking: true

Best regards,

David


Reply to: