teknoraver

rw_verify_area

Dec 18th, 2025 (edited)
4,325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1. #define _GNU_SOURCE
  2.  
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <limits.h>
  8. #include <err.h>
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12.     // Just a MB less than LONG_MAX
  13.     size_t size = LONG_MAX - (1024 * 1024);
  14.     int in, pipefd[2], r;
  15.  
  16.     in = open(argv[1], O_RDONLY);
  17.     if (in < 0)
  18.         err(1, "open");
  19.  
  20.     r = pipe(pipefd);
  21.     if (r < 0)
  22.         err(1, "pipe");
  23.  
  24.     r = fork();
  25.     if (r < 0)
  26.         err(1, "fork");
  27.  
  28.     if (!r) {
  29.         // Discard data from pipe
  30.         char *buffer = malloc(1024 * 1024);
  31.  
  32.         while (read(pipefd[0], buffer, 1024 * 1024) > 0)
  33.             /* do nothing */ ;
  34.  
  35.         return 0;
  36.     }
  37.  
  38.     // splice data from file to pipe
  39.     for (ssize_t copied = 1; copied; ) {
  40.         copied = splice(in, NULL, pipefd[1], NULL, size, 0);
  41.         if (copied < 0) {
  42.             perror("splice");
  43.             // splice() will fail when file pos + size exceeds LONG_MAX
  44.             fprintf(stderr, "Failed to splice %zu bytes at offset %ld\n", size, lseek(in, 0, SEEK_CUR));
  45.             return 1;
  46.         }
  47.     }
  48.  
  49.     return 0;
  50. }
Advertisement