Skip to content

Commit db5bb0d

Browse files
callsopejaquay
authored andcommitted
Avoid out-of-bounds video access
1 parent 92386a0 commit db5bb0d

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

defines.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,32 @@ namespace VCC
118118
};
119119

120120

121+
//
122+
// bounds checking on existing array
123+
// NOTE: only for video surface, out-of-bounds wraps
124+
//
125+
template <class T, size_t Size>
126+
struct VideoArray
127+
{
128+
VideoArray(T* p) : data(p) {}
129+
130+
T& operator[](size_t index)
131+
{
132+
assert(index < Size);
133+
return data[index % Size];
134+
}
135+
136+
const T& operator[](size_t index) const
137+
{
138+
assert(index < Size);
139+
return data[index % Size];
140+
}
141+
142+
private:
143+
T* data;
144+
};
145+
146+
121147
//
122148
// protect T by verifying surrounding bytes
123149
//

tcc1014graphics.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,14 @@ This file is part of VCC (Virtual Color Computer).
3030
#include <stdio.h>
3131
#include <commctrl.h> // Windows common controls
3232

33+
using Surface32 = VCC::VideoArray<unsigned int, 640 * 480>;
34+
3335
void SetupDisplay(void); //This routine gets called every time a software video register get updated.
3436
void MakeRGBPalette (void);
3537
void MakeCMPpalette(void);
3638
bool DDFailedCheck(HRESULT hr, char *szMessage);
3739
char *DDErrorString(HRESULT hr);
38-
void RenderPMODE4NTSC(unsigned int* surfaceDest, int XpitchDest, const unsigned char* cocoSrc, char scanLines);
40+
void RenderPMODE4NTSC(Surface32 surface32, size_t surfaceDest, int XpitchDest, const unsigned char* cocoSrc, char scanLines);
3941

4042
//extern STRConfig CurrentConfig;
4143
static unsigned char ColorValues[4]={0,85,170,255};
@@ -6364,7 +6366,7 @@ void UpdateScreen32(SystemState *USState32)
63646366
char Pix = 0, Bit = 0, Sphase = 0;
63656367
static char Carry1 = 0, Carry2 = 0;
63666368
static char Pcolor = 0;
6367-
unsigned int *szSurface32 = USState32->PTRsurface32;
6369+
Surface32 szSurface32(USState32->PTRsurface32);
63686370
unsigned short y = USState32->LineCounter;
63696371
long Xpitch = USState32->SurfacePitch;
63706372
Carry1 = 1;
@@ -8280,10 +8282,10 @@ case 192+2: //Bpp=0 Sr=2
82808282
// byte pointer to ram
82818283
unsigned char* cocoRam = (unsigned char*)WideBuffer;
82828284
// destination screen (less 2 pixels for bleed)
8283-
unsigned int* surfaceDest = szSurface32 + (((y + VertCenter) * 2) * Xpitch) + HorzCenter - 2;
8285+
size_t surfaceDest = (((y + VertCenter) * 2) * Xpitch) + HorzCenter - 2;
82848286
// source coco screens
82858287
unsigned char* cocoSrc = cocoRam + (VidMask & (Start + (unsigned char)Hoffset));
8286-
RenderPMODE4NTSC(surfaceDest, Xpitch, cocoSrc, USState32->ScanLines);
8288+
RenderPMODE4NTSC(szSurface32, surfaceDest, Xpitch, cocoSrc, USState32->ScanLines);
82878289
}
82888290
else
82898291
for (HorzBeam=0;HorzBeam<BytesperRow;HorzBeam+=2) //1bbp Stretch=2
@@ -10115,27 +10117,27 @@ unsigned char SetColorInvert(unsigned char Tmp)
1011510117
*/
1011610118

1011710119
// Render even/odd 2x2 ntsc pixels
10118-
void RenderNTSCPixel2x2(unsigned int* surfaceDest, int XpitchDest, char colorIndex, char scanLines)
10120+
void RenderNTSCPixel2x2(Surface32 surface32, size_t surfaceDest, int XpitchDest, char colorIndex, char scanLines)
1011910121
{
1012010122
colorIndex <<= 1;
1012110123
unsigned int colorEven = ArtifactsNTSC[ColorInvert][ArtifactsNTSCIndex[colorIndex]];
1012210124
unsigned int colorOdd = ArtifactsNTSC[ColorInvert][ArtifactsNTSCIndex[colorIndex + 1]];
1012310125
// render even pixel
10124-
*surfaceDest = colorEven;
10125-
*(surfaceDest + 1) = colorEven;
10126-
if (!scanLines) *(surfaceDest + XpitchDest) = colorEven;
10127-
if (!scanLines) *(surfaceDest + XpitchDest + 1) = colorEven;
10126+
surface32[surfaceDest] = colorEven;
10127+
surface32[surfaceDest + 1] = colorEven;
10128+
if (!scanLines) surface32[surfaceDest + XpitchDest] = colorEven;
10129+
if (!scanLines) surface32[surfaceDest + XpitchDest + 1] = colorEven;
1012810130
surfaceDest += 2;
1012910131
// render odd pixel
10130-
*surfaceDest = colorOdd;
10131-
*(surfaceDest + 1) = colorOdd;
10132+
surface32[surfaceDest] = colorOdd;
10133+
surface32[surfaceDest + 1] = colorOdd;
1013210134
if (scanLines) return;
10133-
*(surfaceDest + XpitchDest) = colorOdd;
10134-
*(surfaceDest + XpitchDest + 1) = colorOdd;
10135+
surface32[surfaceDest + XpitchDest] = colorOdd;
10136+
surface32[surfaceDest + XpitchDest + 1] = colorOdd;
1013510137
}
1013610138

1013710139
// Render one raster line to surface 1bbp Stretch=2
10138-
void RenderPMODE4NTSC(unsigned int* surfaceDest, int XpitchDest, const unsigned char* cocoSrc, char scanLines)
10140+
void RenderPMODE4NTSC(Surface32 surface32, size_t surfaceDest, int XpitchDest, const unsigned char* cocoSrc, char scanLines)
1013910141
{
1014010142
const char cocoBorderPixel = 3; // white
1014110143

@@ -10153,15 +10155,15 @@ void RenderPMODE4NTSC(unsigned int* surfaceDest, int XpitchDest, const unsigned
1015310155
// roll in next pixel
1015410156
bitPattern = ((bitPattern << 2) + nextCocoPixel) & 0x3F;
1015510157
cocoByte <<= 2;
10156-
RenderNTSCPixel2x2(surfaceDest, XpitchDest, bitPattern, scanLines);
10158+
RenderNTSCPixel2x2(surface32, surfaceDest, XpitchDest, bitPattern, scanLines);
1015710159
surfaceDest += 4;
1015810160
}
1015910161
}
1016010162

1016110163
// end of line pixels (ntsc bleed with border)
1016210164
bitPattern = ((bitPattern << 2) + cocoBorderPixel) & 0x3F;
10163-
RenderNTSCPixel2x2(surfaceDest, XpitchDest, bitPattern, scanLines);
10165+
RenderNTSCPixel2x2(surface32, surfaceDest, XpitchDest, bitPattern, scanLines);
1016410166
surfaceDest += 4;
1016510167
bitPattern = ((bitPattern << 2) + cocoBorderPixel) & 0x3F;
10166-
RenderNTSCPixel2x2(surfaceDest, XpitchDest, bitPattern, scanLines);
10168+
RenderNTSCPixel2x2(surface32, surfaceDest, XpitchDest, bitPattern, scanLines);
1016710169
}

0 commit comments

Comments
 (0)