[FFmpeg-devel] [PATCH] Check malloc values in swscale.
Reimar Döffinger
Reimar.Doeffinger
Sat Aug 15 18:30:38 CEST 2009
On Sat, Aug 15, 2009 at 12:03:32PM -0300, Ramiro Polla wrote:
> Good idea. Patch attached.
I assume that is only the first step and you'll add checks for the
sws_allocVec return value later?
> +SwsVector *sws_allocVec(int length)
> +{
> + SwsVector *vec = av_malloc(sizeof(SwsVector));
> + double *coeff = av_malloc(sizeof(double) * length);
> + if (!coeff | !vec) {
> + av_free(coeff);
> + av_free(vec);
> + return NULL;
> + }
> + vec->length = length;
> + vec->coeff = coeff;
> + return vec;
> +}
I think it would be nicer like this (goto would probably be a bit overkill):
SwsVector *vec = av_malloc(sizeof(*vec));
if (!vec) return NULL;
vec->length = length;
vec->coeff = av_malloc(length * sizeof(*vec->coeff));
if (!vec->coeff)
av_freep(&vec);
return vec;
And there is more to do here anyway:
> SwsVector *sws_getConstVec(double c, int length){
> int i;
> - double *coeff= av_malloc(length*sizeof(double));
> - SwsVector *vec= av_malloc(sizeof(SwsVector));
> -
> - vec->coeff= coeff;
> - vec->length= length;
> + SwsVector *vec= sws_allocVec(length);
>
> for (i=0; i<length; i++)
> - coeff[i]= c;
> + vec->coeff[i]= c;
Possible optimization: use memset if c == 0
> static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
> static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
> static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
> static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
Should all use
sws_getConstVec(0, length);
instead of reinventing it.
> - for (i=0; i<a->length; i++) coeff[i]= a->coeff[i];
> + for (i=0; i<a->length; i++) vec->coeff[i]= a->coeff[i];
memcpy.
More information about the ffmpeg-devel
mailing list