Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Commit

Permalink
error reporting, fix case where /proc/sys/fs/pipe-max-size is not rea…
Browse files Browse the repository at this point in the history
…dable
  • Loading branch information
gltile-two-electric-boogaloo committed May 20, 2022
1 parent dc62242 commit 80b1d38
Showing 1 changed file with 28 additions and 7 deletions.
35 changes: 28 additions & 7 deletions uwurandom.c
Original file line number Diff line number Diff line change
Expand Up @@ -1574,14 +1574,16 @@ int main() {
uwu_state *data = malloc(len);

if (data == NULL) {
return -ENOMEM;
fprintf(stderr, "error: out of memory");
return ENOMEM;
}

char *rng_buf = malloc(RAND_SIZE);

if (rng_buf == NULL) {
fprintf(stderr, "error: out of memory");
free(data);
return -ENOMEM;
return ENOMEM;
}

data->prev_op = -1;
Expand All @@ -1593,20 +1595,39 @@ int main() {

FILE *fd = fopen("/proc/sys/fs/pipe-max-size", "r");

if (fd == NULL) {
return errno;
}

int pipe_max_size;

fscanf(fd, "%d", &pipe_max_size);
if (fd == NULL) {
// in case /proc/sys/fs/pipe-max-size is not readable (like in Android)
fprintf(stderr, "warning: cannot read /proc/sys/fs/pipe-max-size, setting pipe size to 8192");
pipe_max_size = 8192;
} else {
fscanf(fd, "%d", &pipe_max_size);
}

fcntl(1, F_SETPIPE_SZ, pipe_max_size);

size_t uwu_buf_len = (size_t) pipe_max_size;
char *uwu_buf = malloc(uwu_buf_len);

if (uwu_buf == NULL) {
fprintf(stderr, "error: out of memory");
free(data);
free(rng_buf);
return ENOMEM;
}

struct iovec *vec;
vec = malloc(sizeof(struct iovec));

if (vec == NULL) {
fprintf(stderr, "error: out of memory");
free(uwu_buf);
free(data);
free(rng_buf);
return ENOMEM;
}

vec->iov_base = uwu_buf;
vec->iov_len = uwu_buf_len;

Expand Down

0 comments on commit 80b1d38

Please sign in to comment.