vpx_codec_decode: check data size
When a valid data pointer is given make sure the size is greater than
zero.
A previous check for vp9 was incorrectly removed in:
7050074 Make the api behavior conform to api spec.
No semantics for valid pointers + 0-sized frames are defined for VPx
codecs, so move the check to vpx_codec_decode(). This avoids an assert
in vp9.
+ add some basic invalid param testing for decoder init/decode/destroy
Change-Id: I99f9cef6076d15874fd72ac973f2685d8a2353c3
diff --git a/test/decode_api_test.cc b/test/decode_api_test.cc
new file mode 100644
index 0000000..86097cd
--- /dev/null
+++ b/test/decode_api_test.cc
@@ -0,0 +1,59 @@
+/*
+ * Copyright (c) 2014 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+#include "third_party/googletest/src/include/gtest/gtest.h"
+
+#include "./vpx_config.h"
+#include "vpx/vp8dx.h"
+#include "vpx/vpx_decoder.h"
+
+namespace {
+
+#define NELEMENTS(x) static_cast<int>(sizeof(x) / sizeof(x[0]))
+
+TEST(DecodeAPI, InvalidParams) {
+ static const vpx_codec_iface_t *kCodecs[] = {
+#if CONFIG_VP8_DECODER
+ &vpx_codec_vp8_dx_algo,
+#endif
+#if CONFIG_VP9_DECODER
+ &vpx_codec_vp9_dx_algo,
+#endif
+ };
+ uint8_t buf[1] = {0};
+ vpx_codec_ctx_t dec;
+
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_dec_init(NULL, NULL, NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_dec_init(&dec, NULL, NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(NULL, NULL, 0, NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_decode(NULL, buf, 0, NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
+ vpx_codec_decode(NULL, buf, NELEMENTS(buf), NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
+ vpx_codec_decode(NULL, NULL, NELEMENTS(buf), NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM, vpx_codec_destroy(NULL));
+ EXPECT_TRUE(vpx_codec_error(NULL) != NULL);
+
+ for (int i = 0; i < NELEMENTS(kCodecs); ++i) {
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
+ vpx_codec_dec_init(NULL, kCodecs[i], NULL, 0));
+
+ EXPECT_EQ(VPX_CODEC_OK, vpx_codec_dec_init(&dec, kCodecs[i], NULL, 0));
+ EXPECT_EQ(VPX_CODEC_UNSUP_BITSTREAM,
+ vpx_codec_decode(&dec, buf, NELEMENTS(buf), NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
+ vpx_codec_decode(&dec, NULL, NELEMENTS(buf), NULL, 0));
+ EXPECT_EQ(VPX_CODEC_INVALID_PARAM,
+ vpx_codec_decode(&dec, buf, 0, NULL, 0));
+
+ EXPECT_EQ(VPX_CODEC_OK, vpx_codec_destroy(&dec));
+ }
+}
+
+} // namespace
diff --git a/test/test.mk b/test/test.mk
index 057e1e8..0814c2b 100644
--- a/test/test.mk
+++ b/test/test.mk
@@ -59,6 +59,7 @@
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += webm_video_source.h
endif
+LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += decode_api_test.cc
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += invalid_file_test.cc
LIBVPX_TEST_SRCS-$(CONFIG_DECODERS) += test_vector_test.cc
diff --git a/vpx/src/vpx_decoder.c b/vpx/src/vpx_decoder.c
index 276238b..a90fe95 100644
--- a/vpx/src/vpx_decoder.c
+++ b/vpx/src/vpx_decoder.c
@@ -119,7 +119,7 @@
/* Sanity checks */
/* NULL data ptr allowed if data_sz is 0 too */
- if (!ctx || (!data && data_sz))
+ if (!ctx || (!data && data_sz) || (data && !data_sz))
res = VPX_CODEC_INVALID_PARAM;
else if (!ctx->iface || !ctx->priv)
res = VPX_CODEC_ERROR;