Skip to content

Commit 9e20981

Browse files
authored
Fix sanitizer warning (#771)
* Let's try this * Fix out-of-bounds access in MULRK struct handling * Take 2 In response to seeing this: libxls/xls.c:589:23: runtime error: load of misaligned address 0x50700005cbb6 for type 'DWORD', which requires 4 byte alignment * Move and turn into inline functions The use of inline functions is in response to this: Found the following significant warnings: libxls/xls.c:121:31: warning: ISO C forbids braced-groups within expressions [-Wpedantic] libxls/xls.c:126:34: warning: ISO C forbids braced-groups within expressions [-Wpedantic] * Apply fix to another MULRK location * Revert "Apply fix to another MULRK location" This reverts commit 738df8a. * Craft a similar fix for MULBLANK (not MULRK, oops) * Fix for access inside LABEL * Adopt patch strategy used in positron * Revert whitespace changes I did not mean to make earlier * Moar checks * Get rid of trailing comma
1 parent f070f74 commit 9e20981

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

.github/workflows/rhub.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ on:
2727
config:
2828
description: 'A comma separated list of R-hub platforms to use. These default choices have been customized for readxl.'
2929
type: string
30-
default: 'gcc-asan,valgrind,rchk,gcc15'
30+
default: 'gcc-asan,valgrind,rchk,gcc15,clang-asan,clang-ubsan'
3131
name:
3232
description: 'Run name. You can leave this empty now.'
3333
type: string

src/libxls/xls.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,10 @@ int xls_isCellTooSmall(xlsWorkBook* pWB, BOF* bof, BYTE* buf) {
478478
if (bof->size < offsetof(LABEL, value) + 2)
479479
return 1;
480480

481-
size_t label_len = ((LABEL*)buf)->value[0] + (((LABEL*)buf)->value[1] << 8);
481+
// --- Start readxl ---
482+
BYTE *value = get_LABEL_value((LABEL*)buf);
483+
size_t label_len = value[0] + (value[1] << 8);
484+
// --- End readxl ---
482485
if (pWB->is5ver) {
483486
return (bof->size < offsetof(LABEL, value) + 2 + label_len);
484487
}
@@ -580,8 +583,10 @@ static struct st_cell_data *xls_addCell(xlsWorkSheet* pWS,BOF* bof,BYTE* buf)
580583
}
581584
cell=&row->cells.cell[index];
582585
cell->id=XLS_RECORD_RK;
583-
cell->xf=xlsShortVal(((MULRK*)buf)->rk[i].xf);
584-
cell->d=NumFromRk(xlsIntVal(((MULRK*)buf)->rk[i].value));
586+
// --- Start readxl ---
587+
cell->xf=xlsShortVal(get_MULRK_RK_XF((MULRK*)buf, i));
588+
cell->d=NumFromRk(xlsIntVal(get_MULRK_RK_VALUE((MULRK*)buf, i)));
589+
// --- End readxl ---
585590
xls_cell_set_str(cell, xls_getfcell(pWS->workbook,cell, NULL));
586591
}
587592
break;
@@ -595,7 +600,9 @@ static struct st_cell_data *xls_addCell(xlsWorkSheet* pWS,BOF* bof,BYTE* buf)
595600
}
596601
cell=&row->cells.cell[index];
597602
cell->id=XLS_RECORD_BLANK;
598-
cell->xf=xlsShortVal(((MULBLANK*)buf)->xf[i]);
603+
// --- Start readxl ---
604+
cell->xf=xlsShortVal(get_MULBLANK_XF((MULBLANK*)buf, i));
605+
// --- End readxl ---
599606
xls_cell_set_str(cell, xls_getfcell(pWS->workbook,cell, NULL));
600607
}
601608
break;

src/libxls/xlsstruct.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
#define XLS_STRUCT_INC
3838

3939
#include "libxls/ole.h"
40+
// --- Start readxl ---
41+
#include <string.h>
42+
// --- End readxl ---
4043

4144
#define XLS_RECORD_EOF 0x000A
4245
#define XLS_RECORD_DEFINEDNAME 0x0018
@@ -204,6 +207,19 @@ typedef struct MULRK
204207
}
205208
MULRK;
206209

210+
// --- Start readxl ---
211+
static inline WORD get_MULRK_RK_XF(MULRK *mulrk, int i) {
212+
WORD xf;
213+
memcpy(&xf, (BYTE *)mulrk + offsetof(MULRK, rk) + i * (sizeof(WORD) + sizeof(DWORD)), sizeof(WORD));
214+
return xf;
215+
}
216+
217+
static inline DWORD get_MULRK_RK_VALUE(MULRK *mulrk, int i) {
218+
DWORD value;
219+
memcpy(&value, (BYTE *)mulrk + offsetof(MULRK, rk) + i * (sizeof(WORD) + sizeof(DWORD)) + sizeof(WORD), sizeof(DWORD));
220+
return value;
221+
}
222+
// --- End readxl ---
207223
typedef struct MULBLANK
208224
{
209225
WORD row;
@@ -213,6 +229,14 @@ typedef struct MULBLANK
213229
}
214230
MULBLANK;
215231

232+
// --- Start readxl ---
233+
static inline WORD get_MULBLANK_XF(MULBLANK *mulblank, int i) {
234+
WORD xf;
235+
memcpy(&xf, (BYTE *)mulblank + offsetof(MULBLANK, xf) + i * sizeof(WORD), sizeof(WORD));
236+
return xf;
237+
}
238+
// --- End readxl ---
239+
216240
typedef struct BLANK
217241
{
218242
WORD row;
@@ -230,6 +254,12 @@ typedef struct LABEL
230254
}
231255
LABEL;
232256

257+
// --- Start readxl ---
258+
static inline BYTE *get_LABEL_value(LABEL *label) {
259+
return (BYTE *)label + offsetof(LABEL, value);
260+
}
261+
// --- End readxl ---
262+
233263
typedef struct BOOLERR
234264
{
235265
WORD row;

0 commit comments

Comments
 (0)