3131
3232static bool fifo_initialize_internal (fifo_buffer_t * buf , size_t len )
3333{
34- uint8_t * buffer ;
35-
36- /* The ring reserves one slot to distinguish empty from full,
37- * so the actual allocation is (len + 1) bytes. Reject @len
38- * values that would wrap that addition: SIZE_MAX would
39- * compute (size_t)0, which calloc(1, 0) is allowed to satisfy
40- * with a non-NULL pointer to a zero-byte allocation. Letting
41- * that succeed would leave buf->size == 0 and the next
42- * fifo_write would divide by zero at the `% buffer->size`
43- * step. No current caller asks for SIZE_MAX, so the rejection
44- * is purely defensive. */
45- if (len >= SIZE_MAX )
46- return false;
47-
48- buffer = (uint8_t * )calloc (1 , len + 1 );
34+ uint8_t * buffer = (uint8_t * )calloc (1 , len + 1 );
4935
5036 if (!buffer )
5137 return false;
@@ -105,31 +91,8 @@ fifo_buffer_t *fifo_new(size_t len)
10591
10692void fifo_write (fifo_buffer_t * buffer , const void * in_buf , size_t len )
10793{
108- size_t first_write ;
94+ size_t first_write = len ;
10995 size_t rest_write = 0 ;
110- size_t avail ;
111-
112- /* Cap @len at the available space. Existing callers all
113- * gate on FIFO_WRITE_AVAIL before invoking us, so this is
114- * a no-op for them; for any caller that doesn't, the
115- * unbounded branch below would walk off the end of
116- * @buffer->buffer (the wrap-around copy at line `memcpy(
117- * buffer->buffer, ..., rest_write)` would write up to
118- * len - first_write bytes into a buffer of @buffer->size
119- * total, overrunning by len - size). Worse, the original
120- * `buffer->end + len > buffer->size` test wraps in size_t
121- * for huge @len and silently misclassifies the request as
122- * "fits in one chunk", taking the corrupting first memcpy
123- * down a path with no wrap-around bound at all. Capping
124- * here closes both windows. */
125- avail = FIFO_WRITE_AVAIL (buffer );
126- if (len > avail )
127- len = avail ;
128-
129- if (!len )
130- return ;
131-
132- first_write = len ;
13396
13497 if (buffer -> end + len > buffer -> size )
13598 {
@@ -146,22 +109,8 @@ void fifo_write(fifo_buffer_t *buffer, const void *in_buf, size_t len)
146109
147110void fifo_read (fifo_buffer_t * buffer , void * in_buf , size_t len )
148111{
149- size_t first_read ;
112+ size_t first_read = len ;
150113 size_t rest_read = 0 ;
151- size_t avail ;
152-
153- /* Same rationale as fifo_write: cap @len at what's actually
154- * available to avoid out-of-buffer copies on a caller that
155- * forgot to gate on FIFO_READ_AVAIL. Existing callers all
156- * gate first; this is defensive. */
157- avail = FIFO_READ_AVAIL (buffer );
158- if (len > avail )
159- len = avail ;
160-
161- if (!len )
162- return ;
163-
164- first_read = len ;
165114
166115 if (buffer -> first + len > buffer -> size )
167116 {
0 commit comments