std::vector::operator[index] with index >= size is UB

This commit is contained in:
Flyinghead 2023-06-04 12:48:50 +02:00
parent bc3c1ab628
commit ffc0a42c18
13 changed files with 50 additions and 43 deletions

View File

@ -308,6 +308,8 @@ public:
}
bool save(const std::string& filename)
{
if (page.empty())
return false;
for (u8& b : page)
b = 0xff - b;
stbi_write_png(filename.c_str(), printerWidth, lines, 1, &page[0], printerWidth);
@ -318,7 +320,7 @@ public:
{
ser << printerWidth;
ser << (u32)page.size();
ser.serialize(&page[0], page.size());
ser.serialize(page.data(), page.size());
ser << lines;
ser << penx;
ser << peny;
@ -342,11 +344,11 @@ public:
ser << cc.width;
ser << cc.height;
ser << (u32)cc.data.size();
ser.serialize(&cc.data[0], cc.data.size());
ser.serialize(cc.data.data(), cc.data.size());
}
ser << (u32)ruledLine.size();
ser.serialize(&ruledLine[0], ruledLine.size());
ser.serialize(ruledLine.data(), ruledLine.size());
ser << ruledLineMode;
ser << underline;
ser << maxUnderline;
@ -357,7 +359,7 @@ public:
u32 size;
deser >> size;
page.resize(size);
deser.deserialize(&page[0], page.size());
deser.deserialize(page.data(), page.size());
deser >> lines;
deser >> penx;
deser >> peny;
@ -383,12 +385,12 @@ public:
deser >> cc.height;
deser >> size;
cc.data.resize(size);
deser.deserialize(&cc.data[0], cc.data.size());
deser.deserialize(cc.data.data(), cc.data.size());
}
deser >> size;
ruledLine.resize(size);
deser.deserialize(&ruledLine[0], ruledLine.size());
deser.deserialize(ruledLine.data(), ruledLine.size());
deser >> ruledLineMode;
deser >> underline;
deser >> maxUnderline;
@ -655,7 +657,7 @@ public:
ser << expectedDataBytes;
ser << (u32)dataBytes.size();
ser.serialize(&dataBytes[0], dataBytes.size());
ser.serialize(dataBytes.data(), dataBytes.size());
ser << kanji;
ser << kanjiByte0;
@ -666,7 +668,7 @@ public:
ser << bm.width;
ser << bm.height;
ser << (u32)bm.data.size();
ser.serialize(&bm.data[0], bm.data.size());
ser.serialize(bm.data.data(), bm.data.size());
}
if (bitmapWriter == nullptr)
@ -688,7 +690,7 @@ public:
u32 size;
deser >> size;
dataBytes.resize(size);
deser.deserialize(&dataBytes[0], dataBytes.size());
deser.deserialize(dataBytes.data(), dataBytes.size());
deser >> kanji;
deser >> kanjiByte0;
@ -701,7 +703,7 @@ public:
deser >> bm.height;
deser >> size;
bm.data.resize(size);
deser.deserialize(&bm.data[0], bm.data.size());
deser.deserialize(bm.data.data(), bm.data.size());
}
bool b;

View File

