mirror of
http://git.haproxy.org/git/haproxy.git/
synced 2025-05-03 00:08:10 +00:00
BUG/MINOR: ncbuf: fix ncb_is_empty()
ncb_is_empty() was plainly incorrect as it directly dereferences the memory to read offset blocks instead of ncb_read_off(). The result is undefined. Also, BUG_ON() statement is wrong when the buffer starts with a data block. In this case, ncb_head() is not the first gap offset but instead just random data. The calculated sum in BUG_ON() statement has thus no meaning and may cause an abort. Adjust this by reorganizing the whole function. Only the first data block size is read. If and only if not nul, the first gap size is then checked. ncb_is_full() has been rewritten to share the same model as ncb_is_empty().
This commit is contained in:
parent
82c51b561e
commit
f6dbdc1444
22
src/ncbuf.c
22
src/ncbuf.c
@ -457,21 +457,35 @@ ncb_sz_t ncb_total_data(const struct ncbuf *buf)
|
||||
/* Returns true if there is no data anywhere in <buf>. */
|
||||
int ncb_is_empty(const struct ncbuf *buf)
|
||||
{
|
||||
int first_data, first_gap;
|
||||
|
||||
if (ncb_is_null(buf))
|
||||
return 1;
|
||||
|
||||
BUG_ON_HOT(*ncb_reserved(buf) + *ncb_head(buf) > ncb_size(buf));
|
||||
return *ncb_reserved(buf) == 0 && *ncb_head(buf) == ncb_size(buf);
|
||||
first_data = ncb_read_off(buf, ncb_reserved(buf));
|
||||
BUG_ON_HOT(first_data > ncb_size(buf));
|
||||
/* Buffer is not empty if first data block is not nul. */
|
||||
if (first_data)
|
||||
return 0;
|
||||
|
||||
/* Head contains the first gap size if first data block is empty. */
|
||||
first_gap = ncb_read_off(buf, ncb_head(buf));
|
||||
BUG_ON_HOT(first_gap > ncb_size(buf));
|
||||
return first_gap == ncb_size(buf);
|
||||
}
|
||||
|
||||
/* Returns true if no more data can be inserted in <buf>. */
|
||||
int ncb_is_full(const struct ncbuf *buf)
|
||||
{
|
||||
int first_data;
|
||||
|
||||
if (ncb_is_null(buf))
|
||||
return 0;
|
||||
|
||||
BUG_ON_HOT(ncb_read_off(buf, ncb_reserved(buf)) > ncb_size(buf));
|
||||
return ncb_read_off(buf, ncb_reserved(buf)) == ncb_size(buf);
|
||||
/* First data block must cover whole buffer if full. */
|
||||
first_data = ncb_read_off(buf, ncb_reserved(buf));
|
||||
BUG_ON_HOT(first_data > ncb_size(buf));
|
||||
return first_data == ncb_size(buf);
|
||||
}
|
||||
|
||||
/* Returns the number of bytes of data avaiable in <buf> starting at offset
|
||||
|
Loading…
Reference in New Issue
Block a user