From da9bfc27b1fbaf1b3557d213ea46bd8172491c90 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Mon, 17 Feb 2025 06:43:39 +0800 Subject: [PATCH] git_hooks_client: Ensure stdin is a pipe --- git_hooks_client/git_hooks_client.c | 23 ++++++++++++++++++++++- diff --git a/git_hooks_client/git_hooks_client.c b/git_hooks_client/git_hooks_client.c index ef6f3c86b089e6ea8210262a11f3f41dbabbfa63..f402c26dac7622ba8c286e76eae6f67f9ec004df 100644 --- a/git_hooks_client/git_hooks_client.c +++ b/git_hooks_client/git_hooks_client.c @@ -3,6 +3,7 @@ #include #include #include #include +#include #include #include @@ -31,8 +32,28 @@ close(sock); return EXIT_FAILURE; } + struct stat stdin_stat; + if (fstat(STDIN_FILENO, &stdin_stat) == -1) { + perror("fstat"); + close(sock); + return EXIT_FAILURE; + } + + if (!S_ISFIFO(stdin_stat.st_mode)) { + dprintf(STDERR_FILENO, "fatal: stdin must be a pipe\n"); + close(sock); + return EXIT_FAILURE; + } + + int pipe_size = fcntl(STDIN_FILENO, F_GETPIPE_SZ); + if (pipe_size == -1) { + perror("fcntl"); + close(sock); + return EXIT_FAILURE; + } + ssize_t bytes_spliced; - while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, 1, SPLICE_F_MORE)) > 0) { + while ((bytes_spliced = splice(STDIN_FILENO, NULL, sock, NULL, pipe_size, SPLICE_F_MORE)) > 0) { } if (bytes_spliced == -1) { -- 2.48.1