git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
| 1 | #include "../git-compat-util.h" |
| 2 | #include "../wrapper.h" |
| 3 | |
| 4 | ssize_t git_writev(int fd, const struct iovec *iov, int iovcnt) |
| 5 | { |
| 6 | size_t sum = 0; |
| 7 | |
| 8 | if (iovcnt <= 0) { |
| 9 | errno = EINVAL; |
| 10 | return -1; |
| 11 | } |
| 12 | |
| 13 | /* |
| 14 | * According to writev(3p), the syscall shall error with EINVAL in case |
| 15 | * the sum of `iov_len` overflows `ssize_t`. |
| 16 | */ |
| 17 | for (int i = 0; i < iovcnt; i++) { |
| 18 | if (iov[i].iov_len > maximum_signed_value_of_type(ssize_t) || |
| 19 | unsigned_add_overflows(iov[i].iov_len, sum) || |
| 20 | iov[i].iov_len + sum > maximum_signed_value_of_type(ssize_t)) { |
| 21 | errno = EINVAL; |
| 22 | return -1; |
| 23 | } |
| 24 | |
| 25 | sum += iov[i].iov_len; |
| 26 | } |
| 27 | |
| 28 | /* |
| 29 | * We only ever write the first non-empty vector so that we can |
| 30 | * guarantee the call to be non-interleaving as guaranteed by POSIX. |
| 31 | * This works just fine as callers have to loop around writev anyway. |
| 32 | */ |
| 33 | for (int i = 0; i < iovcnt; i++) { |
| 34 | if (!iov[i].iov_len) |
| 35 | continue; |
| 36 | return xwrite(fd, iov[i].iov_base, iov[i].iov_len); |
| 37 | } |
| 38 | |
| 39 | /* When all iovec members were zero we ought to return 0 according to POSIX. */ |
| 40 | return 0; |
| 41 | } |