Skip to content

Commit

Permalink
Prevented some out-of-bounds reading and writing of data
Browse files Browse the repository at this point in the history
  • Loading branch information
bil-bas committed Jan 19, 2013
1 parent c75c376 commit 06fa4e2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
8 changes: 5 additions & 3 deletions ext/ashton/fast_math.c
Expand Up @@ -12,7 +12,7 @@ void initialize_fast_math()
}

// Ensure the cardinal directions are 100% accurate.
for (int i = 0; i < 360; i += 90)
for (int i = 0; i <= 360; i += 90)
{
sin_lookup[i * LOOKUPS_PER_DEGREE] = sin(DEGREES_TO_RADIANS(i));
}
Expand All @@ -22,7 +22,9 @@ float fast_sin_deg(float degrees)
{
// Normalize to 0..360 (i.e. 0..2PI)
degrees = fmod(degrees, 360.0f);
if(degrees < 0) degrees += 360.0f;
if(degrees < 0.0f) degrees += 360.0f;

return sin_lookup[(int)(degrees * LOOKUPS_PER_DEGREE)];
int index = (int)(degrees * LOOKUPS_PER_DEGREE);

return sin_lookup[index % NUM_LOOKUP_VALUES];
}
5 changes: 4 additions & 1 deletion ext/ashton/fast_math.h
Expand Up @@ -11,7 +11,10 @@
#define DEGREES_TO_RADIANS(ANGLE) ((ANGLE - 90) * (M_PI / 180.0f))

#define LOOKUPS_PER_DEGREE 10
#define NUM_LOOKUP_VALUES (360 * LOOKUPS_PER_DEGREE)

// Enough for values from 0..360 inclusive
#define NUM_LOOKUP_VALUES ((360 + 1) * LOOKUPS_PER_DEGREE)

#define LOOKUP_PRECISION (1.0f / LOOKUPS_PER_DEGREE)

extern float sin_lookup[NUM_LOOKUP_VALUES];
Expand Down
70 changes: 41 additions & 29 deletions ext/ashton/particle_emitter.c
Expand Up @@ -476,44 +476,56 @@ static void update_vbo(ParticleEmitter* emitter)
// Ensure that drawing order is correct by drawing in order of creation...

// First, we draw all those from after the current, going up to the last one.
Particle* first = &emitter->particles[emitter->next_particle_index];
Particle* last = &emitter->particles[emitter->max_particles - 1];
if(color_changes(emitter))
uint num_particles_written;
if(emitter->next_particle_index != emitter->max_particles - 1)
{
write_colors_for_particles(emitter->color_array,
first, last);
Particle* first = &emitter->particles[emitter->next_particle_index];
Particle* last = &emitter->particles[emitter->max_particles - 1];
if(color_changes(emitter))
{
write_colors_for_particles(emitter->color_array,
first, last);
}
if(texture_changes(emitter))
{
write_texture_coords_for_particles(emitter->texture_coords_array,
first, last,
&emitter->texture_info);
}
num_particles_written = write_vertices_for_particles(emitter->vertex_array,
first, last,
emitter->width, emitter->height);
}
if(texture_changes(emitter))
else
{
write_texture_coords_for_particles(emitter->texture_coords_array,
first, last,
&emitter->texture_info);
num_particles_written = 0;
}
uint num_particles_written = write_vertices_for_particles(emitter->vertex_array,
first, last,
emitter->width, emitter->height);

// When we copy the second half of the particles, we want to start writing further on.
uint offset = num_particles_written * VERTICES_IN_PARTICLE;

// Then go from the first to the current.
first = emitter->particles;
last = &emitter->particles[emitter->next_particle_index - 1];
if(color_changes(emitter))
if (emitter->next_particle_index > 0)
{
write_colors_for_particles(&emitter->color_array[offset],
first, last);
}
Particle* first = &emitter->particles[0];
Particle* last = &emitter->particles[emitter->next_particle_index - 1];
if (color_changes(emitter))
{
write_colors_for_particles(&emitter->color_array[offset],
first, last);
}

if(texture_changes(emitter))
{
write_texture_coords_for_particles(&emitter->texture_coords_array[offset],
first, last,
&emitter->texture_info);
}
if (texture_changes(emitter))
{
write_texture_coords_for_particles(&emitter->texture_coords_array[offset],
first, last,
&emitter->texture_info);
}

write_vertices_for_particles(&emitter->vertex_array[offset],
first, last,
emitter->width, emitter->height);
write_vertices_for_particles(&emitter->vertex_array[offset],
first, last,
emitter->width, emitter->height);
}

// Upload the data, but only as much as we are actually using.
glBindBufferARB(GL_ARRAY_BUFFER_ARB, emitter->vbo_id);
Expand Down Expand Up @@ -738,8 +750,8 @@ static void update_particle(ParticleEmitter* emitter, Particle* particle,

// Die if out of time, invisible or shrunk to nothing.
if((particle->time_to_live <= 0) ||
(particle->color.alpha < 0) ||
(particle->scale < 0))
(particle->color.alpha <= 0) ||
(particle->scale <= 0))
{
particle->time_to_live = 0;
emitter->count -= 1;
Expand Down

0 comments on commit 06fa4e2

Please sign in to comment.