Skip to content

Commit 76be096

Browse files
committed
small: fix false-positive 'out of array bound' error
Static allocator uses a global char[] array to hand it out in chunks. The array addressing was done via '&buffer[index]', but GCC compiler thinks that there is an out of bound array access, when sees something like static_alloc(max_size + 1). Having static_alloc() inlined it somewhy thinks, that this size is used as an index of the static buffer, despite the fact, that such sizes are filtered out in 'if's before indexing the buffer. This test was leading to the compilation error: test/static.c:78: static_alloc(SMALL_STATIC_SIZE + 1) Static_alloc checks if a current position + size > max_size, then check is size > max_size, and then returns NULL, the test checks it. But GCC can't detect that, and thinks that this code makes 'buffer[max_size + 1]'. This commit uses pointer arithmetic instead of address operator to avoid this bogus error.
1 parent 45710a9 commit 76be096

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

small/static.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,11 @@ static_reserve(size_t size)
7878
return NULL;
7979
static_storage_pos = 0;
8080
}
81-
return &static_storage_buffer[static_storage_pos];
81+
/*
82+
* Do not use &buf[...] - some compilers does not allow
83+
* &buf[len] and think it is an error.
84+
*/
85+
return static_storage_buffer + static_storage_pos;
8286
}
8387

8488
/**
@@ -120,7 +124,7 @@ static_aligned_alloc(size_t size, size_t alignment)
120124
* Aligned reserve could add a padding. Restore
121125
* the position.
122126
*/
123-
static_storage_pos = (char *) res - &static_storage_buffer[0];
127+
static_storage_pos = (char *) res - static_storage_buffer;
124128
static_storage_pos += size;
125129
}
126130
return res;

test/static.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ static inline void
3535
check_static_alloc(size_t size, size_t first_pos, size_t end_pos)
3636
{
3737
char *b = static_alloc(size);
38-
is(b, &static_storage_buffer[first_pos], "allocated %d from %d",
38+
is(b, static_storage_buffer + first_pos, "allocated %d from %d",
3939
(int) size, (int) first_pos);
4040
is(static_storage_pos, end_pos, "to %d", (int) end_pos);
4141
}

0 commit comments

Comments
 (0)