Skip to content

Commit

Permalink
Do not perform void pointer arithmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Aug 3, 2021
1 parent c2ff350 commit 218236c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
14 changes: 7 additions & 7 deletions src/cache.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cache.c
* Copyright (c) 2014-2018 Arkadiusz Bokowy
* Copyright (c) 2014-2021 Arkadiusz Bokowy
*
* This file is a part of cmusfm.
*
Expand Down Expand Up @@ -44,14 +44,14 @@ static size_t get_cache_record_size(const struct cmusfm_cache_record *record) {
* structure - segmentation-fault-safe checksum. */
static uint8_t get_cache_record_checksum1(const struct cmusfm_cache_record *record) {
return make_data_hash((unsigned char *)&record->timestamp,
sizeof(*record) - ((void *)&record->timestamp - (void *)record));
sizeof(*record) - ((char *)&record->timestamp - (char *)record));
}

/* Return the data checksum of the given cache record structure. If the
* overall record length is compromised, we might end up dead... */
static uint8_t get_cache_record_checksum2(const struct cmusfm_cache_record *record) {
return make_data_hash((unsigned char *)&record->timestamp,
get_cache_record_size(record) - ((void *)&record->timestamp - (void *)record));
get_cache_record_size(record) - ((char *)&record->timestamp - (char *)record));
}

/* Copy scrobbler track info into the cache record structure. Returned
Expand Down Expand Up @@ -167,7 +167,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) {
record = (struct cmusfm_cache_record *)rd_buff;

/* iterate while there is no enough data for full cache record header */
while ((void *)record - (void *)rd_buff + sizeof(*record) <= rd_len) {
while (((char *)record - rd_buff) + sizeof(*record) <= rd_len) {

/* convert "universal" endianness to the host one */
record->signature = ntohs(record->signature);
Expand All @@ -192,7 +192,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) {
debug("Record size: %ld", record_size);

/* break if current record is truncated */
if ((void *)record - (void *)rd_buff + record_size > rd_len)
if (((char *)record - rd_buff) + record_size > rd_len)
break;

/* check for second-stage data integration */
Expand Down Expand Up @@ -240,10 +240,10 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) {
record = (struct cmusfm_cache_record *)((char *)record + record_size);
}

if ((unsigned)((void *)record - (void *)rd_buff) != rd_len)
if ((size_t)((char *)record - rd_buff) != rd_len)
/* seek to the beginning of the current record, because it is
* truncated, so we have to read it one more time */
fseek(f, (void *)record - (void *)rd_buff - rd_len, SEEK_CUR);
fseek(f, ((char *)record - rd_buff) - rd_len, SEEK_CUR);
}

return_failure:
Expand Down
6 changes: 3 additions & 3 deletions src/server.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* server.c
* Copyright (c) 2010-2018 Arkadiusz Bokowy
* Copyright (c) 2010-2021 Arkadiusz Bokowy
*
* This file is a part of cmusfm.
*
Expand Down Expand Up @@ -81,14 +81,14 @@ static char *get_record_location(const struct cmusfm_data_record *r) {
/* Return the checksum for the length-invariant part of the record. */
static uint8_t make_record_checksum1(const struct cmusfm_data_record *r) {
return make_data_hash((unsigned char *)&r->status,
sizeof(*r) - ((void *)&r->status - (void *)r));
sizeof(*r) - ((char *)&r->status - (char *)r));
}

/* Return the data checksum of the given record structure. This checksum
* does not include status field, so it can be used for data comparison. */
static uint8_t make_record_checksum2(const struct cmusfm_data_record *r) {
return make_data_hash((unsigned char *)&r->disc_number,
sizeof(*r) - ((void *)&r->disc_number - (void *)r) +
sizeof(*r) - ((char *)&r->disc_number - (char *)r) +
r->off_location + strlen(get_record_location(r)));
}

Expand Down

0 comments on commit 218236c

Please sign in to comment.