Skip to content

Commit 6767235

Browse files
authored
Fix Amiga2 palette bounds handling (#120)
1 parent 0070f5b commit 6767235

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

Source/Amiga/Graphics_Amiga2.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,18 +98,19 @@ tSharedBuffer cGraphics_Amiga2::GetPalette(const std::string pFilename) {
9898
auto Palette = g_Resource->fileGet(Filename);
9999

100100
auto a0 = Palette->data();
101-
for (; a0 < Palette->data() + Palette->size(); ++a0) {
101+
auto a0End = Palette->data() + Palette->size();
102+
for (; a0 + 2 < a0End; a0 += 3) {
102103

103-
uint16 d0 = *a0++;
104-
uint16 d1 = *a0++;
104+
uint16 d0 = a0[0];
105+
uint16 d1 = a0[1];
105106

106107
d0 &= 0xF0;
107108
d0 <<= 4;
108109

109110
d1 &= 0xF0;
110111
d0 |= d1;
111112

112-
d1 = *a0;
113+
d1 = a0[2];
113114
d1 &= 0xF0;
114115
d1 >>= 4;
115116
d0 |= d1;

Source/Graphics.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,13 @@ struct sImage {
9898

9999
uint16 color;
100100

101-
for (size_t ColorID = pStartColorID; ColorID < pStartColorID + pCount; ColorID++) {
101+
if (pStartColorID >= 256)
102+
return;
103+
104+
const size_t MaxColors = 256 - pStartColorID;
105+
const size_t PaletteCount = std::min(pCount, MaxColors);
106+
107+
for (size_t ColorID = pStartColorID; ColorID < pStartColorID + PaletteCount; ColorID++) {
102108

103109
// Get the next color codes
104110
color = readBEWord(pFrom);

0 commit comments

Comments
 (0)