On 03/12/11 06:21, Nathan Caldwell wrote:
Macroify sanity checks and check return value of allocs.
---
  libavcodec/aacenc.c |   37 ++++++++++++++++++-------------------
  1 files changed, 18 insertions(+), 19 deletions(-)

diff --git a/libavcodec/aacenc.c b/libavcodec/aacenc.c
index b112fa8..4f80609 100644
--- a/libavcodec/aacenc.c
+++ b/libavcodec/aacenc.c
@@ -46,6 +46,12 @@

  #define AAC_MAX_CHANNELS 6

+#define RETURN_ON_ERROR( cond, ... ) \
+if (cond) { \
+    av_log(avctx, AV_LOG_ERROR, __VA_ARGS__); \
+    return AVERROR(EINVAL); \
+}
+
  static const uint8_t swb_size_1024_96[] = {
      4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 8, 8, 8, 8,
      12, 12, 12, 12, 12, 16, 16, 24, 28, 36, 44,
@@ -173,22 +179,12 @@ static av_cold int aac_encode_init(AVCodecContext *avctx)
      for (i = 0; i<  16; i++)
          if (avctx->sample_rate == avpriv_mpeg4audio_sample_rates[i])
              break;
-    if (i == 16) {
-        av_log(avctx, AV_LOG_ERROR, "Unsupported sample rate %d\n", 
avctx->sample_rate);
-        return -1;
-    }
-    if (avctx->channels>  AAC_MAX_CHANNELS) {
-        av_log(avctx, AV_LOG_ERROR, "Unsupported number of channels: %d\n", 
avctx->channels);
-        return -1;
-    }
-    if (avctx->profile != FF_PROFILE_UNKNOWN&&  avctx->profile != 
FF_PROFILE_AAC_LOW) {
-        av_log(avctx, AV_LOG_ERROR, "Unsupported profile %d\n", 
avctx->profile);
-        return -1;
-    }
-    if (1024.0 * avctx->bit_rate / avctx->sample_rate>  6144 * 
avctx->channels) {
-        av_log(avctx, AV_LOG_ERROR, "Too many bits per frame requested\n");
-        return -1;
-    }
+
+    RETURN_ON_ERROR(i == 16, "Unsupported sample rate %d\n", 
avctx->sample_rate);
+    RETURN_ON_ERROR(avctx->channels>  AAC_MAX_CHANNELS, "Unsupported number of 
channels: %d\n", avctx->channels);
+    RETURN_ON_ERROR(avctx->profile != FF_PROFILE_UNKNOWN&&  avctx->profile != 
FF_PROFILE_AAC_LOW, "Unsupported profile %d\n", avctx->profile);
+    RETURN_ON_ERROR(1024.0 * avctx->bit_rate / avctx->sample_rate>  6144 * 
avctx->channels, "Too many bits per frame requested\n");

might be nicer with the message in a second line.

+    FF_ALLOC_OR_GOTO (avctx, s->samples, 2 * 1024 * avctx->channels * 
sizeof(s->samples[0]), fail);
+    FF_ALLOCZ_OR_GOTO(avctx, s->cpe, sizeof(ChannelElement) * s->chan_map[0], 
fail);
+    FF_ALLOCZ_OR_GOTO(avctx, avctx->extradata, 5 + 
FF_INPUT_BUFFER_PADDING_SIZE, fail);

...

      return 0;
+fail:
+    return AVERROR(ENOMEM);
  }

What happens if the second or the third fail and the other 1 or 2 succeed ?

lu

--

Luca Barbato
Gentoo/linux
http://dev.gentoo.org/~lu_zero

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to