mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2025-02-24 22:58:50 +01:00
buffers: use ptrdiff_t to indicate offsets.
Previously we used int in some places and off_t for others. Neither is correct: ptrdiff_t is right for differences between pointers. (off_t is only for offsets and sizes on the filesystem.)
This commit is contained in:
parent
ec724fe8c8
commit
3aba13f779
1 changed files with 11 additions and 10 deletions
|
@ -158,7 +158,7 @@ chunk_new_with_alloc_size(size_t alloc)
|
||||||
static inline chunk_t *
|
static inline chunk_t *
|
||||||
chunk_grow(chunk_t *chunk, size_t sz)
|
chunk_grow(chunk_t *chunk, size_t sz)
|
||||||
{
|
{
|
||||||
off_t offset;
|
ptrdiff_t offset;
|
||||||
const size_t memlen_orig = chunk->memlen;
|
const size_t memlen_orig = chunk->memlen;
|
||||||
const size_t orig_alloc = CHUNK_ALLOC_SIZE(memlen_orig);
|
const size_t orig_alloc = CHUNK_ALLOC_SIZE(memlen_orig);
|
||||||
const size_t new_alloc = CHUNK_ALLOC_SIZE(sz);
|
const size_t new_alloc = CHUNK_ALLOC_SIZE(sz);
|
||||||
|
@ -440,7 +440,7 @@ chunk_copy(const chunk_t *in_chunk)
|
||||||
#endif
|
#endif
|
||||||
newch->next = NULL;
|
newch->next = NULL;
|
||||||
if (in_chunk->data) {
|
if (in_chunk->data) {
|
||||||
off_t offset = in_chunk->data - in_chunk->mem;
|
ptrdiff_t offset = in_chunk->data - in_chunk->mem;
|
||||||
newch->data = newch->mem + offset;
|
newch->data = newch->mem + offset;
|
||||||
}
|
}
|
||||||
return newch;
|
return newch;
|
||||||
|
@ -710,7 +710,8 @@ buf_move_all(buf_t *buf_out, buf_t *buf_in)
|
||||||
/** Internal structure: represents a position in a buffer. */
|
/** Internal structure: represents a position in a buffer. */
|
||||||
typedef struct buf_pos_t {
|
typedef struct buf_pos_t {
|
||||||
const chunk_t *chunk; /**< Which chunk are we pointing to? */
|
const chunk_t *chunk; /**< Which chunk are we pointing to? */
|
||||||
int pos;/**< Which character inside the chunk's data are we pointing to? */
|
ptrdiff_t pos;/**< Which character inside the chunk's data are we pointing
|
||||||
|
* to? */
|
||||||
size_t chunk_pos; /**< Total length of all previous chunks. */
|
size_t chunk_pos; /**< Total length of all previous chunks. */
|
||||||
} buf_pos_t;
|
} buf_pos_t;
|
||||||
|
|
||||||
|
@ -726,15 +727,15 @@ buf_pos_init(const buf_t *buf, buf_pos_t *out)
|
||||||
/** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
|
/** Advance <b>out</b> to the first appearance of <b>ch</b> at the current
|
||||||
* position of <b>out</b>, or later. Return -1 if no instances are found;
|
* position of <b>out</b>, or later. Return -1 if no instances are found;
|
||||||
* otherwise returns the absolute position of the character. */
|
* otherwise returns the absolute position of the character. */
|
||||||
static off_t
|
static ptrdiff_t
|
||||||
buf_find_pos_of_char(char ch, buf_pos_t *out)
|
buf_find_pos_of_char(char ch, buf_pos_t *out)
|
||||||
{
|
{
|
||||||
const chunk_t *chunk;
|
const chunk_t *chunk;
|
||||||
int pos;
|
ptrdiff_t pos;
|
||||||
tor_assert(out);
|
tor_assert(out);
|
||||||
if (out->chunk) {
|
if (out->chunk) {
|
||||||
if (out->chunk->datalen) {
|
if (out->chunk->datalen) {
|
||||||
tor_assert(out->pos < (off_t)out->chunk->datalen);
|
tor_assert(out->pos < (ptrdiff_t)out->chunk->datalen);
|
||||||
} else {
|
} else {
|
||||||
tor_assert(out->pos == 0);
|
tor_assert(out->pos == 0);
|
||||||
}
|
}
|
||||||
|
@ -762,7 +763,7 @@ buf_pos_inc(buf_pos_t *pos)
|
||||||
{
|
{
|
||||||
tor_assert(pos->pos < INT_MAX - 1);
|
tor_assert(pos->pos < INT_MAX - 1);
|
||||||
++pos->pos;
|
++pos->pos;
|
||||||
if (pos->pos == (off_t)pos->chunk->datalen) {
|
if (pos->pos == (ptrdiff_t)pos->chunk->datalen) {
|
||||||
if (!pos->chunk->next)
|
if (!pos->chunk->next)
|
||||||
return -1;
|
return -1;
|
||||||
pos->chunk_pos += pos->chunk->datalen;
|
pos->chunk_pos += pos->chunk->datalen;
|
||||||
|
@ -836,11 +837,11 @@ buf_peek_startswith(const buf_t *buf, const char *cmd)
|
||||||
|
|
||||||
/** Return the index within <b>buf</b> at which <b>ch</b> first appears,
|
/** Return the index within <b>buf</b> at which <b>ch</b> first appears,
|
||||||
* or -1 if <b>ch</b> does not appear on buf. */
|
* or -1 if <b>ch</b> does not appear on buf. */
|
||||||
static off_t
|
static ptrdiff_t
|
||||||
buf_find_offset_of_char(buf_t *buf, char ch)
|
buf_find_offset_of_char(buf_t *buf, char ch)
|
||||||
{
|
{
|
||||||
chunk_t *chunk;
|
chunk_t *chunk;
|
||||||
off_t offset = 0;
|
ptrdiff_t offset = 0;
|
||||||
tor_assert(buf->datalen < INT_MAX);
|
tor_assert(buf->datalen < INT_MAX);
|
||||||
for (chunk = buf->head; chunk; chunk = chunk->next) {
|
for (chunk = buf->head; chunk; chunk = chunk->next) {
|
||||||
char *cp = memchr(chunk->data, ch, chunk->datalen);
|
char *cp = memchr(chunk->data, ch, chunk->datalen);
|
||||||
|
@ -863,7 +864,7 @@ int
|
||||||
buf_get_line(buf_t *buf, char *data_out, size_t *data_len)
|
buf_get_line(buf_t *buf, char *data_out, size_t *data_len)
|
||||||
{
|
{
|
||||||
size_t sz;
|
size_t sz;
|
||||||
off_t offset;
|
ptrdiff_t offset;
|
||||||
|
|
||||||
if (!buf->head)
|
if (!buf->head)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue