Commit 5cf646a7 authored by jan.koester's avatar jan.koester
Browse files

stdio: setvbuf(_IONBF) makes reads unbuffered

setvbuf() was a no-op, so a FILE read ahead BUFSIZ bytes even when the
caller asked for no buffering. git reads its remote helper's one-line
reply through such a FILE and the rest from the fd: with protocol v2
over https the capabilities vanished into the FILE's buffer and git
hung. FILE gets an appended __nbf field (FILE objects are only created
by the libc); invalid modes now fail with EINVAL.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 5ada85d7
Loading
Loading
Loading
Loading
+3 −2
Changes for src/linux/arch/powerpc/include/bits/alltypes.h: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -106,10 +106,11 @@ typedef unsigned useconds_t;
 * consumed). FILE objects are only ever created by the libc, so fields
 * appended here don't affect programs, which only hold FILE pointers.
 * __cookie: the fopencookie() functions and cookie of a stream that isn't
 * an fd (null for every other stream; __x is -1 then). */
 * an fd (null for every other stream; __x is -1 then). __nbf:
 * setvbuf(_IONBF) -- reads take one byte at a time, never more than asked. */
struct _IO_FILE { int __x; int __error; int __eof; int __ungot;
                  unsigned char* __rbuf; int __rpos; int __rlen;
                  void* __cookie; };
                  void* __cookie; int __nbf; };
typedef struct _IO_FILE FILE;

typedef __builtin_va_list va_list;
+3 −2
Changes for src/linux/arch/x86_64/include/bits/alltypes.h: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -94,10 +94,11 @@ typedef unsigned useconds_t;
 * consumed). FILE objects are only ever created by the libc, so fields
 * appended here don't affect programs, which only hold FILE pointers.
 * __cookie: the fopencookie() functions and cookie of a stream that isn't
 * an fd (null for every other stream; __x is -1 then). */
 * an fd (null for every other stream; __x is -1 then). __nbf:
 * setvbuf(_IONBF) -- reads take one byte at a time, never more than asked. */
struct _IO_FILE { int __x; int __error; int __eof; int __ungot;
                  unsigned char* __rbuf; int __rpos; int __rlen;
                  void* __cookie; };
                  void* __cookie; int __nbf; };
typedef struct _IO_FILE FILE;

typedef __builtin_va_list va_list;
+13 −3
Changes for src/linux/stdio.cpp: 13 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -243,7 +243,11 @@ extern "C" {
        }
        stream->__rpos = stream->__rlen = 0;
        for (;;) {
            ssize_t n = __io_read(stream, stream->__rbuf, BUFSIZ);
            /* setvbuf(_IONBF): never read ahead -- git reads one reply line
             * from its remote helper's pipe through such a FILE, then the
             * rest from the fd itself (protocol v2 over https hung when the
             * buffer had swallowed it) */
            ssize_t n = __io_read(stream, stream->__rbuf, stream->__nbf ? 1 : BUFSIZ);
            if (n < 0) { if (errno == EINTR) continue; stream->__error = 1; return -1; }
            if (n == 0) { stream->__eof = 1; return 0; }
            stream->__rlen = (int)n;
@@ -423,8 +427,14 @@ extern "C" {
        return fwrite(s, 1, len, stream) == len ? 0 : -1;
    }

    /* No-op: writes are unbuffered and the read buffer is internal. */
    int setvbuf(FILE*, char*, int, size_t) {
    /* Writes are unbuffered anyway; _IONBF stops reads from reading ahead
     * (see __fill_read_buffer()). A caller's buffer is not used. */
    int setvbuf(FILE* stream, char*, int mode, size_t) {
        if (!stream || (mode != _IOFBF && mode != _IOLBF && mode != _IONBF)) {
            errno = EINVAL;
            return -1;
        }
        stream->__nbf = (mode == _IONBF);
        return 0;
    }

+16 −0
Changes for src/test/e2e/stdio_test.cpp: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -161,6 +161,22 @@ int main() {
    CHECK(fwrite("q", 1, 1, cf) == 1, "fwrite() without a write function is discarded");
    CHECK(fclose(cf) == 0, "fclose() without a close function");

    /* setvbuf(_IONBF): a line read through the FILE takes no more from
     * the pipe than that line (git's remote-helper handshake) */
    int pfd[2];
    CHECK(pipe(pfd) == 0, "pipe()");
    CHECK(write(pfd[1], "ok\nrest", 7) == 7, "write() to the pipe");
    close(pfd[1]);
    FILE* pf = fdopen(dup(pfd[0]), "r");
    CHECK(pf != nullptr, "fdopen() of a pipe");
    CHECK(setvbuf(pf, nullptr, _IONBF, 0) == 0, "setvbuf(_IONBF)");
    CHECK(setvbuf(pf, nullptr, 42, 0) == -1, "setvbuf() rejects a bad mode");
    CHECK(fgets(line, sizeof line, pf) != nullptr && strcmp(line, "ok\n") == 0, "fgets() from an unbuffered pipe");
    fclose(pf);
    char rest[8] = {};
    CHECK(read(pfd[0], rest, sizeof rest) == 4 && memcmp(rest, "rest", 4) == 0, "unbuffered FILE left the rest in the pipe");
    close(pfd[0]);

    puts("stdio: all checks passed");
    return 0;
}