From e2b5953d997e0e95a536e79d5f76dda915816a05 Mon Sep 17 00:00:00 2001 From: Giacomo Tesio Date: Mon, 16 Jan 2017 23:53:05 +0100 Subject: [PATCH] libc: handle positive fd on "always-failing" creates (brk and dup) --- sys/src/lib/c/9sys/dup.c | 5 +++++ sys/src/lib/c/9sys/sbrk.c | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/sys/src/lib/c/9sys/dup.c b/sys/src/lib/c/9sys/dup.c index 6b69c56..0d42395 100644 --- a/sys/src/lib/c/9sys/dup.c +++ b/sys/src/lib/c/9sys/dup.c @@ -30,6 +30,11 @@ dup(int oldfd, int newfd) f = create("#d/new", -1, in.aslong); if(f == -1) return -1; + if(f >= 0){ + /* this should never happen */ + close(f); + return -1; + } out.aslong = ~f; return out.fd[1]; } diff --git a/sys/src/lib/c/9sys/sbrk.c b/sys/src/lib/c/9sys/sbrk.c index df8976d..b476f88 100644 --- a/sys/src/lib/c/9sys/sbrk.c +++ b/sys/src/lib/c/9sys/sbrk.c @@ -18,7 +18,18 @@ enum Round = 7 }; -#define brk_(p) ((uintptr_t)~create("#0/brk/set", -1, p)) +uintptr_t +brk_(uintptr_t p) +{ + long f; + f = create("#0/brk/set", -1, p); + if(f >= 0){ + /* this should never happen */ + close(f); + return -1; + } + return (uintptr_t)~f; +} int brk(void *p)