Skip to content

Commit 279949f

Browse files
committed
audio: audio_stream: Use uintptr_t for pointer comparisons in asserts
Switch the pointer bound-check assertions from intptr_t to uintptr_t casts. A signed comparison is incorrect for addresses crossing the 0x80000000 boundary and can silently invert the assert. Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
1 parent 4680f9e commit 279949f

2 files changed

Lines changed: 16 additions & 16 deletions

File tree

src/include/module/audio/audio_stream.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static inline int cir_buf_samples_without_wrap_s16(const void *ptr, const void *
167167
{
168168
int to_end = (const int16_t *)buf_end - (const int16_t *)ptr;
169169

170-
assert((intptr_t)buf_end >= (intptr_t)ptr);
170+
assert((uintptr_t)buf_end >= (uintptr_t)ptr);
171171
return to_end;
172172
}
173173

@@ -183,7 +183,7 @@ static inline int cir_buf_samples_without_wrap_s32(const void *ptr, const void *
183183
{
184184
int to_end = (const int32_t *)buf_end - (const int32_t *)ptr;
185185

186-
assert((intptr_t)buf_end >= (intptr_t)ptr);
186+
assert((uintptr_t)buf_end >= (uintptr_t)ptr);
187187
return to_end;
188188
}
189189

@@ -213,7 +213,7 @@ static inline void *cir_buf_wrap(const void *ptr, const void *buf_addr, const vo
213213
ptr = (const char *)buf_addr +
214214
((const char *)ptr - (const char *)buf_end);
215215

216-
assert((intptr_t)ptr <= (intptr_t)buf_end);
216+
assert((uintptr_t)ptr <= (uintptr_t)buf_end);
217217

218218
return (void *)ptr;
219219
}
@@ -232,7 +232,7 @@ static inline const void *source_cir_buf_wrap(const void *ptr, const void *buf_a
232232
ptr = (const char *)buf_addr +
233233
((const char *)ptr - (const char *)buf_end);
234234

235-
assert((intptr_t)ptr <= (intptr_t)buf_end);
235+
assert((uintptr_t)ptr <= (uintptr_t)buf_end);
236236

237237
return ptr;
238238
}
@@ -265,7 +265,7 @@ static inline const void *source_cir_buf_rewind_wrap(const void *ptr, const void
265265
if (ptr < buf_start)
266266
ptr = (const char *)buf_end - ((const char *)buf_start - (const char *)ptr);
267267

268-
assert((intptr_t)ptr >= (intptr_t)buf_start);
268+
assert((uintptr_t)ptr >= (uintptr_t)buf_start);
269269

270270
return ptr;
271271
}

src/include/sof/audio/audio_stream.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ static inline void *audio_stream_wrap(const struct audio_stream *buffer, void *p
405405
ptr = (char *)buffer->addr +
406406
((char *)ptr - (char *)buffer->end_addr);
407407

408-
assert((intptr_t)ptr <= (intptr_t)buffer->end_addr);
408+
assert((uintptr_t)ptr <= (uintptr_t)buffer->end_addr);
409409

410410
return ptr;
411411
}
@@ -422,7 +422,7 @@ static inline void *audio_stream_rewind_wrap(const struct audio_stream *buffer,
422422
if (ptr < buffer->addr)
423423
ptr = (char *)buffer->end_addr - ((char *)buffer->addr - (char *)ptr);
424424

425-
assert((intptr_t)ptr >= (intptr_t)buffer->addr);
425+
assert((uintptr_t)ptr >= (uintptr_t)buffer->addr);
426426

427427
return ptr;
428428
}
@@ -804,8 +804,8 @@ static inline void audio_stream_writeback(struct audio_stream *buffer, uint32_t
804804
static inline int
805805
audio_stream_bytes_without_wrap(const struct audio_stream *source, const void *ptr)
806806
{
807-
assert((intptr_t)source->end_addr >= (intptr_t)ptr);
808-
return (intptr_t)source->end_addr - (intptr_t)ptr;
807+
assert((uintptr_t)source->end_addr >= (uintptr_t)ptr);
808+
return (uintptr_t)source->end_addr - (uintptr_t)ptr;
809809
}
810810

811811
/**
@@ -819,8 +819,8 @@ audio_stream_bytes_without_wrap(const struct audio_stream *source, const void *p
819819
static inline int
820820
audio_stream_rewind_bytes_without_wrap(const struct audio_stream *source, const void *ptr)
821821
{
822-
assert((intptr_t)ptr >= (intptr_t)source->addr);
823-
int to_begin = (intptr_t)ptr - (intptr_t)source->addr;
822+
assert((uintptr_t)ptr >= (uintptr_t)source->addr);
823+
size_t to_begin = (uintptr_t)ptr - (uintptr_t)source->addr;
824824
return to_begin;
825825
}
826826

@@ -837,8 +837,8 @@ static inline uint32_t
837837
void *wptr = audio_stream_get_wptr(source);
838838
int to_begin = audio_stream_rewind_bytes_without_wrap(source, wptr);
839839

840-
assert((intptr_t)wptr >= (intptr_t)source->addr);
841-
assert((intptr_t)source->end_addr > (intptr_t)wptr);
840+
assert((uintptr_t)wptr >= (uintptr_t)source->addr);
841+
assert((uintptr_t)source->end_addr > (uintptr_t)wptr);
842842

843843
if (to_begin > bytes)
844844
return (uint32_t *)((intptr_t)wptr - bytes);
@@ -858,7 +858,7 @@ audio_stream_samples_without_wrap_s16(const struct audio_stream *source, const v
858858
{
859859
int to_end = (int16_t *)source->end_addr - (int16_t *)ptr;
860860

861-
assert((intptr_t)source->end_addr >= (intptr_t)ptr);
861+
assert((uintptr_t)source->end_addr >= (uintptr_t)ptr);
862862
return to_end;
863863
}
864864

@@ -874,7 +874,7 @@ audio_stream_samples_without_wrap_s24(const struct audio_stream *source, const v
874874
{
875875
int to_end = (int32_t *)source->end_addr - (int32_t *)ptr;
876876

877-
assert((intptr_t)source->end_addr >= (intptr_t)ptr);
877+
assert((uintptr_t)source->end_addr >= (uintptr_t)ptr);
878878
return to_end;
879879
}
880880

@@ -890,7 +890,7 @@ audio_stream_samples_without_wrap_s32(const struct audio_stream *source, const v
890890
{
891891
int to_end = (int32_t *)source->end_addr - (int32_t *)ptr;
892892

893-
assert((intptr_t)source->end_addr >= (intptr_t)ptr);
893+
assert((uintptr_t)source->end_addr >= (uintptr_t)ptr);
894894
return to_end;
895895
}
896896

0 commit comments

Comments
 (0)