Преглед изворни кода

libobs/calldata: Fix unaligned loads/stores

Found via UBSan, actual errors (addresses not pruned for illustrative purposes):

"runtime error: store to misaligned address 0x7f9a9178e84c for type
'size_t' (aka 'unsigned long'), which requires 8 byte alignment"

"runtime error: load of misaligned address 0x7f9a9140f2cf for type
'size_t' (aka 'unsigned long'), which requires 8 byte alignment"
Palana пре 10 година
родитељ
комит
c680b3e115
2 измењених фајлова са 9 додато и 7 уклоњено
  1. 8 6
      libobs/callback/calldata.c
  2. 1 1
      libobs/callback/calldata.h

+ 8 - 6
libobs/callback/calldata.c

@@ -48,7 +48,8 @@ static inline void cd_serialize(uint8_t **pos, void *ptr, size_t size)
 
 static inline size_t cd_serialize_size(uint8_t **pos)
 {
-	size_t size = *(size_t*)*pos;
+	size_t size = 0;
+	memcpy(&size, *pos, sizeof(size_t));
 	*pos += sizeof(size_t);
 	return size;
 }
@@ -97,7 +98,7 @@ static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
 	if (!len)
 		len = strlen(str)+1;
 
-	*(size_t*)*pos = len;
+	memcpy(*pos, &len, sizeof(size_t));
 	*pos += sizeof(size_t);
 	memcpy(*pos, str, len);
 	*pos += len;
@@ -105,7 +106,7 @@ static inline void cd_copy_string(uint8_t **pos, const char *str, size_t len)
 
 static inline void cd_copy_data(uint8_t **pos, const void *in, size_t size)
 {
-	*(size_t*)*pos = size;
+	memcpy(*pos, &size, sizeof(size_t));
 	*pos += sizeof(size_t);
 
 	if (size) {
@@ -133,7 +134,7 @@ static inline void cd_set_first_param(calldata_t *data, const char *name,
 	pos = data->stack;
 	cd_copy_string(&pos, name, name_len);
 	cd_copy_data(&pos, in, size);
-	*(size_t*)pos = 0;
+	memset(pos, 0, sizeof(size_t));
 }
 
 static inline void cd_ensure_capacity(calldata_t *data, uint8_t **pos,
@@ -193,7 +194,8 @@ void calldata_set_data(calldata_t *data, const char *name, const void *in,
 	}
 
 	if (cd_getparam(data, name, &pos)) {
-		size_t cur_size = *(size_t*)pos;
+		size_t cur_size;
+		memcpy(&cur_size, pos, sizeof(size_t));
 
 		if (cur_size < size) {
 			size_t offset = size - cur_size;
@@ -221,7 +223,7 @@ void calldata_set_data(calldata_t *data, const char *name, const void *in,
 
 		cd_copy_string(&pos, name, 0);
 		cd_copy_data(&pos, in, size);
-		*(size_t*)pos = 0;
+		memset(pos, 0, sizeof(size_t));
 	}
 }
 

+ 1 - 1
libobs/callback/calldata.h

@@ -70,7 +70,7 @@ static inline void calldata_clear(struct calldata *data)
 {
 	if (data->stack) {
 		data->size = sizeof(size_t);
-		*(size_t*)data->stack = 0;
+		memset(data->stack, 0, sizeof(size_t));
 	}
 }