Skip to content

Commit dae017c

Browse files
robnlundman
authored andcommitted
lua: add flex array field to TString type
Linux 6.10+ with CONFIG_FORTIFY_SOURCE notices memcpy() accessing past the end of TString, because it has no indication that there there may be an additional allocation there. There's no appropriate upstream change for this (ancient) version of Lua, so this is the narrowest change I could come up with to add a flex array field to the end of TString to satisfy the check. It's loosely based on changes from lua/lua@ca41b43f and lua/lua@9514abc2. Sponsored-by: https://despairlabs.com/sponsor/ Reviewed-by: Alexander Motin <[email protected]> Reviewed-by: Brian Behlendorf <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes openzfs#16541 Closes openzfs#16583
1 parent 00ce608 commit dae017c

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

module/lua/lobject.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -404,19 +404,22 @@ typedef TValue *StkId; /* index to stack elements */
404404
/*
405405
** Header for string value; string bytes follow the end of this structure
406406
*/
407-
typedef union TString {
408-
L_Umaxalign dummy; /* ensures maximum alignment for strings */
409-
struct {
410-
CommonHeader;
411-
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
412-
unsigned int hash;
413-
size_t len; /* number of characters in string */
414-
} tsv;
407+
typedef struct TString {
408+
union {
409+
L_Umaxalign dummy; /* ensures maximum alignment for strings */
410+
struct {
411+
CommonHeader;
412+
lu_byte extra; /* reserved words for short strings; "has hash" for longs */
413+
unsigned int hash;
414+
size_t len; /* number of characters in string */
415+
} tsv;
416+
};
417+
char contents[];
415418
} TString;
416419

417420

418421
/* get the actual string (array of bytes) from a TString */
419-
#define getstr(ts) cast(const char *, (ts) + 1)
422+
#define getstr(ts) ((ts)->contents)
420423

421424
/* get the actual string (array of bytes) from a Lua value */
422425
#define svalue(o) getstr(rawtsvalue(o))

module/lua/lstate.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct lua_State {
185185
*/
186186
union GCObject {
187187
GCheader gch; /* common header */
188-
union TString ts;
188+
struct TString ts;
189189
union Udata u;
190190
union Closure cl;
191191
struct Table h;

module/lua/lstring.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static TString *createstrobj (lua_State *L, const char *str, size_t l,
103103
ts->tsv.len = l;
104104
ts->tsv.hash = h;
105105
ts->tsv.extra = 0;
106-
sbuf = (char *)(TString *)(ts + 1);
106+
sbuf = ts->contents;
107107
memcpy(sbuf, str, l*sizeof(char));
108108
sbuf[l] = '\0'; /* ending 0 */
109109
return ts;

module/lua/lstring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "lstate.h"
1313

1414

15-
#define sizestring(s) (sizeof(union TString)+((s)->len+1)*sizeof(char))
15+
#define sizestring(s) (sizeof(struct TString)+((s)->len+1)*sizeof(char))
1616

1717
#define sizeudata(u) (sizeof(union Udata)+(u)->len)
1818

0 commit comments

Comments
 (0)