Skip to content

Commit 3e09e1c

Browse files
committed
Engine: fix potentially incorrect access of vector's data
Should always use data() member, because &vec[0] will fail if vector's size is zero.
1 parent 8258675 commit 3e09e1c

8 files changed

Lines changed: 27 additions & 27 deletions

File tree

Common/ac/spritefile.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ static bool CreateIndexedBitmap(const BitmapData &image, std::vector<uint8_t> &d
6969
const size_t dst_size = image.GetWidth() * image.GetHeight();
7070
dst_data.resize(dst_size);
7171
const uint8_t *src = image.GetData(), *src_end = src + src_size;
72-
uint8_t *dst = &dst_data[0], *dst_end = dst + dst_size;
72+
uint8_t *dst = dst_data.data(), *dst_end = dst + dst_size;
7373
pal_count = 0;
7474

7575
for (; src < src_end && dst < dst_end; src += src_bpp)
@@ -371,7 +371,7 @@ bool SpriteFile::LoadSpriteIndexFile(std::unique_ptr<Stream> &&fidx,
371371
}
372372
else // large file support
373373
{
374-
fidx->ReadArrayOfInt64(&spriteoffs[0], numsprits);
374+
fidx->ReadArrayOfInt64(spriteoffs.data(), numsprits);
375375
}
376376

377377
for (sprkey_t i = 0; i <= topmost_index; ++i)
@@ -499,7 +499,7 @@ HError SpriteFile::LoadSprite(sprkey_t index, PixelBuffer &sprite)
499499
default: assert(0); break;
500500
}
501501
indexed_buf.resize(w * h);
502-
im_data = ImBufferPtr(&indexed_buf[0], indexed_buf.size(), 1);
502+
im_data = ImBufferPtr(indexed_buf.data(), indexed_buf.size(), 1);
503503
}
504504
// (Optional) Decompress the image data into the temp buffer
505505
size_t in_data_size =
@@ -595,7 +595,7 @@ HError SpriteFile::LoadRawData(sprkey_t index, SpriteDatHeader &hdr, std::vector
595595
// Seek back and read all at once
596596
data.resize(data_size);
597597
_stream->Seek(data_pos, kSeekBegin);
598-
_stream->Read(&data[0], data_size);
598+
_stream->Read(data.data(), data_size);
599599

600600
_curPos = index + 1; // mark correct pos
601601
return HError::None();
@@ -698,7 +698,7 @@ HError SaveSpriteFile(std::unique_ptr<Stream>&& out,
698698
writer.WriteEmptySlot();
699699
continue;
700700
}
701-
writer.WriteRawData(hdr, &membuf[0], membuf.size());
701+
writer.WriteRawData(hdr, membuf.data(), membuf.size());
702702
}
703703
writer.Finalize();
704704

@@ -723,9 +723,9 @@ HError SaveSpriteIndex(const String &filename, const SpriteFileIndex &index)
723723
out->WriteInt32(index.GetCount());
724724
if (index.GetCount() > 0)
725725
{
726-
out->WriteArrayOfInt16(&index.Widths[0], index.Widths.size());
727-
out->WriteArrayOfInt16(&index.Heights[0], index.Heights.size());
728-
out->WriteArrayOfInt64(&index.Offsets[0], index.Offsets.size());
726+
out->WriteArrayOfInt16(index.Widths.data(), index.Widths.size());
727+
out->WriteArrayOfInt16(index.Heights.data(), index.Heights.size());
728+
out->WriteArrayOfInt64(index.Offsets.data(), index.Offsets.size());
729729
}
730730
return HError::None();
731731
}
@@ -784,7 +784,7 @@ void SpriteFileWriter::WriteBitmap(const BitmapData &image)
784784
{ // Test the resulting size, and switch if the paletted image is less
785785
if (im_data.Size > (indexed_buf.size() + gen_pal_count * bpp))
786786
{
787-
im_data = ImBufferCPtr(&indexed_buf[0], indexed_buf.size(), 1);
787+
im_data = ImBufferCPtr(indexed_buf.data(), indexed_buf.size(), 1);
788788
sformat = PaletteFormatForBPP(bpp);
789789
pal_count = gen_pal_count;
790790
}
@@ -810,7 +810,7 @@ void SpriteFileWriter::WriteBitmap(const BitmapData &image)
810810
default: assert(!"Unsupported compression type!"); result = false; break;
811811
}
812812
// mark to write as a plain byte array
813-
im_data = result ? ImBufferCPtr(&_membuf[0], _membuf.size(), 1) : ImBufferCPtr();
813+
im_data = result ? ImBufferCPtr(_membuf.data(), _membuf.size(), 1) : ImBufferCPtr();
814814
}
815815

816816
// Write the final data

Common/util/compress.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ void skip_rle_bitmap8(Stream *in)
340340
// Unpack the pixels into temp buf
341341
std::vector<uint8_t> buf;
342342
buf.resize(w * h);
343-
cunpackbitl(&buf[0], w * h, in);
343+
cunpackbitl(buf.data(), w * h, in);
344344
// Skip RGB palette
345345
in->Seek(3 * 256);
346346
}

