Commit 010e9c0a authored by conrad's avatar conrad
Browse files

libfishsound: handle allocation failure due to out of memory

towards Mozilla bug 468293

git-svn-id: http://svn.annodex.net/libfishsound/trunk@3849 8158c8cd-e7e1-0310-9fa4-c5954c97daef
parent 95183f0f
......@@ -505,7 +505,7 @@ int fish_sound_command (FishSound * fsound, int command, void * data,
* \param fsound A FishSound* handle
* \retval 0 \a fsound uses non-interleaved PCM
* \retval 1 \a fsound uses interleaved PCM
* \retval -1 Invalid \a fsound
* \retval -1 Invalid \a fsound, or out of memory.
*/
int fish_sound_get_interleave (FishSound * fsound);
......
......@@ -46,6 +46,7 @@ fs_strdup (const char * s)
char * ret;
if (s == NULL) return NULL;
ret = fs_malloc (strlen(s) + 1);
if (ret == NULL) return NULL;
return strcpy (ret, s);
}
......@@ -56,6 +57,7 @@ fs_strdup_len (const char * s, int len)
if (s == NULL) return NULL;
if (len == 0) return NULL;
ret = fs_malloc (len + 1);
if (ret == NULL) return NULL;
if (strncpy (ret, s, len) == NULL) {
fs_free (ret);
return NULL;
......@@ -79,11 +81,6 @@ fs_index_len (const char * s, char c, int len)
return NULL;
}
#if 0
static void comment_init(char **comments, int* length, char *vendor_string);
static void comment_add(char **comments, int* length, char *tag, char *val);
#endif
/*
Comments will be stored in the Vorbis style.
It is describled in the "Structure" section of
......@@ -114,47 +111,6 @@ The comment header is decoded as follows:
buf[base]=(val)&0xff; \
}while(0)
#if 0
static void
comment_init(char **comments, int* length, char *vendor_string)
{
int vendor_length=strlen(vendor_string);
int user_comment_list_length=0;
int len=4+vendor_length+4;
char *p=(char*)fs_malloc(len);
if(p==NULL){
}
writeint(p, 0, vendor_length);
memcpy(p+4, vendor_string, vendor_length);
writeint(p, 4+vendor_length, user_comment_list_length);
*length=len;
*comments=p;
}
static void
comment_add(char **comments, int* length, char *tag, char *val)
{
char* p=*comments;
int vendor_length=readint(p, 0);
int user_comment_list_length=readint(p, 4+vendor_length);
int tag_len=(tag?strlen(tag):0);
int val_len=strlen(val);
int len=(*length)+4+tag_len+val_len;
p=(char*)fs_realloc(p, len);
if(p==NULL){
}
writeint(p, *length, tag_len+val_len); /* length of comment */
if(tag) memcpy(p+*length+4, tag, tag_len); /* comment */
memcpy(p+*length+4+tag_len, val, val_len); /* comment */
writeint(p, 4+vendor_length, user_comment_list_length+1);
*comments=p;
*length=len;
}
#endif
static int
fs_comment_validate_byname (const char * name, const char * value)
{
......@@ -184,6 +140,8 @@ fs_comment_new (const char * name, const char * value)
if (!fs_comment_validate_byname (name, value)) return NULL;
comment = fs_malloc (sizeof (FishSoundComment));
if (comment == NULL) return NULL;
comment->name = fs_strdup (name);
comment->value = fs_strdup (value);
......
......@@ -70,7 +70,7 @@ fish_sound_set_format (FishSound * fsound, int format)
}
if (fsound->codec && fsound->codec->init)
fsound->codec->init (fsound);
if (fsound->codec->init (fsound) == NULL) return -1;
fsound->info.format = format;
......@@ -105,6 +105,7 @@ fish_sound_new (int mode, FishSoundInfo * fsinfo)
}
fsound = fs_malloc (sizeof (FishSound));
if (fsound == NULL) return NULL;
fsound->mode = mode;
fsound->interleave = 0;
......
......@@ -369,7 +369,7 @@ fs_flac_enc_write_callback(const FLAC__StreamEncoder *encoder,
printf("fs_flac_enc_write_callback: generating FLAC header packet: "
"%c%c%c%c\n", buffer[0], buffer[1], buffer[2], buffer[3]);
#endif
fi->buffer = (unsigned char*)malloc(sizeof(unsigned char)*(bytes+9));
fi->buffer = (unsigned char*)fs_malloc(sizeof(unsigned char)*(bytes+9));
fi->buffer[0] = 0x7f;
fi->buffer[1] = 0x46; /* 'F' */
fi->buffer[2] = 0x4c; /* 'L' */
......@@ -387,7 +387,7 @@ fs_flac_enc_write_callback(const FLAC__StreamEncoder *encoder,
/* Make a temporary copy of the metadata header to pass to the user
* callback.
*/
unsigned char* tmp = (unsigned char*)malloc(sizeof(unsigned char)*(bytes+fi->bufferlength));
unsigned char* tmp = (unsigned char*)fs_malloc(sizeof(unsigned char)*(bytes+fi->bufferlength));
memcpy (tmp, fi->buffer, fi->bufferlength);
memcpy (tmp+fi->bufferlength, buffer, bytes);
fs_free(fi->buffer);
......@@ -771,6 +771,7 @@ fish_sound_flac_codec (void)
FishSoundCodec * codec;
codec = (FishSoundCodec *) fs_malloc (sizeof (FishSoundCodec));
if (codec == NULL) return NULL;
codec->format.format = FISH_SOUND_FLAC;
codec->format.name = "Flac (Xiph.Org)";
......
......@@ -58,6 +58,7 @@ fs_vector_new (FishSoundCmpFunc cmp)
FishSoundVector * vector;
vector = fs_malloc (sizeof (FishSoundVector));
if (vector == NULL) return NULL;
vector->max_elements = 0;
vector->nr_elements = 0;
......@@ -176,6 +177,8 @@ fs_vector_grow (FishSoundVector * vector)
void *
fs_vector_insert (FishSoundVector * vector, void * data)
{
if (vector == NULL) return NULL;
if (fs_vector_grow (vector) == NULL)
return NULL;
......
......@@ -371,8 +371,8 @@ fs_speex_enc_headers (FishSound * fsound)
int modeID;
SpeexMode * mode = NULL;
SpeexHeader header;
unsigned char * buf;
int bytes;
unsigned char * header_buf = NULL, * comments_buf = NULL;
int header_bytes, comments_bytes;
size_t buflen;
modeID = 1;
......@@ -392,24 +392,23 @@ fs_speex_enc_headers (FishSound * fsound)
fss->st = speex_encoder_init (mode);
if (fsound->callback.encoded) {
FishSoundEncoded encoded = (FishSoundEncoded)fsound->callback.encoded;
char vendor_string[128];
/* header */
buf = (unsigned char *) speex_header_to_packet (&header, &bytes);
encoded (fsound, buf, (long)bytes, fsound->user_data);
fss->packetno++;
fs_free (buf);
/* Allocate and create header */
header_buf = (unsigned char *) speex_header_to_packet (&header, &header_bytes);
if (header_buf == NULL) {
return NULL;
}
/* comments */
/* Allocate and create comments */
snprintf (vendor_string, 128, VENDOR_FORMAT, header.speex_version);
fish_sound_comment_set_vendor (fsound, vendor_string);
bytes = fish_sound_comments_encode (fsound, NULL, 0);
buf = fs_malloc (bytes);
bytes = fish_sound_comments_encode (fsound, buf, bytes);
encoded (fsound, buf, (long)bytes, fsound->user_data);
fss->packetno++;
fs_free (buf);
comments_bytes = fish_sound_comments_encode (fsound, NULL, 0);
comments_buf = fs_malloc (comments_bytes);
if (comments_buf == NULL) {
fs_free (header_buf);
return NULL;
}
}
speex_encoder_ctl (fss->st, SPEEX_SET_SAMPLING_RATE,
......@@ -421,12 +420,33 @@ fs_speex_enc_headers (FishSound * fsound)
printf ("got frame size %d\n", fss->frame_size);
#endif
/* XXX: blah blah blah ... set VBR etc. */
/* XXX: set VBR etc. */
buflen = fss->frame_size * fsound->info.channels * sizeof (float);
fss->ipcm = fs_malloc (buflen);
if (fss->ipcm == NULL) {
if (comments_buf) fs_free (comments_buf);
if (header_buf) fs_free (header_buf);
return NULL;
}
memset (fss->ipcm, 0, buflen);
/* Allocations succeeded, actually call encoded callback for headers */
if (fsound->callback.encoded) {
FishSoundEncoded encoded = (FishSoundEncoded)fsound->callback.encoded;
/* header */
encoded (fsound, header_buf, (long)header_bytes, fsound->user_data);
fss->packetno++;
fs_free (header_buf);
/* comments */
comments_bytes = fish_sound_comments_encode (fsound, comments_buf, comments_bytes);
encoded (fsound, comments_buf, (long)comments_bytes, fsound->user_data);
fss->packetno++;
fs_free (comments_buf);
}
return fsound;
}
......@@ -604,10 +624,13 @@ fs_speex_update (FishSound * fsound, int interleave)
{
FishSoundSpeexInfo * fss = (FishSoundSpeexInfo *)fsound->codec_data;
size_t pcm_size = sizeof (float);
float *ipcm_new, *pcm0, *pcm1;
ipcm_new = (float *)fs_realloc (fss->ipcm,
pcm_size * fss->frame_size * fsound->info.channels);
if (ipcm_new == NULL) return -1;
fss->ipcm = (float *)
fs_realloc (fss->ipcm,
pcm_size * fss->frame_size * fsound->info.channels);
fss->ipcm = ipcm_new;
if (interleave) {
/* if transitioning from non-interleave to interleave,
......@@ -622,8 +645,19 @@ fs_speex_update (FishSound * fsound, int interleave)
if (fsound->info.channels == 1) {
fss->pcm[0] = (float *) fss->ipcm;
} else if (fsound->info.channels == 2) {
fss->pcm[0] = fs_realloc (fss->pcm[0], pcm_size * fss->frame_size);
fss->pcm[1] = fs_realloc (fss->pcm[1], pcm_size * fss->frame_size);
pcm0 = fs_realloc (fss->pcm[0], pcm_size * fss->frame_size);
if (pcm0 == NULL) {
return -1;
}
pcm1 = fs_realloc (fss->pcm[1], pcm_size * fss->frame_size);
if (pcm1 == NULL) {
fs_free (pcm0);
return -1;
}
fss->pcm[0] = pcm0;
fss->pcm[1] = pcm1;
}
}
......@@ -705,6 +739,7 @@ fish_sound_speex_codec (void)
FishSoundCodec * codec;
codec = (FishSoundCodec *) fs_malloc (sizeof (FishSoundCodec));
if (codec == NULL) return NULL;
codec->format.format = FISH_SOUND_SPEEX;
codec->format.name = "Speex (Xiph.Org)";
......
......@@ -113,6 +113,7 @@ fs_vorbis_decode (FishSound * fsound, unsigned char * buf, long bytes)
FishSoundVorbisInfo * fsv = (FishSoundVorbisInfo *)fsound->codec_data;
ogg_packet op;
long samples;
float ** pcm_new;
int ret;
/* Make an ogg_packet structure to pass the data to libvorbis */
......@@ -160,9 +161,15 @@ fs_vorbis_decode (FishSound * fsound, unsigned char * buf, long bytes)
if (fsound->interleave) {
if (samples > fsv->max_pcm) {
fsv->ipcm = realloc (fsv->ipcm, sizeof(float) * samples *
fsound->info.channels);
fsv->max_pcm = samples;
pcm_new = realloc (fsv->ipcm, sizeof(float) * samples *
fsound->info.channels);
if (pcm_new == NULL) {
/* Allocation failure; just truncate here, fail gracefully elsewhere */
samples = fsv->max_pcm;
} else {
fsv->ipcm = pcm_new;
fsv->max_pcm = samples;
}
}
_fs_interleave (fsv->pcm, (float **)fsv->ipcm, samples,
fsound->info.channels, 1.0);
......@@ -473,6 +480,7 @@ fish_sound_vorbis_codec (void)
FishSoundCodec * codec;
codec = (FishSoundCodec *) fs_malloc (sizeof (FishSoundCodec));
if (codec == NULL) return NULL;
codec->format.format = FISH_SOUND_VORBIS;
codec->format.name = "Vorbis (Xiph.Org)";
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment