Merge pull request #38378 from lawnjelly/kessel_ios_particle

Fix uninitialized memory in CPUParticles2D
This commit is contained in:
Rémi Verschelde 2020-05-01 15:17:06 +02:00 committed by GitHub
commit 8426ed2650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 16 deletions

View File

@ -53,9 +53,11 @@ void CPUParticles2D::set_amount(int p_amount) {
{ {
PoolVector<Particle>::Write w = particles.write(); PoolVector<Particle>::Write w = particles.write();
for (int i = 0; i < p_amount; i++) { // each particle must be set to false
w[i].active = false; // zeroing the data also prevents uninitialized memory being sent to GPU
} zeromem(static_cast<void *>(&w[0]), p_amount * sizeof(Particle));
// cast to prevent compiler warning .. note this relies on Particle not containing any complex types.
// an alternative is to use some zero method per item but the generated code will be far less efficient.
} }
particle_data.resize((8 + 4 + 1) * p_amount); particle_data.resize((8 + 4 + 1) * p_amount);
@ -1020,22 +1022,22 @@ void CPUParticles2D::_update_particle_data_buffer() {
ptr[6] = 0; ptr[6] = 0;
ptr[7] = t.elements[2][1]; ptr[7] = t.elements[2][1];
Color c = r[idx].color;
uint8_t *data8 = (uint8_t *)&ptr[8];
data8[0] = CLAMP(c.r * 255.0, 0, 255);
data8[1] = CLAMP(c.g * 255.0, 0, 255);
data8[2] = CLAMP(c.b * 255.0, 0, 255);
data8[3] = CLAMP(c.a * 255.0, 0, 255);
ptr[9] = r[idx].custom[0];
ptr[10] = r[idx].custom[1];
ptr[11] = r[idx].custom[2];
ptr[12] = r[idx].custom[3];
} else { } else {
zeromem(ptr, sizeof(float) * 8); zeromem(ptr, sizeof(float) * 13);
} }
Color c = r[idx].color;
uint8_t *data8 = (uint8_t *)&ptr[8];
data8[0] = CLAMP(c.r * 255.0, 0, 255);
data8[1] = CLAMP(c.g * 255.0, 0, 255);
data8[2] = CLAMP(c.b * 255.0, 0, 255);
data8[3] = CLAMP(c.a * 255.0, 0, 255);
ptr[9] = r[idx].custom[0];
ptr[10] = r[idx].custom[1];
ptr[11] = r[idx].custom[2];
ptr[12] = r[idx].custom[3];
ptr += 13; ptr += 13;
} }
} }

View File

@ -81,6 +81,8 @@ public:
private: private:
bool emitting; bool emitting;
// warning - beware of adding non-trivial types
// to this structure as it is zeroed to initialize in set_amount()
struct Particle { struct Particle {
Transform2D transform; Transform2D transform;
Color color; Color color;