Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5efcd68b authored by Chih-hung Hsieh's avatar Chih-hung Hsieh Committed by Automerger Merge Worker
Browse files

Merge "Fix potential memory leaks" am: 5c9c13b7

parents e822a2c3 5c9c13b7
Loading
Loading
Loading
Loading
+12 −21
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <utils/Log.h>

#include <audio_utils/sndfile.h>
#include <memory>
#include <stdio.h>

#include "pvamrwbdecoder.h"
@@ -121,7 +122,7 @@ int32_t AmrwbDecoderTest::DecodeFrames(int16_t *decoderCookie, void *decoderBuf,

TEST_F(AmrwbDecoderTest, MultiCreateAmrwbDecoderTest) {
    uint32_t memRequirements = pvDecoder_AmrWbMemRequirements();
    void *decoderBuf = malloc(memRequirements);
    std::unique_ptr<char[]> decoderBuf(new char[memRequirements]);
    ASSERT_NE(decoderBuf, nullptr)
            << "Failed to allocate decoder memory of size " << memRequirements;

@@ -129,25 +130,21 @@ TEST_F(AmrwbDecoderTest, MultiCreateAmrwbDecoderTest) {
    void *amrHandle = nullptr;
    int16_t *decoderCookie;
    for (int count = 0; count < kMaxCount; count++) {
        pvDecoder_AmrWb_Init(&amrHandle, decoderBuf, &decoderCookie);
        pvDecoder_AmrWb_Init(&amrHandle, decoderBuf.get(), &decoderCookie);
        ASSERT_NE(amrHandle, nullptr) << "Failed to initialize decoder";
        ALOGV("Decoder created successfully");
    }
    if (decoderBuf) {
        free(decoderBuf);
        decoderBuf = nullptr;
    }
}

TEST_P(AmrwbDecoderTest, DecodeTest) {
    uint32_t memRequirements = pvDecoder_AmrWbMemRequirements();
    void *decoderBuf = malloc(memRequirements);
    std::unique_ptr<char[]> decoderBuf(new char[memRequirements]);
    ASSERT_NE(decoderBuf, nullptr)
            << "Failed to allocate decoder memory of size " << memRequirements;

    void *amrHandle = nullptr;
    int16_t *decoderCookie;
    pvDecoder_AmrWb_Init(&amrHandle, decoderBuf, &decoderCookie);
    pvDecoder_AmrWb_Init(&amrHandle, decoderBuf.get(), &decoderCookie);
    ASSERT_NE(amrHandle, nullptr) << "Failed to initialize decoder";

    string inputFile = gEnv->getRes() + GetParam();
@@ -159,25 +156,21 @@ TEST_P(AmrwbDecoderTest, DecodeTest) {
    SNDFILE *outFileHandle = openOutputFile(&sfInfo);
    ASSERT_NE(outFileHandle, nullptr) << "Error opening output file for writing decoded output";

    int32_t decoderErr = DecodeFrames(decoderCookie, decoderBuf, outFileHandle);
    int32_t decoderErr = DecodeFrames(decoderCookie, decoderBuf.get(), outFileHandle);
    ASSERT_EQ(decoderErr, 0) << "DecodeFrames returned error";

    sf_close(outFileHandle);
    if (decoderBuf) {
        free(decoderBuf);
        decoderBuf = nullptr;
    }
}

TEST_P(AmrwbDecoderTest, ResetDecoderTest) {
    uint32_t memRequirements = pvDecoder_AmrWbMemRequirements();
    void *decoderBuf = malloc(memRequirements);
    std::unique_ptr<char[]> decoderBuf(new char[memRequirements]);
    ASSERT_NE(decoderBuf, nullptr)
            << "Failed to allocate decoder memory of size " << memRequirements;

    void *amrHandle = nullptr;
    int16_t *decoderCookie;
    pvDecoder_AmrWb_Init(&amrHandle, decoderBuf, &decoderCookie);
    pvDecoder_AmrWb_Init(&amrHandle, decoderBuf.get(), &decoderCookie);
    ASSERT_NE(amrHandle, nullptr) << "Failed to initialize decoder";

    string inputFile = gEnv->getRes() + GetParam();
@@ -190,20 +183,18 @@ TEST_P(AmrwbDecoderTest, ResetDecoderTest) {
    ASSERT_NE(outFileHandle, nullptr) << "Error opening output file for writing decoded output";

    // Decode 150 frames first
    int32_t decoderErr = DecodeFrames(decoderCookie, decoderBuf, outFileHandle, kNumFrameReset);
    int32_t decoderErr =
            DecodeFrames(decoderCookie, decoderBuf.get(), outFileHandle, kNumFrameReset);
    ASSERT_EQ(decoderErr, 0) << "DecodeFrames returned error";

    // Reset Decoder
    pvDecoder_AmrWb_Reset(decoderBuf, 1);
    pvDecoder_AmrWb_Reset(decoderBuf.get(), 1);

    // Start decoding again
    decoderErr = DecodeFrames(decoderCookie, decoderBuf, outFileHandle);
    decoderErr = DecodeFrames(decoderCookie, decoderBuf.get(), outFileHandle);
    ASSERT_EQ(decoderErr, 0) << "DecodeFrames returned error";

    sf_close(outFileHandle);
    if (decoderBuf) {
        free(decoderBuf);
    }
}

INSTANTIATE_TEST_SUITE_P(AmrwbDecoderTestAll, AmrwbDecoderTest,
+11 −22
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@
#include <utils/Log.h>

#include <audio_utils/sndfile.h>
#include <memory>
#include <stdio.h>

#include "mp3reader.h"
@@ -99,27 +100,23 @@ SNDFILE *Mp3DecoderTest::openOutputFile(SF_INFO *sfInfo) {
TEST_F(Mp3DecoderTest, MultiCreateMp3DecoderTest) {
    size_t memRequirements = pvmp3_decoderMemRequirements();
    ASSERT_NE(memRequirements, 0) << "Failed to get the memory requirement size";
    void *decoderBuf = malloc(memRequirements);
    unique_ptr<char[]> decoderBuf(new char[memRequirements]);
    ASSERT_NE(decoderBuf, nullptr)
            << "Failed to allocate decoder memory of size " << memRequirements;
    for (int count = 0; count < kMaxCount; count++) {
        pvmp3_InitDecoder(mConfig, decoderBuf);
        pvmp3_InitDecoder(mConfig, (void*)decoderBuf.get());
        ALOGV("Decoder created successfully");
    }
    if (decoderBuf) {
        free(decoderBuf);
        decoderBuf = nullptr;
    }
}

TEST_P(Mp3DecoderTest, DecodeTest) {
    size_t memRequirements = pvmp3_decoderMemRequirements();
    ASSERT_NE(memRequirements, 0) << "Failed to get the memory requirement size";
    void *decoderBuf = malloc(memRequirements);
    unique_ptr<char[]> decoderBuf(new char[memRequirements]);
    ASSERT_NE(decoderBuf, nullptr)
            << "Failed to allocate decoder memory of size " << memRequirements;

    pvmp3_InitDecoder(mConfig, decoderBuf);
    pvmp3_InitDecoder(mConfig, (void*)decoderBuf.get());
    ALOGV("Decoder created successfully");
    string inputFile = gEnv->getRes() + GetParam();
    bool status = mMp3Reader.init(inputFile.c_str());
@@ -130,27 +127,23 @@ TEST_P(Mp3DecoderTest, DecodeTest) {
    SNDFILE *outFileHandle = openOutputFile(&sfInfo);
    ASSERT_NE(outFileHandle, nullptr) << "Error opening output file for writing decoded output";

    ERROR_CODE decoderErr = DecodeFrames(decoderBuf, outFileHandle, sfInfo);
    ERROR_CODE decoderErr = DecodeFrames((void*)decoderBuf.get(), outFileHandle, sfInfo);
    ASSERT_EQ(decoderErr, NO_DECODING_ERROR) << "Failed to decode the frames";
    ASSERT_EQ(sfInfo.channels, mConfig->num_channels) << "Number of channels does not match";
    ASSERT_EQ(sfInfo.samplerate, mConfig->samplingRate) << "Sample rate does not match";

    mMp3Reader.close();
    sf_close(outFileHandle);
    if (decoderBuf) {
        free(decoderBuf);
        decoderBuf = nullptr;
    }
}

TEST_P(Mp3DecoderTest, ResetDecoderTest) {
    size_t memRequirements = pvmp3_decoderMemRequirements();
    ASSERT_NE(memRequirements, 0) << "Failed to get the memory requirement size";
    void *decoderBuf = malloc(memRequirements);
    unique_ptr<char[]> decoderBuf(new char[memRequirements]);
    ASSERT_NE(decoderBuf, nullptr)
            << "Failed to allocate decoder memory of size " << memRequirements;

    pvmp3_InitDecoder(mConfig, decoderBuf);
    pvmp3_InitDecoder(mConfig, (void*)decoderBuf.get());
    ALOGV("Decoder created successfully.");
    string inputFile = gEnv->getRes() + GetParam();
    bool status = mMp3Reader.init(inputFile.c_str());
@@ -162,24 +155,20 @@ TEST_P(Mp3DecoderTest, ResetDecoderTest) {
    ASSERT_NE(outFileHandle, nullptr) << "Error opening output file for writing decoded output";

    ERROR_CODE decoderErr;
    decoderErr = DecodeFrames(decoderBuf, outFileHandle, sfInfo, kNumFrameReset);
    decoderErr = DecodeFrames((void*)decoderBuf.get(), outFileHandle, sfInfo, kNumFrameReset);
    ASSERT_EQ(decoderErr, NO_DECODING_ERROR) << "Failed to decode the frames";
    ASSERT_EQ(sfInfo.channels, mConfig->num_channels) << "Number of channels does not match";
    ASSERT_EQ(sfInfo.samplerate, mConfig->samplingRate) << "Sample rate does not match";

    pvmp3_resetDecoder(decoderBuf);
    pvmp3_resetDecoder((void*)decoderBuf.get());
    // Decode the same file.
    decoderErr = DecodeFrames(decoderBuf, outFileHandle, sfInfo);
    decoderErr = DecodeFrames((void*)decoderBuf.get(), outFileHandle, sfInfo);
    ASSERT_EQ(decoderErr, NO_DECODING_ERROR) << "Failed to decode the frames";
    ASSERT_EQ(sfInfo.channels, mConfig->num_channels) << "Number of channels does not match";
    ASSERT_EQ(sfInfo.samplerate, mConfig->samplingRate) << "Sample rate does not match";

    mMp3Reader.close();
    sf_close(outFileHandle);
    if (decoderBuf) {
        free(decoderBuf);
        decoderBuf = nullptr;
    }
}

INSTANTIATE_TEST_SUITE_P(Mp3DecoderTestAll, Mp3DecoderTest,
+5 −6
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <memory>

#include <media/esds/ESDS.h>
#include <binder/ProcessState.h>
@@ -121,18 +122,16 @@ class ESDSUnitTest : public ::testing::TestWithParam<tuple<
};

TEST_P(ESDSUnitTest, InvalidDataTest) {
    void *invalidData = calloc(mESDSSize, 1);
    std::unique_ptr<char[]> invalidData(new char[mESDSSize]());
    ASSERT_NE(invalidData, nullptr) << "Unable to allocate memory";
    ESDS esds(invalidData, mESDSSize);
    free(invalidData);
    ESDS esds((void*)invalidData.get(), mESDSSize);
    ASSERT_NE(esds.InitCheck(), OK) << "invalid ESDS data accepted";
}

TEST(ESDSSanityUnitTest, ConstructorSanityTest) {
    void *invalidData = malloc(1);
    std::unique_ptr<char[]> invalidData(new char[1]());
    ASSERT_NE(invalidData, nullptr) << "Unable to allocate memory";
    ESDS esds_zero(invalidData, 0);
    free(invalidData);
    ESDS esds_zero((void*)invalidData.get(), 0);
    ASSERT_NE(esds_zero.InitCheck(), OK) << "invalid ESDS data accepted";

    ESDS esds_null(NULL, 0);
+20 −21
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <utils/Log.h>

#include <fstream>
#include <memory>

#include "media/stagefright/foundation/ABitReader.h"
#include "media/stagefright/foundation/avc_utils.h"
@@ -151,10 +152,10 @@ TEST_P(VOLDimensionTest, DimensionTest) {
    size_t fileSize = buf.st_size;
    ASSERT_NE(fileSize, 0) << "Invalid file size found";

    const uint8_t *volBuffer = new uint8_t[fileSize];
    std::unique_ptr<uint8_t[]> volBuffer(new uint8_t[fileSize]);
    ASSERT_NE(volBuffer, nullptr) << "Failed to allocate VOL buffer of size: " << fileSize;

    inputFileStream.read((char *)(volBuffer), fileSize);
    inputFileStream.read((char *)(volBuffer.get()), fileSize);
    ASSERT_EQ(inputFileStream.gcount(), fileSize)
            << "Failed to read complete file, bytes read: " << inputFileStream.gcount();

@@ -163,15 +164,13 @@ TEST_P(VOLDimensionTest, DimensionTest) {
    int32_t volWidth = -1;
    int32_t volHeight = -1;

    bool status = ExtractDimensionsFromVOLHeader(volBuffer, fileSize, &volWidth, &volHeight);
    bool status = ExtractDimensionsFromVOLHeader(volBuffer.get(), fileSize, &volWidth, &volHeight);
    ASSERT_TRUE(status)
            << "Failed to get VOL dimensions from function: ExtractDimensionsFromVOLHeader()";

    ASSERT_EQ(volWidth, width) << "Expected width: " << width << "Found: " << volWidth;

    ASSERT_EQ(volHeight, height) << "Expected height: " << height << "Found: " << volHeight;

    delete[] volBuffer;
}

TEST_P(AVCDimensionTest, DimensionTest) {
@@ -186,7 +185,8 @@ TEST_P(AVCDimensionTest, DimensionTest) {
        stringLine >> type >> chunkLength;
        ASSERT_GT(chunkLength, 0) << "Length of the data chunk must be greater than zero";

        const uint8_t *data = new uint8_t[chunkLength];
        std::unique_ptr<uint8_t[]> dataArray(new uint8_t[chunkLength]);
        const uint8_t *data = dataArray.get();
        ASSERT_NE(data, nullptr) << "Failed to create a data buffer of size: " << chunkLength;

        const uint8_t *nalStart;
@@ -197,9 +197,11 @@ TEST_P(AVCDimensionTest, DimensionTest) {
                << "Failed to read complete file, bytes read: " << mInputFileStream.gcount();

        size_t smallBufferSize = kSmallBufferSize;
        const uint8_t *sanityData = new uint8_t[smallBufferSize];
        uint8_t sanityDataBuffer[smallBufferSize];
        const uint8_t *sanityData = sanityDataBuffer;
        memcpy((void *)sanityData, (void *)data, smallBufferSize);

        // sanityData could be changed, but sanityDataPtr is not and can be cleaned up.
        status_t result = getNextNALUnit(&sanityData, &smallBufferSize, &nalStart, &nalSize, true);
        ASSERT_EQ(result, -EAGAIN) << "Invalid result found when wrong NAL unit passed";

@@ -221,7 +223,6 @@ TEST_P(AVCDimensionTest, DimensionTest) {
            ASSERT_EQ(avcHeight, mFrameHeight)
                    << "Expected height: " << mFrameHeight << "Found: " << avcHeight;
        }
        delete[] data;
    }
    if (mNalUnitsExpected < 0) {
        ASSERT_GT(numNalUnits, 0) << "Failed to find an NAL Unit";
@@ -251,7 +252,8 @@ TEST_P(AvccBoxTest, AvccBoxValidationTest) {
        accessUnitLength += chunkLength;

        if (!type.compare("SPS")) {
            const uint8_t *data = new uint8_t[chunkLength];
            std::unique_ptr<uint8_t[]> dataArray(new uint8_t[chunkLength]);
            const uint8_t *data = dataArray.get();
            ASSERT_NE(data, nullptr) << "Failed to create a data buffer of size: " << chunkLength;

            const uint8_t *nalStart;
@@ -271,14 +273,13 @@ TEST_P(AvccBoxTest, AvccBoxValidationTest) {
                profile = nalStart[1];
                level = nalStart[3];
            }
            delete[] data;
        }
    }
    const uint8_t *accessUnitData = new uint8_t[accessUnitLength];
    std::unique_ptr<uint8_t[]> accessUnitData(new uint8_t[accessUnitLength]);
    ASSERT_NE(accessUnitData, nullptr) << "Failed to create a buffer of size: " << accessUnitLength;

    mInputFileStream.seekg(0, ios::beg);
    mInputFileStream.read((char *)accessUnitData, accessUnitLength);
    mInputFileStream.read((char *)accessUnitData.get(), accessUnitLength);
    ASSERT_EQ(mInputFileStream.gcount(), accessUnitLength)
            << "Failed to read complete file, bytes read: " << mInputFileStream.gcount();

@@ -286,7 +287,7 @@ TEST_P(AvccBoxTest, AvccBoxValidationTest) {
    ASSERT_NE(accessUnit, nullptr)
            << "Failed to create an android data buffer of size: " << accessUnitLength;

    memcpy(accessUnit->data(), accessUnitData, accessUnitLength);
    memcpy(accessUnit->data(), accessUnitData.get(), accessUnitLength);
    sp<ABuffer> csdDataBuffer = MakeAVCCodecSpecificData(accessUnit, &avcWidth, &avcHeight);
    ASSERT_NE(csdDataBuffer, nullptr) << "No data returned from MakeAVCCodecSpecificData()";

@@ -306,7 +307,6 @@ TEST_P(AvccBoxTest, AvccBoxValidationTest) {
    ASSERT_EQ(*(csdData + 3), level)
            << "Expected AVC level: " << level << " found: " << *(csdData + 3);
    csdDataBuffer.clear();
    delete[] accessUnitData;
    accessUnit.clear();
}

@@ -321,32 +321,31 @@ TEST_P(AVCFrameTest, FrameTest) {
        stringLine >> type >> chunkLength >> frameLayerID;
        ASSERT_GT(chunkLength, 0) << "Length of the data chunk must be greater than zero";

        char *data = new char[chunkLength];
        std::unique_ptr<char[]> data(new char[chunkLength]);
        ASSERT_NE(data, nullptr) << "Failed to allocation data buffer of size: " << chunkLength;

        mInputFileStream.read(data, chunkLength);
        mInputFileStream.read(data.get(), chunkLength);
        ASSERT_EQ(mInputFileStream.gcount(), chunkLength)
                << "Failed to read complete file, bytes read: " << mInputFileStream.gcount();

        if (!type.compare("IDR")) {
            bool isIDR = IsIDR((uint8_t *)data, chunkLength);
            bool isIDR = IsIDR((uint8_t *)data.get(), chunkLength);
            ASSERT_TRUE(isIDR);

            layerID = FindAVCLayerId((uint8_t *)data, chunkLength);
            layerID = FindAVCLayerId((uint8_t *)data.get(), chunkLength);
            ASSERT_EQ(layerID, frameLayerID) << "Wrong layer ID found";
        } else if (!type.compare("P") || !type.compare("B")) {
            sp<ABuffer> accessUnit = new ABuffer(chunkLength);
            ASSERT_NE(accessUnit, nullptr) << "Unable to create access Unit";

            memcpy(accessUnit->data(), data, chunkLength);
            memcpy(accessUnit->data(), data.get(), chunkLength);
            bool isReferenceFrame = IsAVCReferenceFrame(accessUnit);
            ASSERT_TRUE(isReferenceFrame);

            accessUnit.clear();
            layerID = FindAVCLayerId((uint8_t *)data, chunkLength);
            layerID = FindAVCLayerId((uint8_t *)data.get(), chunkLength);
            ASSERT_EQ(layerID, frameLayerID) << "Wrong layer ID found";
        }
        delete[] data;
    }
}

+7 −16
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@
#include <string.h>
#include <sys/stat.h>
#include <fstream>
#include <memory>

#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MetaDataBase.h>
@@ -48,18 +49,16 @@ namespace android {
class MetaDataBaseUnitTest : public ::testing::Test {};

TEST_F(MetaDataBaseUnitTest, CreateMetaDataBaseTest) {
    MetaDataBase *metaData = new MetaDataBase();
    std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
    ASSERT_NE(metaData, nullptr) << "Failed to create meta data";

    // Testing copy constructor
    MetaDataBase *metaDataCopy = metaData;
    MetaDataBase *metaDataCopy = metaData.get();
    ASSERT_NE(metaDataCopy, nullptr) << "Failed to create meta data copy";

    delete metaData;
}

TEST_F(MetaDataBaseUnitTest, SetAndFindDataTest) {
    MetaDataBase *metaData = new MetaDataBase();
    std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
    ASSERT_NE(metaData, nullptr) << "Failed to create meta data";

    // Setting the different key-value pair type for first time, overwrite
@@ -142,12 +141,10 @@ TEST_F(MetaDataBaseUnitTest, SetAndFindDataTest) {
    int32_t angle;
    status = metaData->findInt32(kKeyRotation, &angle);
    ASSERT_FALSE(status) << "Value for an invalid key is returned when the key is not set";

    delete (metaData);
}

TEST_F(MetaDataBaseUnitTest, OverWriteFunctionalityTest) {
    MetaDataBase *metaData = new MetaDataBase();
    std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
    ASSERT_NE(metaData, nullptr) << "Failed to create meta data";

    // set/set/read to check first overwrite operation
@@ -186,12 +183,10 @@ TEST_F(MetaDataBaseUnitTest, OverWriteFunctionalityTest) {
    status = metaData->findInt32(kKeyHeight, &height);
    ASSERT_TRUE(status) << "kKeyHeight key does not exists in metadata";
    ASSERT_EQ(height, kHeight3) << "Value of height is not overwritten";

    delete (metaData);
}

TEST_F(MetaDataBaseUnitTest, RemoveKeyTest) {
    MetaDataBase *metaData = new MetaDataBase();
    std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
    ASSERT_NE(metaData, nullptr) << "Failed to create meta data";

    bool status = metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_AVC);
@@ -246,12 +241,10 @@ TEST_F(MetaDataBaseUnitTest, RemoveKeyTest) {

    metaData->setCString(kKeyMIMEType, MEDIA_MIMETYPE_VIDEO_HEVC);
    ASSERT_FALSE(status) << "Overwrite should be false since the metadata was cleared";

    delete (metaData);
}

TEST_F(MetaDataBaseUnitTest, ConvertToStringTest) {
    MetaDataBase *metaData = new MetaDataBase();
    std::unique_ptr<MetaDataBase> metaData(new MetaDataBase());
    ASSERT_NE(metaData, nullptr) << "Failed to create meta data";

    String8 info = metaData->toString();
@@ -281,8 +274,6 @@ TEST_F(MetaDataBaseUnitTest, ConvertToStringTest) {
    info = metaData->toString();
    ASSERT_EQ(info.length(), 0) << "MetaData length is non-zero after clearing it: "
                                << info.length();

    delete (metaData);
}

}  // namespace android
Loading