File: | sal/qa/rtl/alloc/rtl_alloc.cxx |
Location: | line 48, column 17 |
Description: | Array access (from variable '_pMemory') results in a null pointer dereference |
1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ | |||||
2 | /************************************************************************* | |||||
3 | * | |||||
4 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | |||||
5 | * | |||||
6 | * Copyright 2000, 2010 Oracle and/or its affiliates. | |||||
7 | * | |||||
8 | * OpenOffice.org - a multi-platform office productivity suite | |||||
9 | * | |||||
10 | * This file is part of OpenOffice.org. | |||||
11 | * | |||||
12 | * OpenOffice.org is free software: you can redistribute it and/or modify | |||||
13 | * it under the terms of the GNU Lesser General Public License version 3 | |||||
14 | * only, as published by the Free Software Foundation. | |||||
15 | * | |||||
16 | * OpenOffice.org is distributed in the hope that it will be useful, | |||||
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |||||
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||||
19 | * GNU Lesser General Public License version 3 for more details | |||||
20 | * (a copy is included in the LICENSE file that accompanied this code). | |||||
21 | * | |||||
22 | * You should have received a copy of the GNU Lesser General Public License | |||||
23 | * version 3 along with OpenOffice.org. If not, see | |||||
24 | * <http://www.openoffice.org/license.html> | |||||
25 | * for a copy of the LGPLv3 License. | |||||
26 | * | |||||
27 | ************************************************************************/ | |||||
28 | ||||||
29 | #include <rtl/alloc.h> | |||||
30 | #include <sal/types.h> | |||||
31 | #include <cppunit/TestFixture.h> | |||||
32 | #include <cppunit/extensions/HelperMacros.h> | |||||
33 | #include <cppunit/plugin/TestPlugIn.h> | |||||
34 | ||||||
35 | #include <memory.h> | |||||
36 | ||||||
37 | namespace rtl_alloc | |||||
38 | { | |||||
39 | ||||||
40 | // small memory check routine, which return false, if there is a problem | |||||
41 | ||||||
42 | bool checkMemory(char* _pMemory, sal_uInt32 _nSize, char _n) | |||||
43 | { | |||||
44 | bool bOk = true; | |||||
45 | ||||||
46 | for (sal_uInt32 i=0;i<_nSize;i++) | |||||
47 | { | |||||
48 | if (_pMemory[i] != _n) | |||||
| ||||||
49 | { | |||||
50 | bOk = false; | |||||
51 | } | |||||
52 | } | |||||
53 | return bOk; | |||||
54 | } | |||||
55 | ||||||
56 | class Memory : public CppUnit::TestFixture | |||||
57 | { | |||||
58 | // for normal alloc functions | |||||
59 | char *m_pMemory; | |||||
60 | sal_uInt32 m_nSizeOfMemory; | |||||
61 | ||||||
62 | public: | |||||
63 | Memory() | |||||
64 | : m_pMemory(NULL__null) | |||||
65 | , m_nSizeOfMemory(1024) | |||||
66 | { | |||||
67 | } | |||||
68 | ||||||
69 | // initialise your test code values here. | |||||
70 | void setUp() | |||||
71 | { | |||||
72 | m_pMemory = (char*) rtl_allocateMemory( m_nSizeOfMemory ); | |||||
73 | } | |||||
74 | ||||||
75 | void tearDown() | |||||
76 | { | |||||
77 | rtl_freeMemory(m_pMemory); | |||||
78 | m_pMemory = NULL__null; | |||||
79 | } | |||||
80 | ||||||
81 | void rtl_allocateMemory_001() | |||||
82 | { | |||||
83 | CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pMemory != NULL)( CppUnit::Asserter::failIf( !(m_pMemory != __null), CppUnit:: Message( "assertion failed", "Expression: " "m_pMemory != NULL" , "Can get zero memory." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 83 ) ) ); | |||||
84 | memset(m_pMemory, 1, m_nSizeOfMemory); | |||||
85 | CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory, m_nSizeOfMemory, 1) == true)( CppUnit::Asserter::failIf( !(checkMemory(m_pMemory, m_nSizeOfMemory , 1) == true), CppUnit::Message( "assertion failed", "Expression: " "checkMemory(m_pMemory, m_nSizeOfMemory, 1) == true", "memory contains wrong value." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 85 ) ) ); | |||||
86 | } | |||||
87 | ||||||
88 | void rtl_reallocateMemory_001() | |||||
89 | { | |||||
90 | sal_uInt32 nSize = 2 * 1024; | |||||
91 | m_pMemory = (char*)rtl_reallocateMemory(m_pMemory, nSize); | |||||
92 | ||||||
93 | CPPUNIT_ASSERT_MESSAGE( "Can reallocate memory.", m_pMemory != NULL)( CppUnit::Asserter::failIf( !(m_pMemory != __null), CppUnit:: Message( "assertion failed", "Expression: " "m_pMemory != NULL" , "Can reallocate memory." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 93 ) ) ); | |||||
94 | memset(m_pMemory, 2, nSize); | |||||
95 | CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pMemory, nSize, 2) == true)( CppUnit::Asserter::failIf( !(checkMemory(m_pMemory, nSize, 2 ) == true), CppUnit::Message( "assertion failed", "Expression: " "checkMemory(m_pMemory, nSize, 2) == true", "memory contains wrong value." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 95 ) ) ); | |||||
96 | } | |||||
97 | ||||||
98 | CPPUNIT_TEST_SUITE(Memory)public: typedef Memory TestFixtureType; private: static const CppUnit::TestNamer &getTestNamer__() { static CppUnit::TestNamer testNamer( typeid(Memory) ); return testNamer; } public: typedef CppUnit::TestSuiteBuilderContext<TestFixtureType> TestSuiteBuilderContextType ; static void addTestsToSuite( CppUnit::TestSuiteBuilderContextBase &baseContext ) { TestSuiteBuilderContextType context( baseContext ); | |||||
99 | CPPUNIT_TEST(rtl_allocateMemory_001)context.addTest( ( new CppUnit::TestCaller<TestFixtureType >( context.getTestNameFor( "rtl_allocateMemory_001"), & TestFixtureType::rtl_allocateMemory_001, context.makeFixture( ) ) ) ); | |||||
100 | CPPUNIT_TEST(rtl_reallocateMemory_001)context.addTest( ( new CppUnit::TestCaller<TestFixtureType >( context.getTestNameFor( "rtl_reallocateMemory_001"), & TestFixtureType::rtl_reallocateMemory_001, context.makeFixture () ) ) ); | |||||
101 | CPPUNIT_TEST_SUITE_END()} static CppUnit::TestSuite *suite() { const CppUnit::TestNamer &namer = getTestNamer__(); std::auto_ptr<CppUnit::TestSuite > suite( new CppUnit::TestSuite( namer.getFixtureName() )) ; CppUnit::ConcretTestFixtureFactory<TestFixtureType> factory ; CppUnit::TestSuiteBuilderContextBase context( *suite.get(), namer, factory ); TestFixtureType::addTestsToSuite( context ) ; return suite.release(); } private: typedef int CppUnitDummyTypedefForSemiColonEnding__; | |||||
102 | }; // class test | |||||
103 | ||||||
104 | class TestZeroMemory : public CppUnit::TestFixture | |||||
105 | { | |||||
106 | // for zero functions | |||||
107 | char *m_pZeroMemory; | |||||
108 | sal_uInt32 m_nSizeOfZeroMemory; | |||||
109 | ||||||
110 | public: | |||||
111 | TestZeroMemory() | |||||
112 | : m_pZeroMemory(NULL__null) | |||||
113 | , m_nSizeOfZeroMemory( 50 * 1024 * 1024 ) | |||||
114 | { | |||||
115 | } | |||||
116 | ||||||
117 | // initialise your test code values here. | |||||
118 | void setUp() | |||||
119 | { | |||||
120 | m_pZeroMemory = (char*) rtl_allocateZeroMemory( m_nSizeOfZeroMemory ); | |||||
121 | } | |||||
122 | ||||||
123 | void tearDown() | |||||
124 | { | |||||
125 | rtl_freeZeroMemory(m_pZeroMemory, m_nSizeOfZeroMemory); | |||||
126 | // LLA: no check possible, may GPF if there is something wrong. | |||||
127 | // CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", pZeroMemory != NULL); | |||||
128 | } | |||||
129 | ||||||
130 | // insert your test code here. | |||||
131 | ||||||
132 | void rtl_allocateZeroMemory_001() | |||||
133 | { | |||||
134 | CPPUNIT_ASSERT_MESSAGE( "Can get zero memory.", m_pZeroMemory != NULL)( CppUnit::Asserter::failIf( !(m_pZeroMemory != __null), CppUnit ::Message( "assertion failed", "Expression: " "m_pZeroMemory != NULL" , "Can get zero memory." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 134 ) ) ); | |||||
135 | CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 0) == true)( CppUnit::Asserter::failIf( !(checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory , 0) == true), CppUnit::Message( "assertion failed", "Expression: " "checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 0) == true" , "memory contains wrong value." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 135 ) ) ); | |||||
| ||||||
136 | ||||||
137 | memset(m_pZeroMemory, 3, m_nSizeOfZeroMemory); | |||||
138 | CPPUNIT_ASSERT_MESSAGE( "memory contains wrong value.", checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 3) == true)( CppUnit::Asserter::failIf( !(checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory , 3) == true), CppUnit::Message( "assertion failed", "Expression: " "checkMemory(m_pZeroMemory, m_nSizeOfZeroMemory, 3) == true" , "memory contains wrong value." ), CppUnit::SourceLine( "/usr/local/src/libreoffice/sal/qa/rtl/alloc/rtl_alloc.cxx" , 138 ) ) ); | |||||
139 | } | |||||
140 | ||||||
141 | CPPUNIT_TEST_SUITE(TestZeroMemory)public: typedef TestZeroMemory TestFixtureType; private: static const CppUnit::TestNamer &getTestNamer__() { static CppUnit ::TestNamer testNamer( typeid(TestZeroMemory) ); return testNamer ; } public: typedef CppUnit::TestSuiteBuilderContext<TestFixtureType > TestSuiteBuilderContextType; static void addTestsToSuite ( CppUnit::TestSuiteBuilderContextBase &baseContext ) { TestSuiteBuilderContextType context( baseContext ); | |||||
142 | CPPUNIT_TEST(rtl_allocateZeroMemory_001)context.addTest( ( new CppUnit::TestCaller<TestFixtureType >( context.getTestNameFor( "rtl_allocateZeroMemory_001"), & TestFixtureType::rtl_allocateZeroMemory_001, context.makeFixture () ) ) ); | |||||
143 | CPPUNIT_TEST_SUITE_END()} static CppUnit::TestSuite *suite() { const CppUnit::TestNamer &namer = getTestNamer__(); std::auto_ptr<CppUnit::TestSuite > suite( new CppUnit::TestSuite( namer.getFixtureName() )) ; CppUnit::ConcretTestFixtureFactory<TestFixtureType> factory ; CppUnit::TestSuiteBuilderContextBase context( *suite.get(), namer, factory ); TestFixtureType::addTestsToSuite( context ) ; return suite.release(); } private: typedef int CppUnitDummyTypedefForSemiColonEnding__; | |||||
144 | }; | |||||
145 | ||||||
146 | // ----------------------------------------------------------------------------- | |||||
147 | CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::Memory)static CppUnit::AutoRegisterSuite< rtl_alloc::Memory > autoRegisterRegistry__147; | |||||
148 | CPPUNIT_TEST_SUITE_REGISTRATION(rtl_alloc::TestZeroMemory)static CppUnit::AutoRegisterSuite< rtl_alloc::TestZeroMemory > autoRegisterRegistry__148; | |||||
149 | } // namespace rtl_alloc | |||||
150 | ||||||
151 | ||||||
152 | CPPUNIT_PLUGIN_IMPLEMENT()extern "C" __attribute__ ((visibility("default"))) CppUnitTestPlugIn *cppunitTestPlugIn(void) { static CppUnit::TestPlugInDefaultImpl plugIn; return &plugIn; } typedef char __CppUnitPlugInExportFunctionDummyTypeDef ; int main( int, char *[] ) { return 0; } typedef char __CppUnitPlugInImplementMainDummyTypeDef; | |||||
153 | ||||||
154 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |