Add two macros for char*-based pointer arithmetic and use it in font.c

This fixes errornous pointer addition (+ on a short*), which crashed in some situation.
Fixes FS#12317 and should hopefully get the clips booting again.

Thanks to Jonathan Gordon for spotting the bad pointer arithmetic.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30724 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Martitz 2011-10-07 19:29:18 +00:00
parent 42a33a7f70
commit 5783505b99
2 changed files with 13 additions and 14 deletions

View file

@ -124,6 +124,8 @@ int get_cpu_boost_counter(void);
ptr = (typeof(ptr))tmp_ptr1; \ ptr = (typeof(ptr))tmp_ptr1; \
} }
#define PTR_ADD(ptr, x) ((typeof(ptr))((char*)(ptr) + (x)))
#define PTR_SUB(ptr, x) ((typeof(ptr))((char*)(ptr) - (x)))
/* newer? SDL includes endian.h, So we ignore it */ /* newer? SDL includes endian.h, So we ignore it */
#if (CONFIG_PLATFORM & PLATFORM_HOSTED) || defined(__PCTOOL__) #if (CONFIG_PLATFORM & PLATFORM_HOSTED) || defined(__PCTOOL__)

View file

@ -88,26 +88,23 @@ static int buflibmove_callback(int handle, void* current, void* new)
{ {
(void)handle; (void)handle;
struct buflib_alloc_data *alloc = (struct buflib_alloc_data*)current; struct buflib_alloc_data *alloc = (struct buflib_alloc_data*)current;
size_t diff = new - current; ptrdiff_t diff = new - current;
if (alloc->handle_locked) if (alloc->handle_locked)
return BUFLIB_CB_CANNOT_MOVE; return BUFLIB_CB_CANNOT_MOVE;
if (alloc->font.bits) #define UPDATE(x) if (x) { x = PTR_ADD(x, diff); }
alloc->font.bits += diff;
if (alloc->font.offset)
alloc->font.offset += diff;
if (alloc->font.width)
alloc->font.width += diff;
alloc->font.buffer_start += diff; UPDATE(alloc->font.bits);
alloc->font.buffer_end += diff; UPDATE(alloc->font.offset);
alloc->font.buffer_position += diff; UPDATE(alloc->font.width);
if (alloc->font.cache._index) UPDATE(alloc->font.buffer_start);
alloc->font.cache._index += diff; UPDATE(alloc->font.buffer_end);
if (alloc->font.cache._lru._base) UPDATE(alloc->font.buffer_position);
alloc->font.cache._lru._base += diff;
UPDATE(alloc->font.cache._index);
UPDATE(alloc->font.cache._lru._base);
return BUFLIB_CB_OK; return BUFLIB_CB_OK;
} }