(svn r2897) Check the return values of [cm]alloc and the length of an array, plus some smaller changes

This commit is contained in:
tron 2005-08-27 20:54:48 +00:00
parent 175b2cd12a
commit abb034c2cd

72
sound.c
View File

@ -27,13 +27,21 @@ static FileEntry* _files;
static void OpenBankFile(const char *filename) static void OpenBankFile(const char *filename)
{ {
uint count, i; FileEntry* fe;
uint32 size, tag; uint count;
FileEntry *fe; uint i;
FioOpenFile(SOUND_SLOT, filename); FioOpenFile(SOUND_SLOT, filename);
_file_count = count = FioReadDword() >> 3; count = FioReadDword() / 8;
fe = _files = calloc(sizeof(FileEntry), count); fe = calloc(sizeof(*fe), count);
if (fe == NULL) {
_file_count = 0;
_files = NULL;
}
_file_count = count;
_files = fe;
FioSeekTo(0, SEEK_SET); FioSeekTo(0, SEEK_SET);
@ -54,8 +62,8 @@ static void OpenBankFile(const char *filename)
// Read riff tags // Read riff tags
for (;;) { for (;;) {
tag = FioReadDword(); uint32 tag = FioReadDword();
size = FioReadDword(); uint32 size = FioReadDword();
if (tag == ' tmf') { if (tag == ' tmf') {
FioReadWord(); // wFormatTag FioReadWord(); // wFormatTag
@ -91,14 +99,18 @@ static void OpenBankFile(const char *filename)
static bool SetBankSource(MixerChannel *mc, uint bank) static bool SetBankSource(MixerChannel *mc, uint bank)
{ {
FileEntry *fe = &_files[bank]; FileEntry* fe;
int8 *mem; int8* mem;
uint i; uint i;
if (fe->file_size == 0) if (bank >= _file_count) return false;
return false; fe = &_files[bank];
if (fe->file_size == 0) return false;
mem = malloc(fe->file_size);
if (mem == NULL) return false;
mem = malloc(fe->file_size); /* XXX unchecked malloc */
FioSeekToFile(fe->file_offset); FioSeekToFile(fe->file_offset);
FioReadBlock(mem, fe->file_size); FioReadBlock(mem, fe->file_size);
@ -121,15 +133,14 @@ bool SoundInitialize(const char *filename)
// Low level sound player // Low level sound player
static void StartSound(uint sound, uint panning, uint volume) static void StartSound(uint sound, uint panning, uint volume)
{ {
if (volume != 0) { MixerChannel* mc;
MixerChannel *mc = MxAllocateChannel(_mixer);
if (mc == NULL) if (volume == 0) return;
return; mc = MxAllocateChannel(_mixer);
if (SetBankSource(mc, sound)) { if (mc == NULL) return;
MxSetChannelVolume(mc, volume << 8, volume << 8); if (!SetBankSource(mc, sound)) return;
MxActivateChannel(mc); MxSetChannelVolume(mc, volume << 8, volume << 8);
} MxActivateChannel(mc);
}
} }
@ -149,12 +160,12 @@ static const byte _sound_base_vol[] = {
}; };
static const byte _sound_idx[] = { static const byte _sound_idx[] = {
2, 3, 4, 5, 6, 7, 8, 9, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20, 21, 22, 23, 24, 25, 18, 19, 20, 21, 22, 23, 24, 25,
26, 27, 28, 29, 30, 31, 32, 33, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 0, 34, 35, 36, 37, 38, 39, 40, 0,
1, 41, 42, 43, 44, 45, 46, 47, 1, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 48, 49, 50, 51, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 62, 63, 56, 57, 58, 59, 60, 61, 62, 63,
64, 65, 66, 67, 68, 69, 70, 71, 64, 65, 66, 67, 68, 69, 70, 71,
@ -163,19 +174,18 @@ static const byte _sound_idx[] = {
static void SndPlayScreenCoordFx(SoundFx sound, int x, int y) static void SndPlayScreenCoordFx(SoundFx sound, int x, int y)
{ {
Window *w; const Window* w;
ViewPort *vp;
int left;
if (msf.effect_vol == 0) if (msf.effect_vol == 0) return;
return;
for (w = _windows; w != _last_window; w++) { for (w = _windows; w != _last_window; w++) {
if ((vp = w->viewport) != NULL && const ViewPort* vp = w->viewport;
if (vp != NULL &&
IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) && IS_INSIDE_1D(x, vp->virtual_left, vp->virtual_width) &&
IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) { IS_INSIDE_1D(y, vp->virtual_top, vp->virtual_height)) {
int left = ((x - vp->virtual_left) >> vp->zoom) + vp->left;
left = ((x - vp->virtual_left) >> vp->zoom) + vp->left;
StartSound( StartSound(
_sound_idx[sound], _sound_idx[sound],
clamp(left / 71, 0, 8), clamp(left / 71, 0, 8),