Common/util/string_utils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ size_t StrUtil::ConvertUtf8ToAscii(const char *mbstr, const char *loc_name, char
413413
wcsbuf[at] = static_cast<wchar_t>(r);
414414
}
415415
// Then convert widestring to single-byte string using specified locale
416-
size_t res_sz = wcstombs(out_cstr, &wcsbuf[0], out_sz);
416+
size_t res_sz = wcstombs(out_cstr, wcsbuf.data(), out_sz);
417417
setlocale(LC_CTYPE, old_locale);
418418
if (res_sz == static_cast<std::size_t>(-1))
419419
{

Engine/ac/room.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ void save_room_data_segment()
296296
croom->tsdatasize = globaldata.size();
297297
if (croom->tsdatasize > 0) {
298298
croom->tsdata.resize(croom->tsdatasize);
299-
memcpy(croom->tsdata.data(),&globaldata[0],croom->tsdatasize);
299+
memcpy(croom->tsdata.data(), globaldata.data(), croom->tsdatasize);
300300
}
301301

302302
}

Engine/ac/translation.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ bool init_translation(const String &lang, const String &fallback_lang)
194194
for (const auto &item : trans.Dict)
195195
{
196196
ascii.resize(item.first.GetLength() + 1); // ascii len will be <= utf-8 len
197-
StrUtil::ConvertUtf8ToAscii(item.first.GetCStr(), key_enc.GetCStr(), &ascii[0], ascii.size());
198-
conv_map.insert(std::make_pair(&ascii[0], item.second));
197+
StrUtil::ConvertUtf8ToAscii(item.first.GetCStr(), key_enc.GetCStr(), ascii.data(), ascii.size());
198+
conv_map.insert(std::make_pair(ascii.data(), item.second));
199199
}
200200
trans.Dict = conv_map;
201201
}

Engine/game/game_init.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,13 @@ void RegisterStaticArrays(GameSetupStruct &game)
261261
StaticInventoryArray.Create(&ccDynamicInv, sizeof(ScriptInvItem), sizeof(ScriptInvItem));
262262
StaticDialogArray.Create(&ccDynamicDialog, sizeof(ScriptDialog), sizeof(ScriptDialog));
263263

264-
ccAddExternalStaticArray("character",&game.chars[0], &StaticCharacterArray);
265-
ccAddExternalStaticArray("object",&scrObj[0], &StaticObjectArray);
266-
ccAddExternalStaticArray("gui",&scrGui[0], &StaticGUIArray);
267-
ccAddExternalStaticArray("hotspot",&scrHotspot[0], &StaticHotspotArray);
268-
ccAddExternalStaticArray("region",&scrRegion[0], &StaticRegionArray);
269-
ccAddExternalStaticArray("inventory",&scrInv[0], &StaticInventoryArray);
270-
ccAddExternalStaticArray("dialog", &scrDialog[0], &StaticDialogArray);
264+
ccAddExternalStaticArray("character", game.chars.data(), &StaticCharacterArray);
265+
ccAddExternalStaticArray("object", &scrObj[0], &StaticObjectArray);
266+
ccAddExternalStaticArray("gui", scrGui.data(), &StaticGUIArray);
267+
ccAddExternalStaticArray("hotspot", &scrHotspot[0], &StaticHotspotArray);
268+
ccAddExternalStaticArray("region", &scrRegion[0], &StaticRegionArray);
269+
ccAddExternalStaticArray("inventory", &scrInv[0], &StaticInventoryArray);
270+
ccAddExternalStaticArray("dialog", scrDialog.data(), &StaticDialogArray);
271271
}
272272

273273
// Initializes various game entities and registers them in the script system

Engine/gfx/ali3dogl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,16 +669,16 @@ void OGLGraphicsDriver::OutputShaderError(GLuint obj_id, const String &obj_name,
669669
if (log_len > 0)
670670
{
671671
if (is_shader)
672-
glGetShaderInfoLog(obj_id, log_len, &log_len, &errorLog[0]);
672+
glGetShaderInfoLog(obj_id, log_len, &log_len, errorLog.data());
673673
else
674-
glGetProgramInfoLog(obj_id, log_len, &log_len, &errorLog[0]);
674+
glGetProgramInfoLog(obj_id, log_len, &log_len, errorLog.data());
675675
}
676676

677677
Debug::Printf(kDbgMsg_Error, "ERROR: OpenGL: %s %s:", obj_name.GetCStr(), is_shader ? "failed to compile" : "failed to link");
678678
if (errorLog.size() > 0)
679679
{
680680
Debug::Printf(kDbgMsg_Error, "----------------------------------------");
681-
Debug::Printf(kDbgMsg_Error, "%s", &errorLog[0]);
681+
Debug::Printf(kDbgMsg_Error, "%s", errorLog.data());
682682
Debug::Printf(kDbgMsg_Error, "----------------------------------------");
683683
}
684684
else

Engine/media/audio/sdldecoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ const void *SDLResampler::Convert(const void *data, size_t sz, size_t &out_sz)
4343
size_t len = sz * _cvt.len_mult;
4444
_buf.resize(len);
4545
}
46-
_cvt.buf = &_buf[0];
46+
_cvt.buf = _buf.data();
4747
_cvt.len = _cvt.len_cvt = sz;
4848
SDL_memcpy(_cvt.buf, data, sz);
4949
if (SDL_ConvertAudio(&_cvt) < 0)
5050
return nullptr;
5151
out_sz = _cvt.len_cvt;
52-
return &_buf[0];
52+
return _buf.data();
5353
}
5454

5555
//-----------------------------------------------------------------------------

0 commit comments

Comments
 (0)