Skip to content

Commit 00c423b

Browse files
committed
spearmint: Fix reading past end of lightmap image
Causes crash on OS X.
1 parent b294dbc commit 00c423b

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

code/renderergl1/tr_bsp.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ R_ColorShiftLightingBytes
113113
114114
===============
115115
*/
116-
static void R_ColorShiftLightingBytes( byte in[4], byte out[4] ) {
116+
static void R_ColorShiftLightingBytes( int inSize, byte in[4], byte out[4] ) {
117117
int shift, r, g, b;
118118

119119
// shift the color data based on overbright range
@@ -138,7 +138,11 @@ static void R_ColorShiftLightingBytes( byte in[4], byte out[4] ) {
138138
out[0] = r;
139139
out[1] = g;
140140
out[2] = b;
141-
out[3] = in[3];
141+
if ( inSize == 4 ) {
142+
out[3] = in[3];
143+
} else {
144+
out[3] = 255;
145+
}
142146
}
143147

144148
/*
@@ -192,8 +196,7 @@ float R_ProcessLightmap( byte **pic, int in_padding, int width, int height, byte
192196
}
193197
} else {
194198
for ( j = 0 ; j < width * height; j++ ) {
195-
R_ColorShiftLightingBytes( &( *pic )[j * in_padding], &( *pic_out )[j * 4] );
196-
( *pic_out )[j * 4 + 3] = 255;
199+
R_ColorShiftLightingBytes( 3, &( *pic )[j * in_padding], &( *pic_out )[j * 4] );
197200
}
198201
}
199202

@@ -492,7 +495,7 @@ static void ParseMesh( dsurface_t *ds, drawVert_t *verts, msurface_t *surf ) {
492495
points[i].st[j] = LittleFloat( verts[i].st[j] );
493496
points[i].lightmap[j] = LittleFloat( verts[i].lightmap[j] );
494497
}
495-
R_ColorShiftLightingBytes( verts[i].color, points[i].color );
498+
R_ColorShiftLightingBytes( 4, verts[i].color, points[i].color );
496499
}
497500

498501
// pre-tesseleate
@@ -582,7 +585,7 @@ static void ParseTriSurf( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, i
582585
tri->verts[i].lightmap[j] = LittleFloat( verts[i].lightmap[j] );
583586
}
584587

585-
R_ColorShiftLightingBytes( verts[i].color, tri->verts[i].color );
588+
R_ColorShiftLightingBytes( 4, verts[i].color, tri->verts[i].color );
586589
}
587590

588591
// copy indexes
@@ -723,7 +726,7 @@ static void ParseFoliage( dsurface_t *ds, drawVert_t *verts, msurface_t *surf, i
723726
AddPointToBounds( boundsTranslated[ 1 ], foliage->bounds[ 0 ], foliage->bounds[ 1 ] );
724727

725728
// copy color
726-
R_ColorShiftLightingBytes( verts[ i ].color, foliage->instances[ i ].color );
729+
R_ColorShiftLightingBytes( 4, verts[ i ].color, foliage->instances[ i ].color );
727730
}
728731

729732
// finish surface
@@ -1987,8 +1990,8 @@ void R_LoadLightGrid( const bspFile_t *bsp ) {
19871990

19881991
// deal with overbright bits
19891992
for ( i = 0 ; i < bsp->numGridPoints ; i++ ) {
1990-
R_ColorShiftLightingBytes( &w->lightGridData[i*8], &w->lightGridData[i*8] );
1991-
R_ColorShiftLightingBytes( &w->lightGridData[i*8+3], &w->lightGridData[i*8+3] );
1993+
R_ColorShiftLightingBytes( 4, &w->lightGridData[i*8], &w->lightGridData[i*8] );
1994+
R_ColorShiftLightingBytes( 4, &w->lightGridData[i*8+3], &w->lightGridData[i*8+3] );
19921995
}
19931996
}
19941997

code/renderergl2/tr_bsp.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ R_ColorShiftLightingBytes
117117
118118
===============
119119
*/
120-
static void R_ColorShiftLightingBytes( byte in[4], byte out[4] ) {
120+
static void R_ColorShiftLightingBytes( int inSize, byte in[4], byte out[4] ) {
121121
int shift, r, g, b;
122122

123123
// shift the color data based on overbright range
@@ -142,7 +142,11 @@ static void R_ColorShiftLightingBytes( byte in[4], byte out[4] ) {
142142
out[0] = r;
143143
out[1] = g;
144144
out[2] = b;
145-
out[3] = in[3];
145+
if ( inSize == 4 ) {
146+
out[3] = in[3];
147+
} else {
148+
out[3] = 255;
149+
}
146150
}
147151

148152

@@ -312,8 +316,7 @@ float R_ProcessLightmap( byte **pic, int in_padding, int width, int height, byte
312316
}
313317
else
314318
{
315-
R_ColorShiftLightingBytes( &( *pic )[j*in_padding], &( *pic_out )[j*4] );
316-
( *pic_out )[j*4+3] = 255;
319+
R_ColorShiftLightingBytes( 3, &( *pic )[j*in_padding], &( *pic_out )[j*4] );
317320
}
318321
}
319322
}
@@ -3137,8 +3140,8 @@ void R_LoadLightGrid( const bspFile_t *bsp ) {
31373140

31383141
// deal with overbright bits
31393142
for ( i = 0 ; i < bsp->numGridPoints ; i++ ) {
3140-
R_ColorShiftLightingBytes( &w->lightGridData[i*8], &w->lightGridData[i*8] );
3141-
R_ColorShiftLightingBytes( &w->lightGridData[i*8+3], &w->lightGridData[i*8+3] );
3143+
R_ColorShiftLightingBytes( 4, &w->lightGridData[i*8], &w->lightGridData[i*8] );
3144+
R_ColorShiftLightingBytes( 4, &w->lightGridData[i*8+3], &w->lightGridData[i*8+3] );
31423145
}
31433146

31443147
// load hdr lightgrid

0 commit comments

Comments
 (0)