Putting try { ... } catch(std::bad_alloc &) { ... } around recently introduced resize() calls

At the very least, I suppose this doesn't hurt; I'd just like to discuss how this should work in the long run.

Also, this blog entry sums up my opinion perfectly:  http://christiangarbin.blogspot.com/2013/05/the-futility-of-catching-stdbadalloc.html
This commit is contained in:
Nathan Woods 2016-09-25 07:30:03 -04:00
parent 5f3bb35b12
commit f83fce0c28
4 changed files with 9 additions and 4 deletions

View File

@ -208,7 +208,8 @@ static imgtoolerr_t imgtool_floppy_read_sector(imgtool_image *image, UINT32 trac
return imgtool_floppy_error(ferr);
// resize the buffer accordingly
buffer.resize(sector_size);
try { buffer.resize(sector_size); }
catch (std::bad_alloc& b) { return IMGTOOLERR_OUTOFMEMORY; }
// and read the sector
ferr = floppy_read_sector(imgtool_floppy(image), head, track, sector, 0, &buffer[0], sector_size);

View File

@ -1818,7 +1818,9 @@ static imgtoolerr_t amiga_image_read_sector(imgtool_image* img, UINT32 track, UI
static imgtoolerr_t amiga_image_read_sector(imgtool_image* img,
UINT32 track, UINT32 head, UINT32 sector, std::vector<UINT8> &buffer)
{
buffer.resize(BSIZE);
try { buffer.resize(BSIZE); }
catch (std::bad_alloc& b) { return IMGTOOLERR_OUTOFMEMORY; }
return amiga_image_read_sector(img, track, head, sector, &buffer[0], buffer.size());
}

View File

@ -346,7 +346,8 @@ static imgtoolerr_t pc_chd_image_readsector(imgtool_image *image, UINT32 track,
// get the sector size and resize the buffer
UINT32 sector_size = imghd_get_header(&info->hard_disk)->sectorbytes;
buffer.resize(sector_size);
try { buffer.resize(sector_size); }
catch (std::bad_alloc& b) { return IMGTOOLERR_OUTOFMEMORY; }
// read the data
return imghd_read(&info->hard_disk,

View File

@ -811,7 +811,8 @@ static imgtoolerr_t thom_read_sector(imgtool_image* img, UINT32 track,
return IMGTOOLERR_SEEKERROR;
// resize the buffer
buffer.resize(f->sector_size);
try { buffer.resize(f->sector_size); }
catch (std::bad_alloc& b) { return IMGTOOLERR_OUTOFMEMORY; }
memcpy( &buffer[0], thom_get_sector( f, head, track, sector ), f->sector_size);
return IMGTOOLERR_SUCCESS;