@ -367,8 +367,10 @@ void fix_texture_bleeding(const std::vector<PolyParam>& polys, int first, int en
//
void makePrimRestartIndex(std::vector<PolyParam>& polys, int first, int end, bool merge, rend_context& ctx)
{
if (first >= (int)polys.size())
return;
PolyParam *last_poly = nullptr;
const PolyParam *end_poly = &polys[end];
const PolyParam *end_poly = polys.data() + end;
for (PolyParam *poly = &polys[first]; poly != end_poly; poly++)
{
int first_index;
@ -447,8 +449,10 @@ void makePrimRestartIndex(std::vector<PolyParam>& polys, int first, int end, boo
//
void makeIndex(std::vector<PolyParam>& polys, int first, int end, bool merge, rend_context& ctx)
{
if (first >= (int)polys.size())
return;
PolyParam *last_poly = nullptr;
const PolyParam *end_poly = &polys[end];
const PolyParam *end_poly = polys.data() + end;
bool cullingReversed = false;
for (PolyParam *poly = &polys[first]; poly != end_poly; poly++)
{

View File

@ -116,7 +116,7 @@ int post(const std::string& url, const std::vector<PostField>& fields)
return 500;
}
fclose(f);
content += std::string(&data[0], size);
content += std::string(data.data(), size);
}
content += "\r\n";
}

View File

@ -361,19 +361,19 @@ void DX11Renderer::uploadGeometryBuffers()
{
setFirstProvokingVertex(pvrrc);
size_t size = pvrrc.verts.size() * sizeof(decltype(pvrrc.verts[0]));
size_t size = pvrrc.verts.size() * sizeof(decltype(*pvrrc.verts.data()));
bool rc = ensureBufferSize(vertexBuffer, D3D11_BIND_VERTEX_BUFFER, vertexBufferSize, size);
verify(rc);
D3D11_MAPPED_SUBRESOURCE mappedSubres;
deviceContext->Map(vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedSubres);
memcpy(mappedSubres.pData, &pvrrc.verts[0], size);
memcpy(mappedSubres.pData, pvrrc.verts.data(), size);
deviceContext->Unmap(vertexBuffer, 0);
size = pvrrc.idx.size() * sizeof(decltype(pvrrc.idx[0]));
size = pvrrc.idx.size() * sizeof(decltype(*pvrrc.idx.data()));
rc = ensureBufferSize(indexBuffer, D3D11_BIND_INDEX_BUFFER, indexBufferSize, size);
verify(rc);
deviceContext->Map(indexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedSubres);
memcpy(mappedSubres.pData, &pvrrc.idx[0], size);
memcpy(mappedSubres.pData, pvrrc.idx.data(), size);
deviceContext->Unmap(indexBuffer, 0);
if (config::ModifierVolumes && !pvrrc.modtrig.empty())

View File

@ -539,7 +539,7 @@ struct DX11OITRenderer : public DX11Renderer
drawList<ListType_Opaque, false, DX11OITShaders::Depth>(pvrrc.global_param_op, previous_pass.op_count, op_count);
drawList<ListType_Punch_Through, false, DX11OITShaders::Depth>(pvrrc.global_param_pt, previous_pass.pt_count, pt_count);
drawModVols<false>(previous_pass.mvo_count, mvo_count, &pvrrc.global_param_mvo[0]);
drawModVols<false>(previous_pass.mvo_count, mvo_count, pvrrc.global_param_mvo.data());
//
// PASS 2: Render OP and PT to opaque render target
@ -575,9 +575,9 @@ struct DX11OITRenderer : public DX11Renderer
{
// Intel Iris Plus 640 just crashes
if (current_pass.mv_op_tr_shared)
drawModVols<true>(previous_pass.mvo_count, mvo_count, &pvrrc.global_param_mvo[0]);
drawModVols<true>(previous_pass.mvo_count, mvo_count, pvrrc.global_param_mvo.data());
else
drawModVols<true>(previous_pass.mvo_tr_count, current_pass.mvo_tr_count - previous_pass.mvo_tr_count, &pvrrc.global_param_mvo_tr[0]);
drawModVols<true>(previous_pass.mvo_tr_count, current_pass.mvo_tr_count - previous_pass.mvo_tr_count, pvrrc.global_param_mvo_tr.data());
}
}
else

View File

@ -1009,20 +1009,20 @@ bool D3DRenderer::Render()
v[1] = -1.f;
device->SetClipPlane(3, v);
size_t size = pvrrc.verts.size() * sizeof(decltype(pvrrc.verts[0]));
size_t size = pvrrc.verts.size() * sizeof(decltype(*pvrrc.verts.data()));
rc = ensureVertexBufferSize(vertexBuffer, vertexBufferSize, size);
verify(rc);
void *ptr;
rc = SUCCEEDED(vertexBuffer->Lock(0, size, &ptr, D3DLOCK_DISCARD));
verify(rc);
memcpy(ptr, &pvrrc.verts[0], size);
memcpy(ptr, pvrrc.verts.data(), size);
vertexBuffer->Unlock();
size = pvrrc.idx.size() * sizeof(decltype(pvrrc.idx[0]));
size = pvrrc.idx.size() * sizeof(decltype(*pvrrc.idx.data()));
rc = ensureIndexBufferSize(indexBuffer, indexBufferSize, size);
verify(rc);
rc = SUCCEEDED(indexBuffer->Lock(0, size, &ptr, D3DLOCK_DISCARD));
verify(rc);
memcpy(ptr, &pvrrc.idx[0], size);
memcpy(ptr, pvrrc.idx.data(), size);
indexBuffer->Unlock();
if (config::ModifierVolumes && !pvrrc.modtrig.empty())

View File

@ -875,12 +875,12 @@ bool OpenGL4Renderer::renderFrame(int width, int height)
{
//Main VBO
//move vertex to gpu
gl4.vbo.getVertexBuffer()->update(&pvrrc.verts[0], pvrrc.verts.size() * sizeof(decltype(pvrrc.verts[0])));
gl4.vbo.getIndexBuffer()->update(&pvrrc.idx[0], pvrrc.idx.size() * sizeof(decltype(pvrrc.idx[0])));
gl4.vbo.getVertexBuffer()->update(pvrrc.verts.data(), pvrrc.verts.size() * sizeof(decltype(*pvrrc.verts.data())));
gl4.vbo.getIndexBuffer()->update(pvrrc.idx.data(), pvrrc.idx.size() * sizeof(decltype(*pvrrc.idx.data())));
//Modvol VBO
if (!pvrrc.modtrig.empty())
gl4.vbo.getModVolBuffer()->update(&pvrrc.modtrig[0], pvrrc.modtrig.size() * sizeof(decltype(pvrrc.modtrig[0])));
gl4.vbo.getModVolBuffer()->update(pvrrc.modtrig.data(), pvrrc.modtrig.size() * sizeof(decltype(*pvrrc.modtrig.data())));
// TR PolyParam data
if (!pvrrc.global_param_tr.empty())

View File

@ -1142,10 +1142,10 @@ static void upload_vertex_indices()
short_idx.reserve(pvrrc.idx.size());
for (u32 i : pvrrc.idx)
short_idx.push_back(i);
gl.vbo.idxs->update(&short_idx[0], short_idx.size() * sizeof(u16));
gl.vbo.idxs->update(short_idx.data(), short_idx.size() * sizeof(u16));
}
else
gl.vbo.idxs->update(&pvrrc.idx[0], pvrrc.idx.size() * sizeof(decltype(pvrrc.idx[0])));
gl.vbo.idxs->update(pvrrc.idx.data(), pvrrc.idx.size() * sizeof(decltype(*pvrrc.idx.data())));
glCheck();
}

View File

@ -116,7 +116,8 @@ public:
void upload(BufferData& bufferData, u32 bufOffset = 0)
{
bufferData.upload(chunks.size(), &chunkSizes[0], &chunks[0], bufOffset);
if (!chunks.empty())
bufferData.upload(chunks.size(), &chunkSizes[0], &chunks[0], bufOffset);
}
vk::DeviceSize size() const {

View File

@ -262,7 +262,7 @@ void Drawer::DrawList(const vk::CommandBuffer& cmdBuffer, u32 listType, bool sor
{
if (first == last)
return;
const PolyParam *pp_end = &polys[last];
const PolyParam *pp_end = polys.data() + last;
for (const PolyParam *pp = &polys[first]; pp != pp_end; pp++)
if (pp->count > 2)
DrawPoly(cmdBuffer, listType, sortTriangles, *pp, pp->first, pp->count);
@ -328,11 +328,11 @@ void Drawer::UploadMainBuffer(const VertexShaderUniforms& vertexUniforms, const
BufferPacker packer;
// Vertex
packer.add(&pvrrc.verts[0], pvrrc.verts.size() * sizeof(decltype(pvrrc.verts[0])));
packer.add(pvrrc.verts.data(), pvrrc.verts.size() * sizeof(decltype(*pvrrc.verts.data())));
// Modifier Volumes
offsets.modVolOffset = packer.add(&pvrrc.modtrig[0], pvrrc.modtrig.size() * sizeof(decltype(pvrrc.modtrig[0])));
offsets.modVolOffset = packer.add(pvrrc.modtrig.data(), pvrrc.modtrig.size() * sizeof(decltype(*pvrrc.modtrig.data())));
// Index
offsets.indexOffset = packer.add(&pvrrc.idx[0], pvrrc.idx.size() * sizeof(decltype(pvrrc.idx[0])));
offsets.indexOffset = packer.add(pvrrc.idx.data(), pvrrc.idx.size() * sizeof(decltype(*pvrrc.idx.data())));
// Uniform buffers
offsets.vertexUniformOffset = packer.addUniform(&vertexUniforms, sizeof(vertexUniforms));
offsets.fragmentUniformOffset = packer.addUniform(&fragmentUniforms, sizeof(fragmentUniforms));

View File

@ -146,7 +146,7 @@ protected:
vk::DeviceSize offset = -1;
size_t n2LightSize = sizeof(N2LightModel) + align(sizeof(N2LightModel), GetContext()->GetUniformBufferAlignment());
if (n2LightSize == sizeof(N2LightModel))
if (n2LightSize == sizeof(N2LightModel) && !pvrrc.lightModels.empty())
{
offset = packer.addUniform(&pvrrc.lightModels[0], pvrrc.lightModels.size() * sizeof(decltype(pvrrc.lightModels[0])));
}

View File

@ -125,7 +125,7 @@ void OITDrawer::DrawList(const vk::CommandBuffer& cmdBuffer, u32 listType, bool
{
if (first == last)
return;
const PolyParam *pp_end = &polys[last];
const PolyParam *pp_end = polys.data() + last;
for (const PolyParam *pp = &polys[first]; pp != pp_end; pp++)
if (pp->count > 2)
DrawPoly(cmdBuffer, listType, sortTriangles, pass, *pp, pp->first, pp->count);
@ -216,11 +216,11 @@ void OITDrawer::UploadMainBuffer(const OITDescriptorSets::VertexShaderUniforms&
BufferPacker packer;
// Vertex
packer.add(&pvrrc.verts[0], pvrrc.verts.size() * sizeof(decltype(pvrrc.verts[0])));
packer.add(pvrrc.verts.data(), pvrrc.verts.size() * sizeof(decltype(*pvrrc.verts.data())));
// Modifier Volumes
offsets.modVolOffset = packer.add(&pvrrc.modtrig[0], pvrrc.modtrig.size() * sizeof(decltype(pvrrc.modtrig[0])));
offsets.modVolOffset = packer.add(pvrrc.modtrig.data(), pvrrc.modtrig.size() * sizeof(decltype(*pvrrc.modtrig.data())));
// Index
offsets.indexOffset = packer.add(&pvrrc.idx[0], pvrrc.idx.size() * sizeof(decltype(pvrrc.idx[0])));
offsets.indexOffset = packer.add(pvrrc.idx.data(), pvrrc.idx.size() * sizeof(decltype(*pvrrc.idx.data())));
// Uniform buffers
offsets.vertexUniformOffset = packer.addUniform(&vertexUniforms, sizeof(vertexUniforms));
offsets.fragmentUniformOffset = packer.addUniform(&fragmentUniforms, sizeof(fragmentUniforms));
@ -354,7 +354,7 @@ bool OITDrawer::Draw(const Texture *fogTexture, const Texture *paletteTexture)
DrawList(cmdBuffer, ListType_Opaque, false, Pass::Depth, pvrrc.global_param_op, previous_pass.op_count, current_pass.op_count);
DrawList(cmdBuffer, ListType_Punch_Through, false, Pass::Depth, pvrrc.global_param_pt, previous_pass.pt_count, current_pass.pt_count);
DrawModifierVolumes<false>(cmdBuffer, previous_pass.mvo_count, current_pass.mvo_count - previous_pass.mvo_count, &pvrrc.global_param_mvo[0]);
DrawModifierVolumes<false>(cmdBuffer, previous_pass.mvo_count, current_pass.mvo_count - previous_pass.mvo_count, pvrrc.global_param_mvo.data());
// Color subpass
cmdBuffer.nextSubpass(vk::SubpassContents::eInline);
@ -403,9 +403,9 @@ bool OITDrawer::Draw(const Texture *fogTexture, const Texture *paletteTexture)
if (GetContext()->GetVendorID() != VulkanContext::VENDOR_QUALCOMM) // Adreno bug
{
if (current_pass.mv_op_tr_shared)
DrawModifierVolumes<true>(cmdBuffer, previous_pass.mvo_count, current_pass.mvo_count - previous_pass.mvo_count, &pvrrc.global_param_mvo[0]);
DrawModifierVolumes<true>(cmdBuffer, previous_pass.mvo_count, current_pass.mvo_count - previous_pass.mvo_count, pvrrc.global_param_mvo.data());
else
DrawModifierVolumes<true>(cmdBuffer, previous_pass.mvo_tr_count, current_pass.mvo_tr_count - previous_pass.mvo_tr_count, &pvrrc.global_param_mvo_tr[0]);
DrawModifierVolumes<true>(cmdBuffer, previous_pass.mvo_tr_count, current_pass.mvo_tr_count - previous_pass.mvo_tr_count, pvrrc.global_param_mvo_tr.data());
}
vk::Pipeline pipeline = pipelineManager->GetFinalPipeline();

View File

@ -174,7 +174,7 @@ public:
template<typename T>
MD5Sum& add(const std::vector<T>& v) {
MD5_Update(&ctx, &v[0], (unsigned long)(v.size() * sizeof(T)));
MD5_Update(&ctx, v.data(), (unsigned long)(v.size() * sizeof(T)));
return *this;
}