diff options
Diffstat (limited to 'close_range.c')
-rw-r--r-- | close_range.c | 19 |
1 files changed, 12 insertions, 7 deletions
diff --git a/close_range.c b/close_range.c index 540eba0..29602c9 100644 --- a/close_range.c +++ b/close_range.c @@ -73,7 +73,8 @@ libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next) { int saved_errno; - *next = first; + if (next) + *next = first; if (first > last) { errno = EINVAL; @@ -103,10 +104,12 @@ libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next) qsort(fds, n, sizeof(*fds), uintpcmp); for (i = 0; i < n; i++) { if (close((int)fds[i]) && errno != EBADF) { - if (i + 1 < n) - *next = fds[i + 1]; - else - *next = fds[i] + (fds[i] < LIBSIMPLE_CLOSE_RANGE_MAX); + if (next) { + if (i + 1 < n) + *next = fds[i + 1]; + else + *next = fds[i] + (fds[i] < LIBSIMPLE_CLOSE_RANGE_MAX); + } free(fds); return -1; } @@ -118,13 +121,15 @@ libsimple_close_range(unsigned int first, unsigned int last, unsigned int *next) fallback: do { if (close((int)first) && errno != EBADF) { - *next = first + (first < LIBSIMPLE_CLOSE_RANGE_MAX); + if (next) + *next = first + (first < LIBSIMPLE_CLOSE_RANGE_MAX); return -1; } } while (first++ < last); out: - *next = last + (last < LIBSIMPLE_CLOSE_RANGE_MAX); + (next) + *next = last + (last < LIBSIMPLE_CLOSE_RANGE_MAX); errno = saved_errno; return 0; } |