mirror of
https://github.com/servalproject/serval-dna.git
synced 2025-04-13 22:03:09 +00:00
Use ssize_t for read(2) and write(2) return value
Test for errors using == -1 not < 0, and cast to (size_t) subsequently Fixes some -Wsign-compare warnings
This commit is contained in:
parent
6254709a84
commit
4428a76379
@ -1051,7 +1051,7 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
uint8_t recv_payload[12];
|
||||
ssize_t len = mdp_recv(mdp_sockfd, &mdp_recv_header, recv_payload, sizeof(recv_payload));
|
||||
|
||||
if (len<0){
|
||||
if (len == -1){
|
||||
WHY_perror("mdp_recv");
|
||||
break;
|
||||
}
|
||||
@ -1069,7 +1069,7 @@ int app_mdp_ping(const struct cli_parsed *parsed, struct cli_context *context)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (len<sizeof(recv_payload)){
|
||||
if ((size_t)len < sizeof(recv_payload)){
|
||||
DEBUGF("Ignoring ping response as it is too short");
|
||||
continue;
|
||||
}
|
||||
|
@ -1593,7 +1593,7 @@ static void http_request_send_response(struct http_request *r)
|
||||
if (r->phase != PAUSE)
|
||||
http_request_set_idle_timeout(r);
|
||||
// If we wrote less than we tried, then go back to polling, otherwise keep generating content.
|
||||
if (written < (size_t) unsent)
|
||||
if ((size_t) written < (size_t) unsent)
|
||||
return;
|
||||
}
|
||||
if (r->debug_flag && *r->debug_flag)
|
||||
|
@ -119,8 +119,10 @@ ssize_t mdp_recv(int socket, struct mdp_header *header, uint8_t *payload, ssize_
|
||||
};
|
||||
|
||||
ssize_t len = recvmsg(socket, &hdr, 0);
|
||||
if (len<sizeof(struct mdp_header))
|
||||
return WHYF("Received message is too short (%d)", (int)len);
|
||||
if (len == -1)
|
||||
return WHYF_perror("recvmsg(%d,%p,0)", socket, &hdr);
|
||||
if ((size_t)len < sizeof(struct mdp_header))
|
||||
return WHYF("Received message is too short (%zu)", (size_t)len);
|
||||
addr.addrlen=hdr.msg_namelen;
|
||||
// double check that the incoming address matches the servald daemon
|
||||
if (cmp_sockaddr(&addr, &mdp_addr) != 0
|
||||
|
10
net.c
10
net.c
@ -81,7 +81,7 @@ ssize_t _write_all(int fd, const void *buf, size_t len, struct __sourceloc __whe
|
||||
if (written == -1)
|
||||
return WHYF_perror("write_all: write(%d,%p %s,%zu)",
|
||||
fd, buf, alloca_toprint(30, buf, len), len);
|
||||
if (written != len)
|
||||
if ((size_t)written != len)
|
||||
return WHYF_perror("write_all: write(%d,%p %s,%zu) returned %zd",
|
||||
fd, buf, alloca_toprint(30, buf, len), len, (size_t)written);
|
||||
return written;
|
||||
@ -96,7 +96,7 @@ ssize_t _writev_all(int fd, const struct iovec *iov, int iovcnt, struct __source
|
||||
ssize_t written = writev(fd, iov, iovcnt);
|
||||
if (written == -1)
|
||||
return WHYF_perror("writev_all: writev(%d,%s len=%zu)", fd, alloca_iovec(iov, iovcnt), len);
|
||||
if (written != len)
|
||||
if ((size_t)written != len)
|
||||
return WHYF_perror("writev_all: writev(%d,%s len=%zu) returned %zd", fd, alloca_iovec(iov, iovcnt), len, (size_t)written);
|
||||
return written;
|
||||
}
|
||||
@ -123,9 +123,9 @@ ssize_t _write_nonblock(int fd, const void *buf, size_t len, struct __sourceloc
|
||||
ssize_t _write_all_nonblock(int fd, const void *buf, size_t len, struct __sourceloc __whence)
|
||||
{
|
||||
ssize_t written = _write_nonblock(fd, buf, len, __whence);
|
||||
if (written != -1 && written != len)
|
||||
return WHYF("write_all_nonblock: write(%d,%p %s,%lu) returned %ld",
|
||||
fd, buf, alloca_toprint(30, buf, len), (unsigned long)len, (long)written);
|
||||
if (written != -1 && (size_t)written != len)
|
||||
return WHYF("write_all_nonblock: write(%d,%p %s,%zu) returned %zd",
|
||||
fd, buf, alloca_toprint(30, buf, len), len, (size_t)written);
|
||||
return written;
|
||||
}
|
||||
|
||||
|
6
os.c
6
os.c
@ -156,9 +156,9 @@ ssize_t read_symlink(const char *path, char *buf, size_t len)
|
||||
}
|
||||
ssize_t nr = readlink(path, buf, len);
|
||||
if (nr == -1)
|
||||
return WHYF_perror("readlink(%s)", path);
|
||||
if (nr >= len)
|
||||
return WHYF("buffer overrun from readlink(%s, len=%lu)", path, (unsigned long) len);
|
||||
return WHYF_perror("readlink(%s,%p,%zu)", path, buf, len);
|
||||
if ((size_t)nr >= len)
|
||||
return WHYF("buffer overrun from readlink(%s, len=%zu)", path, len);
|
||||
buf[nr] = '\0';
|
||||
return nr;
|
||||
}
|
||||
|
@ -1473,11 +1473,11 @@ static void mdp_poll2(struct sched_ent *alarm)
|
||||
struct iovec iov[]={
|
||||
{
|
||||
.iov_base = (void *)&header,
|
||||
.iov_len = sizeof(struct mdp_header)
|
||||
.iov_len = sizeof header
|
||||
},
|
||||
{
|
||||
.iov_base = (void *)payload,
|
||||
.iov_len = sizeof(payload)
|
||||
.iov_len = sizeof payload
|
||||
}
|
||||
};
|
||||
|
||||
@ -1489,17 +1489,17 @@ static void mdp_poll2(struct sched_ent *alarm)
|
||||
};
|
||||
|
||||
ssize_t len = recvmsg(alarm->poll.fd, &hdr, 0);
|
||||
if (len<0){
|
||||
WHY_perror("recvmsg");
|
||||
if (len == -1){
|
||||
WHYF_perror("recvmsg(%d,%p,0)", alarm->poll.fd, &hdr);
|
||||
return;
|
||||
}
|
||||
if (len<sizeof(struct mdp_header)){
|
||||
WHYF("Expected length %d, got %d from %s", (int)sizeof(struct mdp_header), (int)len, alloca_socket_address(&client));
|
||||
if ((size_t)len < sizeof header) {
|
||||
WHYF("Expected length %zu, got %zu from %s", sizeof header, len, alloca_socket_address(&client));
|
||||
return;
|
||||
}
|
||||
|
||||
client.addrlen = hdr.msg_namelen;
|
||||
size_t payload_len = len - sizeof(header);
|
||||
size_t payload_len = (size_t)(len - sizeof header);
|
||||
mdp_process_packet(&client, &header, payload, payload_len);
|
||||
}
|
||||
}
|
||||
@ -1521,7 +1521,7 @@ static void overlay_mdp_poll(struct sched_ent *alarm)
|
||||
&client, alloca_socket_address(&client)
|
||||
);
|
||||
|
||||
if (len > 0) {
|
||||
if ((size_t)len > 0) {
|
||||
if (client.addrlen <= sizeof(sa_family_t))
|
||||
WHYF("got client.addrlen=%d too short -- ignoring frame len=%zu", (int)client.addrlen, (size_t)len);
|
||||
else {
|
||||
|
@ -144,10 +144,8 @@ int rhizome_bundle_import_files(rhizome_manifest *m, const char *manifest_path,
|
||||
}
|
||||
|
||||
if (ret==0){
|
||||
ret = fread(buffer, 1, buffer_len, f);
|
||||
if (ret==buffer_len)
|
||||
ret=0;
|
||||
else
|
||||
ssize_t nread = fread(buffer, 1, buffer_len, f);
|
||||
if ((size_t)nread != buffer_len)
|
||||
ret=WHY_perror("Unable to read manifest contents");
|
||||
}
|
||||
|
||||
|
@ -221,17 +221,17 @@ static int write_data(struct rhizome_write *write_state, uint64_t file_offset, u
|
||||
WARNF("Writing file data out of order! [%"PRId64",%"PRId64"]", file_offset, write_state->written_offset);
|
||||
|
||||
if (write_state->blob_fd != -1) {
|
||||
int ofs=0;
|
||||
size_t ofs = 0;
|
||||
// keep trying until all of the data is written.
|
||||
if (lseek64(write_state->blob_fd, (off64_t) file_offset, SEEK_SET) == -1)
|
||||
return WHYF_perror("lseek64(%d,%"PRIu64",SEEK_SET)", write_state->blob_fd, file_offset);
|
||||
while(ofs < data_size){
|
||||
int r=write(write_state->blob_fd, buffer + ofs, data_size - ofs);
|
||||
if (r<0)
|
||||
while (ofs < data_size){
|
||||
ssize_t r = write(write_state->blob_fd, buffer + ofs, (size_t)(data_size - ofs));
|
||||
if (r == -1)
|
||||
return WHY_perror("write");
|
||||
if (config.debug.externalblobs)
|
||||
DEBUGF("Wrote %d bytes to fd %d", r, write_state->blob_fd);
|
||||
ofs+=r;
|
||||
DEBUGF("Wrote %zd bytes to fd %d", (size_t)r, write_state->blob_fd);
|
||||
ofs += (size_t)r;
|
||||
}
|
||||
}else{
|
||||
if (!write_state->sql_blob)
|
||||
@ -782,7 +782,7 @@ static ssize_t rhizome_read_retry(sqlite_retry_state *retry, struct rhizome_read
|
||||
// the length.
|
||||
size_t bytes_read = 0;
|
||||
if (buffer && bufsz && read_state->offset < read_state->length) {
|
||||
bytes_read = read_state->length - read_state->offset;
|
||||
bytes_read = (size_t)(read_state->length - read_state->offset);
|
||||
if (bytes_read > bufsz)
|
||||
bytes_read = bufsz;
|
||||
assert(bytes_read > 0);
|
||||
@ -821,7 +821,7 @@ ssize_t rhizome_read(struct rhizome_read *read_state, unsigned char *buffer, siz
|
||||
SHA512_Update(&read_state->sha512_context, buffer, bytes_read);
|
||||
read_state->hash_offset += bytes_read;
|
||||
// if we hash everything and the has doesn't match, we need to delete the payload
|
||||
if (read_state->hash_offset>=read_state->length){
|
||||
if (read_state->hash_offset >= read_state->length){
|
||||
rhizome_filehash_t hash_out;
|
||||
SHA512_Final(hash_out.binary, &read_state->sha512_context);
|
||||
SHA512_End(&read_state->sha512_context, NULL);
|
||||
|
Loading…
x
Reference in New Issue
Block a user