From 3b35c447e1a90da7bec41226bf0b9153d100c7bd Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 16 Apr 2020 00:13:13 +0200 Subject: no undefined behavior by adding/substracting values != 0 from null ptr Change-Id: Id2dc21e884991b3cca9f617aece12ac35959c1b2 --- Transceiver52M/device/ipc/shm.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Transceiver52M/device/ipc/shm.c b/Transceiver52M/device/ipc/shm.c index 76f6151..6b16f8d 100644 --- a/Transceiver52M/device/ipc/shm.c +++ b/Transceiver52M/device/ipc/shm.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "shm.h" @@ -66,11 +67,11 @@ unsigned int ipc_shm_encode_smpl_buf(struct ipc_shm_raw_region *root_raw, struct unsigned int ipc_shm_encode_stream(struct ipc_shm_raw_region *root_raw, struct ipc_shm_raw_stream *stream_raw, uint32_t num_buffers, uint32_t buffer_size) { - uint8_t* start = (uint8_t*)stream_raw; unsigned int i; + ptrdiff_t start = (ptrdiff_t)stream_raw; unsigned int offset = sizeof(struct ipc_shm_raw_stream) + sizeof(uint32_t)*num_buffers; - fprintf(stderr, "encode: stream at offset %lu\n", (start - (uint8_t*)root_raw)); + fprintf(stderr, "encode: stream at offset %lu\n", (start - (ptrdiff_t)root_raw)); if (root_raw) { stream_raw->num_buffers = num_buffers; @@ -80,7 +81,7 @@ unsigned int ipc_shm_encode_stream(struct ipc_shm_raw_region *root_raw, struct i } for (i = 0; i < num_buffers; i++) { if (root_raw) - stream_raw->buffer_offset[i] = (start + offset - (uint8_t*)root_raw); + stream_raw->buffer_offset[i] = (start + offset - (ptrdiff_t)root_raw); offset += ipc_shm_encode_smpl_buf(root_raw, (struct ipc_shm_raw_smpl_buf *)(start + offset), buffer_size); } return offset; @@ -103,16 +104,16 @@ unsigned int ipc_shm_encode_channel(struct ipc_shm_raw_region *root_raw, struct /* if root_raw is NULL, then do a dry run, aka only calculate final offset */ unsigned int ipc_shm_encode_region(struct ipc_shm_raw_region *root_raw, uint32_t num_chans, uint32_t num_buffers, uint32_t buffer_size) { - uint8_t* start = (uint8_t*)root_raw; unsigned i; + ptrdiff_t start = (ptrdiff_t)root_raw; unsigned int offset = sizeof(struct ipc_shm_raw_region) + sizeof(uint32_t)*num_chans; if (root_raw) root_raw->num_chans = num_chans; for (i = 0; i < num_chans; i++) { - uint32_t ofs = (start + offset - (uint8_t*)root_raw); + uint32_t ofs = (start + offset - (ptrdiff_t)root_raw); if (root_raw) - root_raw->chan_offset[i] = (start + offset - (uint8_t*)root_raw); + root_raw->chan_offset[i] = (start + offset - (ptrdiff_t)root_raw); fprintf(stderr, "encode: channel %d chan_offset[i]=%u\n", i, ofs); offset += ipc_shm_encode_channel(root_raw, (struct ipc_shm_raw_channel *)(start + offset), num_buffers, buffer_size); } -- cgit v1.2.3-59-g8ed1b