Skip to content

Commit ad8a275

Browse files
authored
Fix some UB in movie_lib.cc (#117)
1 parent ef27a64 commit ad8a275

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed

src/movie_lib.cc

+47-34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "movie_lib.h"
66

77
#include <assert.h>
8+
#include <stdint.h>
89
#include <stdio.h>
910
#include <string.h>
1011

@@ -96,6 +97,10 @@ static int _MVE_sndDecompS16(unsigned short* a1, unsigned char* a2, int a3, int
9697
static void _nfPkConfig();
9798
static void _nfPkDecomp(unsigned char* buf, unsigned char* a2, int a3, int a4, int a5, int a6);
9899

100+
static constexpr uint16_t loadUInt16LE(const uint8_t* b);
101+
static constexpr uint32_t loadUInt32LE(const uint8_t* b);
102+
static uint8_t getOffset(uint16_t v);
103+
99104
// 0x51EBD8
100105
static int dword_51EBD8 = 0;
101106

@@ -757,7 +762,7 @@ static unsigned char* _ioNextRecord()
757762
return NULL;
758763
}
759764

760-
_io_next_hdr = *(int*)(buf + (_io_next_hdr & 0xFFFF));
765+
_io_next_hdr = loadUInt32LE(buf + (_io_next_hdr & 0xFFFF));
761766

762767
return buf;
763768
}
@@ -854,7 +859,7 @@ int _MVE_rmStepMovie()
854859
}
855860

856861
while (1) {
857-
v5 = *(unsigned int*)((unsigned char*)v1 + v0);
862+
v5 = loadUInt32LE((unsigned char*)v1 + v0);
858863
v1 = (unsigned short*)((unsigned char*)v1 + v0 + 4);
859864
v0 = v5 & 0xFFFF;
860865

@@ -877,7 +882,7 @@ int _MVE_rmStepMovie()
877882
} else {
878883
v7 = (v1[1] & 0x04) >> 2;
879884
}
880-
v8 = *(unsigned int*)((unsigned char*)v1 + 6);
885+
v8 = loadUInt32LE((unsigned char*)v1 + 6);
881886
if ((v5 >> 24) == 0) {
882887
v8 &= 0xFFFF;
883888
}
@@ -1438,7 +1443,7 @@ static int _MVE_sndAdd(unsigned char* dest, unsigned char** src_ptr, int a3, int
14381443
}
14391444

14401445
if (a5) {
1441-
v12 = *(unsigned int*)src;
1446+
v12 = loadUInt32LE(src);
14421447
src += 4;
14431448

14441449
*(unsigned int*)dest = v12;
@@ -1868,8 +1873,6 @@ static void _nfPkDecomp(unsigned char* a1, unsigned char* a2, int a3, int a4, in
18681873
int i;
18691874
int j;
18701875
ptrdiff_t v10;
1871-
int v11;
1872-
int v13;
18731876
int byte;
18741877
unsigned int value1;
18751878
unsigned int value2;
@@ -1919,38 +1922,36 @@ static void _nfPkDecomp(unsigned char* a1, unsigned char* a2, int a3, int a4, in
19191922
break;
19201923
case 2:
19211924
case 3:
1922-
byte = *a2++;
1923-
v11 = word_51F618[byte];
1924-
if (v7 == 3) {
1925-
v11 = ((-(v11 & 0xFF)) & 0xFF) | ((-(v11 >> 8) & 0xFF) << 8);
1926-
} else {
1927-
v11 = v11;
1925+
if (1) {
1926+
byte = *a2++;
1927+
uint16_t offset = word_51F618[byte];
1928+
if (v7 == 3) {
1929+
offset = ((-(offset & 0xFF)) & 0xFF) | ((-(offset >> 8) & 0xFF) << 8);
1930+
}
1931+
v10 = getOffset(offset);
19281932
}
1929-
v10 = ((v11 << 24) >> 24) + dword_51F018[v11 >> 8];
19301933
break;
19311934
case 4:
19321935
case 5:
1933-
if (v7 == 4) {
1934-
byte = *a2++;
1935-
v13 = word_51F418[byte];
1936-
} else {
1937-
v13 = *(unsigned short*)a2;
1938-
a2 += 2;
1939-
}
1936+
if (1) {
1937+
uint16_t offset;
1938+
if (v7 == 4) {
1939+
byte = *a2++;
1940+
offset = word_51F418[byte];
1941+
} else {
1942+
offset = loadUInt16LE(a2);
1943+
a2 += 2;
1944+
}
19401945

1941-
v10 = ((v13 << 24) >> 24) + dword_51F018[v13 >> 8] + (gMovieDirectDrawSurfaceBuffer2 - gMovieDirectDrawSurfaceBuffer1);
1946+
v10 = getOffset(offset) + (gMovieDirectDrawSurfaceBuffer2 - gMovieDirectDrawSurfaceBuffer1);
1947+
}
19421948
break;
19431949
}
19441950

19451951
value2 = _mveBW;
19461952

1947-
for (i = 0; i < 8; i++) {
1948-
src_ptr = (unsigned int*)(dest + v10);
1949-
dest_ptr = (unsigned int*)dest;
1950-
1951-
dest_ptr[0] = src_ptr[0];
1952-
dest_ptr[1] = src_ptr[1];
1953-
1953+
for (i = 0; i < 8; ++i) {
1954+
memcpy(dest, dest + v10, 8);
19541955
dest += value2;
19551956
}
19561957

@@ -2669,11 +2670,8 @@ static void _nfPkDecomp(unsigned char* a1, unsigned char* a2, int a3, int a4, in
26692670
case 11:
26702671
value2 = _mveBW;
26712672

2672-
src_ptr = (unsigned int*)a2;
2673-
for (i = 0; i < 8; i++) {
2674-
dest_ptr = (unsigned int*)dest;
2675-
dest_ptr[0] = src_ptr[i * 2];
2676-
dest_ptr[1] = src_ptr[i * 2 + 1];
2673+
for (i = 0; i < 32; i += 4) {
2674+
memcpy(dest, &a2[i * 2], 8);
26772675
dest += value2;
26782676
}
26792677

@@ -2763,7 +2761,7 @@ static void _nfPkDecomp(unsigned char* a1, unsigned char* a2, int a3, int a4, in
27632761
value1 = byte | (byte << 8) | (byte << 16) | (byte << 24);
27642762
value2 = value1;
27652763
} else {
2766-
byte = *(unsigned short*)a2;
2764+
byte = loadUInt16LE(a2);
27672765
a2 += 2;
27682766
value1 = byte | (byte << 16);
27692767
value2 = value1;
@@ -2794,4 +2792,19 @@ static void _nfPkDecomp(unsigned char* a1, unsigned char* a2, int a3, int a4, in
27942792
}
27952793
}
27962794

2795+
constexpr uint16_t loadUInt16LE(const uint8_t* b)
2796+
{
2797+
return (b[1] << 8) | b[0];
2798+
}
2799+
2800+
constexpr uint32_t loadUInt32LE(const uint8_t* b)
2801+
{
2802+
return (b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0];
2803+
}
2804+
2805+
uint8_t getOffset(uint16_t v)
2806+
{
2807+
return static_cast<int8_t>(v & 0xFF) + dword_51F018[v >> 8];
2808+
}
2809+
27972810
} // namespace fallout

0 commit comments

Comments
 (0)