Fix the bugs marked in defspace.h

Now size is checked properly for defspace_free_loc and defspace_alloc_loc,
and defspace_alloc_loc check's grow()'s return value.
This commit is contained in:
Bill Currie 2012-12-04 13:40:00 +09:00
parent f7bf05034f
commit d340aac2eb
3 changed files with 9 additions and 11 deletions

View File

@ -57,9 +57,8 @@ typedef struct defspace_s {
be allocated and an internal error will be generated.
\param space This defspace.
\return 1 for success, 0 for failure.
\bug The return value is ignored.
\return 1 for success, 0 for failure. On failure, an internal
error will be generated.
*/
int (*grow) (struct defspace_s *space);
int qfo_space; ///< index to space in qfo spaces
@ -91,7 +90,6 @@ defspace_t *defspace_new (void);
\return The offset of the first word of the freshly allocated
space. May be 0 if the allocated space is at the beginning
of the defspace.
\bug does not check for allocating 0 (or negative) words.
*/
int defspace_alloc_loc (defspace_t *space, int size);
@ -113,8 +111,6 @@ int defspace_alloc_loc (defspace_t *space, int size);
\param space The space to which the freed block will be returned.
\param ofs The first word of the block to be freed.
\param size The number of words in the block to be freed.
\bug \a size is not checked for being negative.
*/
void defspace_free_loc (defspace_t *space, int ofs, int size);

View File

@ -99,6 +99,8 @@ defspace_alloc_loc (defspace_t *space, int size)
locref_t *loc;
locref_t **l = &space->free_locs;
if (size <= 0)
internal_error (0, "invalid number of words requested: %d", size);
while (*l && (*l)->size < size)
l = &(*l)->next;
if ((loc = *l)) {
@ -116,9 +118,8 @@ defspace_alloc_loc (defspace_t *space, int size)
ofs = space->size;
space->size += size;
if (space->size > space->max_size) {
if (!space->grow)
if (!space->grow || !space->grow (space))
internal_error (0, "unable to allocate %d words", size);
space->grow (space);
}
return ofs;
}
@ -129,8 +130,8 @@ defspace_free_loc (defspace_t *space, int ofs, int size)
locref_t **l;
locref_t *loc;
if (!size)
internal_error (0, "defspace: freeing size 0 location");
if (size <= 0)
internal_error (0, "defspace: freeing size %d location", size);
if (ofs < 0 || ofs >= space->size || ofs + size > space->size)
internal_error (0, "defspace: freeing bogus location %d:%d",
ofs, size);

View File

@ -536,7 +536,8 @@ add_data (int space, qfo_mspace_t *data)
{
if (space < 0 || space >= qfo_num_spaces || !work_spaces[space])
linker_internal_error ("bad space for add_data (): %d", space);
defspace_add_data (*work_spaces[space], data->d.data, data->data_size);
if (data->data_size)
defspace_add_data (*work_spaces[space], data->d.data, data->data_size);
work->spaces[space].d.data = (*work_spaces[space])->data;
work->spaces[space].data_size = (*work_spaces[space])->size;
}