Skip to content

audio: use size_t for circular-buffer bytes counts - #11208

Open
softwarecki wants to merge 3 commits into
thesofproject:mainfrom
softwarecki:p20-size_t2
Open

softwarecki wants to merge 3 commits into
thesofproject:mainfrom
softwarecki:p20-size_t2

Conversation

@softwarecki

Copy link
Copy Markdown
Collaborator

Change cir_buf_bytes_without_wrap() and cir_buf_bytes_without_wrap_rewind() to return size_t instead of int. Both now use uintptr_t for the pointer arithmetic. Correct doxygen comments of cir_buf_bytes_without_wrap(), cir_buf_wrap() and source_cir_buf_wrap().

In the volume module change the frames parameter, return type, and the internal byte/frame/sample counters of vol_zc_get_s16/s24/s32 (and the vol_zc_func typedef) to size_t, and make the channel count unsigned.

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.

…lpers

Change cir_buf_bytes_without_wrap() and cir_buf_bytes_without_wrap_rewind()
to return size_t instead of int. Both now use uintptr_t for the pointer
arithmetic. Correct doxygen comments of cir_buf_bytes_without_wrap(),
cir_buf_wrap() and source_cir_buf_wrap().

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
Change the frames parameter, return type, and the internal byte/frame/
sample counters of vol_zc_get_s16/s24/s32 (and the vol_zc_func typedef)
to size_t, and make the channel count unsigned.

Signed-off-by: Adrian Warecki <adrian.warecki@intel.com>
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Signed pointer-distance arithmetic remains in circular-buffer helpers and can overflow on valid 32-bit address-boundary cases.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

This PR updates circular-buffer byte accounting and volume zero-crossing interfaces to use unsigned, pointer-sized types, with related assertion and documentation fixes.

Changes:

  • Uses size_t and uintptr_t for byte calculations.
  • Updates volume zero-crossing counters and callback types.
  • Corrects pointer assertions and Doxygen documentation.
File summaries
File Summary
src/include/sof/audio/audio_stream.h Updates stream pointer assertions and byte-distance handling.
src/include/module/audio/audio_stream.h Updates circular-buffer helpers and documentation.
src/audio/volume/volume.h Updates the zero-crossing callback signature.
src/audio/volume/volume.c Updates zero-crossing counters and channel typing.
Review details

Suppressed comments (3)

src/include/module/audio/audio_stream.h:205

  • cir_buf_wrap() is used for both source/read and sink/write pointers (for example, the output pointer in src/audio/component.c:311), so describing ptr as a read pointer is inaccurate for this generic helper. Please document it as a read-or-write pointer, as the previous API wording did.
 * Verifies the pointer and performs rollover when reaching the end of the circular buffer.
 * @param ptr Read pointer that may have moved past the buffer end.

src/include/sof/audio/audio_stream.h:824

  • The rewind assertion is now unsigned, but to_begin is still computed by subtracting intptr_t addresses and storing the result in int. A valid range crossing 0x80000000 therefore passes the assertion while the subtraction can overflow and return an incorrect rewind distance. Use unsigned pointer-sized arithmetic and propagate a suitable non-negative type.
	assert((uintptr_t)ptr >= (uintptr_t)source->addr);
	int to_begin = (intptr_t)ptr - (intptr_t)source->addr;
	return to_begin;

src/include/sof/audio/audio_stream.h:841

  • These checks now accept addresses on either side of 0x80000000, but the rewind results below still convert the addresses to intptr_t before subtracting. A valid rewind that crosses that sign boundary can therefore invoke signed-overflow arithmetic and return an invalid pointer. Keep the pointer-distance calculations unsigned (or use pointer arithmetic) throughout this helper.
	assert((uintptr_t)wptr >= (uintptr_t)source->addr);
	assert((uintptr_t)source->end_addr > (uintptr_t)wptr);
  • Files reviewed: 4/4 changed files
  • Comments generated: 1
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +807 to 808
assert((uintptr_t)source->end_addr >= (uintptr_t)ptr);
return (intptr_t)source->end_addr - (intptr_t)ptr;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants