Skip to content

Commit bfbbe21

Browse files
committed
IGNITE-10981: Fixed heap memory corruption in C++
1 parent 457ffc1 commit bfbbe21

1 file changed

Lines changed: 16 additions & 30 deletions

File tree

modules/platforms/cpp/core/src/ignition.cpp

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ namespace ignite
7575
* Constructor.
7676
*/
7777
JvmOptions() :
78-
size(0),
79-
opts(0)
78+
opts()
8079
{
8180
// No-op.
8281
}
@@ -100,16 +99,9 @@ namespace ignite
10099
{
101100
Deinit();
102101

103-
size = 3 + static_cast<int>(cfg.jvmOpts.size());
102+
const size_t REQ_OPTS_CNT = 4;
104103

105-
if (!home.empty())
106-
++size;
107-
108-
// Brackets '()' here guarantee for the array to be zeroed.
109-
// Important to avoid crash in case of exception.
110-
opts = new char*[size]();
111-
112-
int idx = 0;
104+
opts.reserve(cfg.jvmOpts.size() + REQ_OPTS_CNT);
113105

114106
std::string fileEncParam = "-Dfile.encoding=";
115107

@@ -118,35 +110,35 @@ namespace ignite
118110
// 1. Set classpath.
119111
std::string cpFull = "-Djava.class.path=" + cp;
120112

121-
opts[idx++] = CopyChars(cpFull.c_str());
113+
opts.push_back(CopyChars(cpFull.c_str()));
122114

123115
// 2. Set home.
124116
if (!home.empty()) {
125117
std::string homeFull = "-DIGNITE_HOME=" + home;
126118

127-
opts[idx++] = CopyChars(homeFull.c_str());
119+
opts.push_back(CopyChars(homeFull.c_str()));
128120
}
129121

130122
// 3. Set Xms, Xmx.
131123
std::string xmsStr = JvmMemoryString("-Xms", cfg.jvmInitMem);
132124
std::string xmxStr = JvmMemoryString("-Xmx", cfg.jvmMaxMem);
133125

134-
opts[idx++] = CopyChars(xmsStr.c_str());
135-
opts[idx++] = CopyChars(xmxStr.c_str());
126+
opts.push_back(CopyChars(xmsStr.c_str()));
127+
opts.push_back(CopyChars(xmxStr.c_str()));
136128

137129
// 4. Set the rest options.
138130
for (std::list<std::string>::const_iterator i = cfg.jvmOpts.begin(); i != cfg.jvmOpts.end(); ++i) {
139131
if (i->find(fileEncParam) != std::string::npos)
140132
hadFileEnc = true;
141133

142-
opts[idx++] = CopyChars(i->c_str());
134+
opts.push_back(CopyChars(i->c_str()));
143135
}
144136

145137
// 5. Set file.encoding.
146138
if (!hadFileEnc) {
147139
std::string fileEncFull = fileEncParam + "UTF-8";
148140

149-
opts[idx++] = CopyChars(fileEncFull.c_str());
141+
opts.push_back(CopyChars(fileEncFull.c_str()));
150142
}
151143
}
152144

@@ -155,23 +147,20 @@ namespace ignite
155147
*/
156148
void Deinit()
157149
{
158-
if (opts)
159-
{
160-
for (int i = 0; i < size; ++i)
161-
ReleaseChars(opts[i]);
150+
for (size_t i = 0; i < opts.size(); ++i)
151+
ReleaseChars(opts[i]);
162152

163-
delete[] opts;
164-
}
153+
opts.clear();
165154
}
166155

167156
/**
168157
* Get built options.
169158
*
170159
* @return Built options
171160
*/
172-
char** GetOpts() const
161+
char** GetOpts()
173162
{
174-
return opts;
163+
return &opts[0];
175164
}
176165

177166
/**
@@ -181,15 +170,12 @@ namespace ignite
181170
*/
182171
int GetSize() const
183172
{
184-
return size;
173+
return static_cast<int>(opts.size());
185174
}
186175

187176
private:
188-
/** Size */
189-
int size;
190-
191177
/** Options array. */
192-
char** opts;
178+
std::vector<char*> opts;
193179
};
194180

195181
Ignite Ignition::Start(const IgniteConfiguration& cfg)

0 commit comments

Comments
 (0)