summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAidan MacDonald <amachronic@protonmail.com>2022-05-02 15:23:37 +0100
committerAidan MacDonald <amachronic@protonmail.com>2022-05-02 15:38:48 +0100
commit6b8c94a6e3094ab75fcfe319fa2bc100f4e329ec (patch)
tree6c6ee0a2e7f77084f721ed33ff5b8856ec43fb61
parent366f00a3d3dc6517e7dcb1bbebb887c4f795320b (diff)
downloadrockbox-6b8c94a6e3094ab75fcfe319fa2bc100f4e329ec.tar.gz
rockbox-6b8c94a6e3094ab75fcfe319fa2bc100f4e329ec.zip
Fix some non-portable alignment values
UBSan reports an avalanche of unaligned pointer bugs stemming from hardcoded 4-byte alignments used in certain places. Use sizeof(long) instead to align to the machine word size. Change-Id: I28e505212462c5268afa24e95df3a103ac3e2213
-rw-r--r--apps/gui/skin_engine/skin_parser.c5
-rw-r--r--apps/plugins/pictureflow/pictureflow.c34
-rw-r--r--apps/recorder/jpeg_load.c4
-rw-r--r--apps/tagcache.c9
-rw-r--r--lib/skin_parser/skin_buffer.c4
5 files changed, 19 insertions, 37 deletions
diff --git a/apps/gui/skin_engine/skin_parser.c b/apps/gui/skin_engine/skin_parser.c
index ee32c06ace..1a6861ff16 100644
--- a/apps/gui/skin_engine/skin_parser.c
+++ b/apps/gui/skin_engine/skin_parser.c
@@ -2538,8 +2538,9 @@ bool skin_data_load(enum screen_type screen, struct wps_data *wps_data,
2538 skin_buffer = wps_buffer; 2538 skin_buffer = wps_buffer;
2539 wps_buffer = (char*)buf; 2539 wps_buffer = (char*)buf;
2540 } 2540 }
2541 skin_buffer = ALIGN_UP(skin_buffer, 4); /* align on 4-byte boundary */ 2541
2542 buffersize -= 3; 2542 /* align to long */
2543 ALIGN_BUFFER(skin_buffer, buffersize, sizeof(long));
2543#ifdef HAVE_BACKDROP_IMAGE 2544#ifdef HAVE_BACKDROP_IMAGE
2544 backdrop_filename = "-"; 2545 backdrop_filename = "-";
2545 wps_data->backdrop_id = -1; 2546 wps_data->backdrop_id = -1;
diff --git a/apps/plugins/pictureflow/pictureflow.c b/apps/plugins/pictureflow/pictureflow.c
index 5136fc56e9..83d4bb5a59 100644
--- a/apps/plugins/pictureflow/pictureflow.c
+++ b/apps/plugins/pictureflow/pictureflow.c
@@ -1303,9 +1303,8 @@ static int build_artist_index(struct tagcache_search *tcs,
1303 if (res < SUCCESS) 1303 if (res < SUCCESS)
1304 return res; 1304 return res;
1305 1305
1306 ALIGN_BUFFER(*buf, *bufsz, 4);
1307
1308 /* finalize the artist index */ 1306 /* finalize the artist index */
1307 ALIGN_BUFFER(*buf, *bufsz, alignof(struct artist_data));
1309 tmp_artist = (struct artist_data*)*buf; 1308 tmp_artist = (struct artist_data*)*buf;
1310 for (i = pf_idx.artist_ct - 1; i >= 0; i--) 1309 for (i = pf_idx.artist_ct - 1; i >= 0; i--)
1311 tmp_artist[i] = pf_idx.artist_index[-i]; 1310 tmp_artist[i] = pf_idx.artist_index[-i];
@@ -1313,7 +1312,6 @@ static int build_artist_index(struct tagcache_search *tcs,
1313 pf_idx.artist_index = tmp_artist; 1312 pf_idx.artist_index = tmp_artist;
1314 /* move buf ptr to end of artist_index */ 1313 /* move buf ptr to end of artist_index */
1315 *buf = pf_idx.artist_index + pf_idx.artist_ct; 1314 *buf = pf_idx.artist_index + pf_idx.artist_ct;
1316 ALIGN_BUFFER(*buf, *bufsz, 4);
1317 1315
1318 if (res == SUCCESS) 1316 if (res == SUCCESS)
1319 { 1317 {
@@ -1384,14 +1382,14 @@ static int create_album_index(void)
1384 int i, j, last, final, retry, res; 1382 int i, j, last, final, retry, res;
1385 1383
1386 draw_splashscreen(buf, buf_size); 1384 draw_splashscreen(buf, buf_size);
1387 ALIGN_BUFFER(buf, buf_size, 4); 1385 ALIGN_BUFFER(buf, buf_size, sizeof(long));
1388 1386
1389/* Artists */ 1387 /* Artists */
1390 res = build_artist_index(&tcs, &buf, &buf_size); 1388 res = build_artist_index(&tcs, &buf, &buf_size);
1391 if (res < SUCCESS) 1389 if (res < SUCCESS)
1392 return res; 1390 return res;
1393 1391
1394/* Albums */ 1392 /* Albums */
1395 pf_idx.album_ct = 0; 1393 pf_idx.album_ct = 0;
1396 pf_idx.album_len =0; 1394 pf_idx.album_len =0;
1397 pf_idx.album_untagged_idx = 0; 1395 pf_idx.album_untagged_idx = 0;
@@ -1407,7 +1405,6 @@ static int create_album_index(void)
1407 rb->tagcache_search_finish(&tcs); 1405 rb->tagcache_search_finish(&tcs);
1408 if (res < SUCCESS) 1406 if (res < SUCCESS)
1409 return res; 1407 return res;
1410 ALIGN_BUFFER(buf, buf_size, 4);
1411 1408
1412 /* Build artist list for untagged albums */ 1409 /* Build artist list for untagged albums */
1413 res = create_album_untagged(&tcs, &buf, &buf_size); 1410 res = create_album_untagged(&tcs, &buf, &buf_size);
@@ -1415,9 +1412,8 @@ static int create_album_index(void)
1415 if (res < SUCCESS) 1412 if (res < SUCCESS)
1416 return res; 1413 return res;
1417 1414
1418 ALIGN_BUFFER(buf, buf_size, 4);
1419
1420 /* finalize the album index */ 1415 /* finalize the album index */
1416 ALIGN_BUFFER(buf, buf_size, alignof(struct album_data));
1421 tmp_album = (struct album_data*)buf; 1417 tmp_album = (struct album_data*)buf;
1422 for (i = pf_idx.album_ct - 1; i >= 0; i--) 1418 for (i = pf_idx.album_ct - 1; i >= 0; i--)
1423 tmp_album[i] = pf_idx.album_index[-i]; 1419 tmp_album[i] = pf_idx.album_index[-i];
@@ -1425,9 +1421,8 @@ static int create_album_index(void)
1425 pf_idx.album_index = tmp_album; 1421 pf_idx.album_index = tmp_album;
1426 /* move buf ptr to end of album_index */ 1422 /* move buf ptr to end of album_index */
1427 buf = pf_idx.album_index + pf_idx.album_ct; 1423 buf = pf_idx.album_index + pf_idx.album_ct;
1428 ALIGN_BUFFER(buf, buf_size, 4);
1429 1424
1430/* Assign indices */ 1425 /* Assign indices */
1431 draw_splashscreen(buf, buf_size); 1426 draw_splashscreen(buf, buf_size);
1432 draw_progressbar(0, pf_idx.album_ct, "Assigning Albums"); 1427 draw_progressbar(0, pf_idx.album_ct, "Assigning Albums");
1433 for (j = 0; j < pf_idx.album_ct; j++) 1428 for (j = 0; j < pf_idx.album_ct; j++)
@@ -1541,7 +1536,6 @@ retry_artist_lookup:
1541 } 1536 }
1542 } 1537 }
1543 1538
1544 ALIGN_BUFFER(buf, buf_size, 4);
1545 pf_idx.buf = buf; 1539 pf_idx.buf = buf;
1546 pf_idx.buf_sz = buf_size; 1540 pf_idx.buf_sz = buf_size;
1547 pf_idx.artist_index = 0; 1541 pf_idx.artist_index = 0;
@@ -1618,7 +1612,6 @@ static int load_album_index(void){
1618 1612
1619 //rb->lseek(fr, sizeof(data) + 1, SEEK_SET); 1613 //rb->lseek(fr, sizeof(data) + 1, SEEK_SET);
1620 /* artist names */ 1614 /* artist names */
1621 ALIGN_BUFFER(buf, buf_size, 4);
1622 if (read2buf(fr, buf, data.artist_len) == 0) 1615 if (read2buf(fr, buf, data.artist_len) == 0)
1623 goto failure; 1616 goto failure;
1624 1617
@@ -1627,7 +1620,6 @@ static int load_album_index(void){
1627 buf_size -= data.artist_len; 1620 buf_size -= data.artist_len;
1628 1621
1629 /* album names */ 1622 /* album names */
1630 ALIGN_BUFFER(buf, buf_size, 4);
1631 if (read2buf(fr, buf, data.album_len) == 0) 1623 if (read2buf(fr, buf, data.album_len) == 0)
1632 goto failure; 1624 goto failure;
1633 1625
@@ -1636,7 +1628,6 @@ static int load_album_index(void){
1636 buf_size -= data.album_len; 1628 buf_size -= data.album_len;
1637 1629
1638 /* index of album names */ 1630 /* index of album names */
1639 ALIGN_BUFFER(buf, buf_size, 4);
1640 if (read2buf(fr, buf, album_idx_sz) == 0) 1631 if (read2buf(fr, buf, album_idx_sz) == 0)
1641 goto failure; 1632 goto failure;
1642 1633
@@ -4286,8 +4277,6 @@ static int pictureflow_main(const char* selected_file)
4286 init_scroll_lines(); 4277 init_scroll_lines();
4287 init_reflect_table(); 4278 init_reflect_table();
4288 4279
4289 ALIGN_BUFFER(pf_idx.buf, pf_idx.buf_sz, 4);
4290
4291 /*Scan will trigger when no file is found or the option was activated*/ 4280 /*Scan will trigger when no file is found or the option was activated*/
4292 if ((pf_cfg.cache_version != CACHE_VERSION)||(load_album_index() < 0)){ 4281 if ((pf_cfg.cache_version != CACHE_VERSION)||(load_album_index() < 0)){
4293 rb->splash(HZ/2,"Creating index, please wait"); 4282 rb->splash(HZ/2,"Creating index, please wait");
@@ -4312,23 +4301,20 @@ static int pictureflow_main(const char* selected_file)
4312 return PLUGIN_ERROR; 4301 return PLUGIN_ERROR;
4313 } 4302 }
4314 4303
4315 ALIGN_BUFFER(pf_idx.buf, pf_idx.buf_sz, 4); 4304 number_of_slides = pf_idx.album_ct;
4316 number_of_slides = pf_idx.album_ct;
4317
4318 size_t aa_bufsz = ALIGN_DOWN(pf_idx.buf_sz / 4, 0x4);
4319 4305
4306 size_t aa_bufsz = pf_idx.buf_sz / 4 + sizeof(long) - 1;
4320 if (aa_bufsz < DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(pix_t)) 4307 if (aa_bufsz < DISPLAY_WIDTH * DISPLAY_HEIGHT * sizeof(pix_t))
4321 { 4308 {
4322 error_wait("Not enough memory for album art cache"); 4309 error_wait("Not enough memory for album art cache");
4323 return PLUGIN_ERROR; 4310 return PLUGIN_ERROR;
4324 } 4311 }
4325 4312
4326 pf_idx.buf_sz -= aa_bufsz; 4313 ALIGN_BUFFER(pf_idx.buf, pf_idx.buf_sz, sizeof(long));
4327 ALIGN_BUFFER(pf_idx.buf, pf_idx.buf_sz, 4);
4328 aa_cache.buf = (char*) pf_idx.buf; 4314 aa_cache.buf = (char*) pf_idx.buf;
4329 aa_cache.buf_sz = aa_bufsz; 4315 aa_cache.buf_sz = aa_bufsz;
4330 pf_idx.buf += aa_bufsz; 4316 pf_idx.buf += aa_bufsz;
4331 ALIGN_BUFFER(pf_idx.buf, pf_idx.buf_sz, 4); 4317 pf_idx.buf_sz -= aa_bufsz;
4332 4318
4333 if (!create_empty_slide(pf_cfg.cache_version != CACHE_VERSION)) { 4319 if (!create_empty_slide(pf_cfg.cache_version != CACHE_VERSION)) {
4334 config_save(CACHE_REBUILD); 4320 config_save(CACHE_REBUILD);
diff --git a/apps/recorder/jpeg_load.c b/apps/recorder/jpeg_load.c
index 5b287aff75..34d543b56e 100644
--- a/apps/recorder/jpeg_load.c
+++ b/apps/recorder/jpeg_load.c
@@ -2024,7 +2024,7 @@ int clip_jpeg_fd(int fd,
2024#else 2024#else
2025 struct jpeg *p_jpeg = (struct jpeg*)bm->data; 2025 struct jpeg *p_jpeg = (struct jpeg*)bm->data;
2026 int tmp_size = maxsize; 2026 int tmp_size = maxsize;
2027 ALIGN_BUFFER(p_jpeg, tmp_size, sizeof(int)); 2027 ALIGN_BUFFER(p_jpeg, tmp_size, sizeof(long));
2028 /* not enough memory for our struct jpeg */ 2028 /* not enough memory for our struct jpeg */
2029 if ((size_t)tmp_size < sizeof(struct jpeg)) 2029 if ((size_t)tmp_size < sizeof(struct jpeg))
2030 return -1; 2030 return -1;
@@ -2133,7 +2133,7 @@ int clip_jpeg_fd(int fd,
2133 char *buf_end = (char *)bm->data + maxsize; 2133 char *buf_end = (char *)bm->data + maxsize;
2134 maxsize = buf_end - buf_start; 2134 maxsize = buf_end - buf_start;
2135#ifndef JPEG_FROM_MEM 2135#ifndef JPEG_FROM_MEM
2136 ALIGN_BUFFER(buf_start, maxsize, sizeof(uint32_t)); 2136 ALIGN_BUFFER(buf_start, maxsize, sizeof(long));
2137 if (maxsize < (int)sizeof(struct jpeg)) 2137 if (maxsize < (int)sizeof(struct jpeg))
2138 return -1; 2138 return -1;
2139 memmove(buf_start, p_jpeg, sizeof(struct jpeg)); 2139 memmove(buf_start, p_jpeg, sizeof(struct jpeg));
diff --git a/apps/tagcache.c b/apps/tagcache.c
index c18380854e..b6d15e7a1f 100644
--- a/apps/tagcache.c
+++ b/apps/tagcache.c
@@ -2258,17 +2258,12 @@ static int tempbuf_sort(int fd)
2258 while (idlist->next != NULL) 2258 while (idlist->next != NULL)
2259 idlist = idlist->next; 2259 idlist = idlist->next;
2260 2260
2261 ALIGN_BUFFER(tempbuf_pos, tempbuf_left, alignof(struct tempbuf_id_list));
2261 tempbuf_left -= sizeof(struct tempbuf_id_list); 2262 tempbuf_left -= sizeof(struct tempbuf_id_list);
2262 if (tempbuf_left - 4 < 0) 2263 if (tempbuf_left < 0)
2263 return -1; 2264 return -1;
2264 2265
2265 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos]; 2266 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
2266 if (tempbuf_pos & 0x03)
2267 {
2268 tempbuf_pos = (tempbuf_pos & ~0x03) + 0x04;
2269 tempbuf_left -= 3;
2270 idlist->next = (struct tempbuf_id_list *)&tempbuf[tempbuf_pos];
2271 }
2272 tempbuf_pos += sizeof(struct tempbuf_id_list); 2267 tempbuf_pos += sizeof(struct tempbuf_id_list);
2273 2268
2274 idlist = idlist->next; 2269 idlist = idlist->next;
diff --git a/lib/skin_parser/skin_buffer.c b/lib/skin_parser/skin_buffer.c
index d18122ef20..021746ba82 100644
--- a/lib/skin_parser/skin_buffer.c
+++ b/lib/skin_parser/skin_buffer.c
@@ -80,8 +80,8 @@ void* skin_buffer_alloc(size_t size)
80{ 80{
81 void *retval = NULL; 81 void *retval = NULL;
82#endif 82#endif
83 /* 32-bit aligned */ 83 /* align to long which is enough for most types */
84 size = (size + 3) & ~3; 84 size = (size + sizeof(long) - 1) & ~(sizeof(long) - 1);
85 if (size > skin_buffer_freespace()) 85 if (size > skin_buffer_freespace())
86 { 86 {
87 skin_error(MEMORY_LIMIT_EXCEEDED, NULL); 87 skin_error(MEMORY_LIMIT_EXCEEDED, NULL);