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

Commit 079077fd authored by George Burgess IV's avatar George Burgess IV
Browse files

CursorWindow_test: fix leaks

`buf` isn't freed here. Convert it to either a unique_ptr or stack
array, depending on size.

Caught by the static analyzer:
> frameworks/base/libs/androidfw/tests/CursorWindow_test.cpp:190:5:
warning: Potential leak of memory pointed to by 'buf'
[clang-analyzer-unix.Malloc]
> frameworks/base/libs/androidfw/tests/CursorWindow_test.cpp:268:5:
warning: Potential leak of memory pointed to by 'buf'
[clang-analyzer-unix.Malloc]
> frameworks/base/libs/androidfw/tests/CursorWindow_test.cpp:327:5:
warning: Potential leak of memory pointed to by 'buf'
[clang-analyzer-unix.Malloc]

Bug: 206470603
Test: TreeHugger
Change-Id: I6fee4a9c2eedce0385f794567b0083df24c63419
parent 72154d30
Loading
Loading
Loading
Loading
+5 −3
Original line number Original line Diff line number Diff line
@@ -14,6 +14,7 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


#include <memory>
#include <utility>
#include <utility>


#include "androidfw/CursorWindow.h"
#include "androidfw/CursorWindow.h"
@@ -184,7 +185,7 @@ TEST(CursorWindowTest, Inflate) {
    ASSERT_EQ(w->allocRow(), OK);
    ASSERT_EQ(w->allocRow(), OK);


    // Scratch buffer that will fit before inflation
    // Scratch buffer that will fit before inflation
    void* buf = malloc(kHalfInlineSize);
    char buf[kHalfInlineSize];


    // Store simple value
    // Store simple value
    ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
    ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
@@ -262,7 +263,7 @@ TEST(CursorWindowTest, ParcelSmall) {
    ASSERT_EQ(w->allocRow(), OK);
    ASSERT_EQ(w->allocRow(), OK);


    // Scratch buffer that will fit before inflation
    // Scratch buffer that will fit before inflation
    void* buf = malloc(kHalfInlineSize);
    char buf[kHalfInlineSize];


    // Store simple value
    // Store simple value
    ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
    ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
@@ -322,7 +323,8 @@ TEST(CursorWindowTest, ParcelLarge) {
    ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);
    ASSERT_EQ(w->putLong(0, 0, 0xcafe), OK);


    // Store object that forces inflation
    // Store object that forces inflation
    void* buf = malloc(kGiantSize);
    std::unique_ptr<char> bufPtr(new char[kGiantSize]);
    void* buf = bufPtr.get();
    memset(buf, 42, kGiantSize);
    memset(buf, 42, kGiantSize);
    ASSERT_EQ(w->putBlob(0, 1, buf, kGiantSize), OK);
    ASSERT_EQ(w->putBlob(0, 1, buf, kGiantSize), OK);