Skip to content

Commit b5223e1

Browse files
author
Petter Reinholdtsen
committed
Rewrote local char* to use std::unique_ptr.
1 parent d8c1345 commit b5223e1

File tree

4 files changed

+56
-131
lines changed

4 files changed

+56
-131
lines changed

src/tbb/cgroup_info.h

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -150,42 +150,27 @@ class cgroup_info {
150150

151151
static bool try_read_cgroup_v1_num_cpus_from(const char* dir, int& num_cpus) {
152152
std::size_t pathlen = strlen(dir) + 30;
153-
char *path = (char*)calloc(pathlen, 1);
154-
if (std::snprintf(path, pathlen, "%s/cpu.cfs_quota_us", dir) < 0) {
155-
free(path);
156-
path = NULL;
153+
std::unique_ptr<char[]> path(new char[pathlen]);
154+
if (std::snprintf(path.get(), pathlen, "%s/cpu.cfs_quota_us", dir) < 0)
157155
return false; // Failed to create path
158-
}
159156

160-
unique_file_t fd(std::fopen(path, "r"), &close_file);
161-
if (!fd) {
162-
free(path);
163-
path = NULL;
157+
unique_file_t fd(std::fopen(path.get(), "r"), &close_file);
158+
if (!fd)
164159
return false;
165-
}
166160

167161
long long cpu_quota = 0;
168-
if (std::fscanf(fd.get(), "%lld", &cpu_quota) != 1) {
169-
free(path);
170-
path = NULL;
162+
if (std::fscanf(fd.get(), "%lld", &cpu_quota) != 1)
171163
return false;
172-
}
173164

174165
if (-1 == cpu_quota) {
175166
num_cpus = unlimited_num_cpus; // -1 quota means maximum available CPUs
176-
free(path);
177-
path = NULL;
178167
return true;
179168
}
180169

181-
if (std::snprintf(path, pathlen, "%s/cpu.cfs_period_us", dir) < 0) {
182-
free(path);
183-
path = NULL;
170+
if (std::snprintf(path.get(), pathlen, "%s/cpu.cfs_period_us", dir) < 0)
184171
return false; // Failed to create path;
185-
}
186-
fd.reset(std::fopen(path, "r"));
187-
free(path);
188-
path = NULL;
172+
173+
fd.reset(std::fopen(path.get(), "r"));
189174
if (!fd)
190175
return false;
191176

@@ -199,16 +184,11 @@ class cgroup_info {
199184

200185
static bool try_read_cgroup_v2_num_cpus_from(const char* dir, int& num_cpus) {
201186
std::size_t pathlen = strlen(dir) + 30;
202-
char *path = (char*)calloc(pathlen, 1);
203-
if (std::snprintf(path, pathlen, "%s/cpu.max", dir) < 0) {
204-
free(path);
205-
path = NULL;
187+
std::unique_ptr<char[]> path(new char[pathlen]);
188+
if (std::snprintf(path.get(), pathlen, "%s/cpu.max", dir) < 0)
206189
return false; // Failed to create path
207-
}
208190

209-
unique_file_t fd(std::fopen(path, "r"), &close_file);
210-
free(path);
211-
path = NULL;
191+
unique_file_t fd(std::fopen(path.get(), "r"), &close_file);
212192
if (!fd)
213193
return false;
214194

@@ -246,18 +226,14 @@ class cgroup_info {
246226
static int parse_cgroup_entry(const char* mnt_dir, process_cgroup_data& pcd) {
247227
int num_cpus = error_value; // Initialize to an impossible value
248228
std::size_t dirlen = strlen(mnt_dir) + strlen(pcd.relative_path) + 2;
249-
char *dir = (char*)calloc(dirlen, 1);
250-
if (std::snprintf(dir, dirlen, "%s/%s", mnt_dir, pcd.relative_path) >= 0) {
251-
if (try_read_cgroup_num_cpus_from(dir, num_cpus, pcd.version)) {
252-
free(dir);
253-
dir = NULL;
229+
std::unique_ptr<char[]> dir(new char[dirlen]);
230+
if (std::snprintf(dir.get(), dirlen, "%s/%s", mnt_dir, pcd.relative_path) >= 0) {
231+
if (try_read_cgroup_num_cpus_from(dir.get(), num_cpus, pcd.version)) {
254232
return num_cpus;
255233
}
256234
}
257-
num_cpus = try_read_cgroup_num_cpus_from(mnt_dir, num_cpus, pcd.version) ? num_cpus : error_value;
258-
free(dir);
259-
dir = NULL;
260-
return num_cpus;
235+
236+
return try_read_cgroup_num_cpus_from(mnt_dir, num_cpus, pcd.version) ? num_cpus : error_value;
261237
}
262238

263239
static bool is_cpu_restriction_possible(process_cgroup_data& pcd) {
@@ -307,19 +283,18 @@ class cgroup_info {
307283
std::size_t dirlen = (strlen(pcd.relative_path)
308284
+ strlen(cg_cfg.sys_fs_cgroup_dir_path)
309285
+ 25);
310-
char *dir = (char*)calloc(dirlen, 1);
286+
std::unique_ptr<char[]> dir(new char[dirlen]);
311287
__TBB_ASSERT(*pcd.relative_path, nullptr);
312-
if (0 <= std::snprintf(dir, dirlen, "%s/%s", cg_cfg.sys_fs_cgroup_dir_path,
288+
if (0 <= std::snprintf(dir.get(), dirlen, "%s/%s",
289+
cg_cfg.sys_fs_cgroup_dir_path,
313290
pcd.relative_path))
314-
try_read_cgroup_num_cpus_from(dir, num_cpus, pcd.version);
291+
try_read_cgroup_num_cpus_from(dir.get(), num_cpus, pcd.version);
315292

316293
if (error_value == num_cpus && pcd.version == process_cgroup_data::cgroup_version::v2) {
317-
if (0 <= std::snprintf(dir, dirlen, "%s/%s", "/sys/fs/cgroup/unified",
294+
if (0 <= std::snprintf(dir.get(), dirlen, "%s/%s", "/sys/fs/cgroup/unified",
318295
pcd.relative_path))
319-
try_read_cgroup_v2_num_cpus_from(dir, num_cpus);
296+
try_read_cgroup_v2_num_cpus_from(dir.get(), num_cpus);
320297
}
321-
free(dir);
322-
dir = NULL;
323298
return num_cpus;
324299
}
325300

src/tbb/dynamic_link.cpp

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -690,22 +690,18 @@ namespace r1 {
690690
* occurs, in which case the error is optionally reported.
691691
*/
692692
bool has_valid_signature(const char* filepath, const std::size_t length) {
693-
std::size_t pathlen = strlen(filepath)+1;
694-
wchar_t *wfilepath = (wchar_t*)calloc(pathlen, sizeof(wchar_t));
693+
std::unique_ptr<wchar_t[]> path(new wchar_t[length+1]);
695694
{
696695
std::mbstate_t state{};
697696
const char* ansi_filepath = filepath; // mbsrtowcs moves original pointer
698-
const size_t num_converted = mbsrtowcs(wfilepath, &ansi_filepath, length, &state);
699-
if (num_converted == std::size_t(-1)) {
700-
free (wfilepath);
701-
wfilepath = NULL;
697+
const size_t num_converted = mbsrtowcs(wfilepath.get(), &ansi_filepath, length, &state);
698+
if (num_converted == std::size_t(-1))
702699
return false;
703-
}
704700
}
705701
WINTRUST_FILE_INFO fdata;
706702
std::memset(&fdata, 0, sizeof(fdata));
707703
fdata.cbStruct = sizeof(WINTRUST_FILE_INFO);
708-
fdata.pcwszFilePath = wfilepath;
704+
fdata.pcwszFilePath = wfilepath.get();
709705

710706
// Check that the certificate used to sign the specified file chains up to a root
711707
// certificate located in the trusted root certificate store, implying that the identity of
@@ -730,8 +726,6 @@ namespace r1 {
730726
pWVTData.dwStateAction = WTD_STATEACTION_CLOSE; // Release WVT state data
731727
(void)WinVerifyTrust(NULL, &pgActionID, &pWVTData);
732728

733-
free (wfilepath);
734-
wfilepath = NULL;
735729
return ERROR_SUCCESS == rc;
736730
}
737731
#endif // _WIN32 && !__TBB_SKIP_DEPENDENCY_SIGNATURE_VERIFICATION
@@ -744,49 +738,38 @@ namespace r1 {
744738
#if __TBB_DYNAMIC_LOAD_ENABLED
745739
const char* path = library;
746740
std::size_t len = abs_path(library, NULL, 1);
747-
char *absolute_path = (char*)calloc(len, 1);
741+
std::unique_ptr<char[]> absolute_path(new char[len]);
748742
std::size_t length = 0;
749743
const bool build_absolute_path = flags & DYNAMIC_LINK_BUILD_ABSOLUTE_PATH;
750744
if (build_absolute_path) {
751-
length = abs_path( library, absolute_path, len );
745+
length = abs_path( library, absolute_path.get(), len );
752746
if (length == 0) {
753747
// length == 0 means failing of init_ap_data so the warning has already been issued.
754-
free(absolute_path);
755-
absolute_path = NULL;
756748
return nullptr;
757-
} else if (!file_exists(absolute_path)) {
749+
} else if (!file_exists(absolute_path.get())) {
758750
// Path to a file has been built manually. It is not proven to exist however.
759-
DYNAMIC_LINK_WARNING( dl_lib_not_found, absolute_path, dlerror() );
760-
free(absolute_path);
761-
absolute_path = NULL;
751+
DYNAMIC_LINK_WARNING( dl_lib_not_found, absolute_path.get(), dlerror() );
762752
return nullptr;
763753
}
764-
path = absolute_path;
754+
path = absolute_path.get();
765755
}
766756
#if _WIN32
767757
#if !__TBB_SKIP_DEPENDENCY_SIGNATURE_VERIFICATION
768758
if (!build_absolute_path) { // Get the path if it is not yet built
769-
length = get_module_path(absolute_path, (unsigned)len, library);
759+
length = get_module_path(absolute_path.get(), (unsigned)len, library);
770760
if (length == 0) {
771761
DYNAMIC_LINK_WARNING( dl_lib_not_found, path, dlerror() );
772-
free(absolute_path);
773-
absolute_path = NULL;
774762
return library_handle;
775763
} else if (length >= len) { // The buffer length was insufficient
776764
DYNAMIC_LINK_WARNING( dl_buff_too_small );
777-
free(absolute_path);
778-
absolute_path = NULL;
779765
return library_handle;
780766
}
781767
length += 1; // Count terminating NULL character as part of string length
782-
path = absolute_path;
768+
path = absolute_path.get();
783769
}
784770

785-
if (!has_valid_signature(path, length)) {
786-
free(absolute_path);
787-
absolute_path = NULL;
771+
if (!has_valid_signature(path, length))
788772
return library_handle; // Warning (if any) has already been reported
789-
}
790773
#endif /* !__TBB_SKIP_DEPENDENCY_SIGNATURE_VERIFICATION */
791774
// Prevent Windows from displaying silly message boxes if it fails to load library
792775
// (e.g. because of MS runtime problems - one of those crazy manifest related ones)
@@ -805,8 +788,6 @@ namespace r1 {
805788
}
806789
} else
807790
DYNAMIC_LINK_WARNING( dl_lib_not_found, path, dlerror() );
808-
free(absolute_path);
809-
absolute_path = NULL;
810791
#endif /* __TBB_DYNAMIC_LOAD_ENABLED */
811792
return library_handle;
812793
}

test/common/utils_concurrency_limit.h

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -208,43 +208,27 @@ class cgroup_info {
208208

209209
static bool try_read_cgroup_v1_num_cpus_from(const char* dir, int& num_cpus) {
210210
std::size_t pathlen = strlen(dir) + 20;
211-
char *path = (char*)calloc(pathlen, 1);
212-
if (std::snprintf(path, pathlen, "%s/cpu.cfs_quota_us", dir) < 0) {
213-
free(path);
214-
path = NULL;
211+
std::unique_ptr<char[]> path(new char[pathlen]);
212+
if (std::snprintf(path.get(), pathlen, "%s/cpu.cfs_quota_us", dir) < 0)
215213
return false; // Failed to create path
216-
}
217214

218-
unique_file_t fd(std::fopen(path, "r"), &close_file);
219-
if (!fd) {
220-
free(path);
221-
path = NULL;
215+
unique_file_t fd(std::fopen(path.get(), "r"), &close_file);
216+
if (!fd)
222217
return false;
223-
}
224218

225219
long long cpu_quota = 0;
226-
if (std::fscanf(fd.get(), "%lld", &cpu_quota) != 1) {
227-
free(path);
228-
path = NULL;
220+
if (std::fscanf(fd.get(), "%lld", &cpu_quota) != 1)
229221
return false;
230-
}
231222

232223
if (-1 == cpu_quota) {
233224
num_cpus = unlimited_num_cpus; // -1 quota means maximum available CPUs
234-
free(path);
235-
path = NULL;
236225
return true;
237226
}
238227

239-
if (std::snprintf(path, pathlen, "%s/cpu.cfs_period_us", dir) < 0) {
240-
free(path);
241-
path = NULL;
228+
if (std::snprintf(path.get(), pathlen, "%s/cpu.cfs_period_us", dir) < 0)
242229
return false; // Failed to create path
243-
}
244230

245-
fd.reset(std::fopen(path, "r"));
246-
free(path);
247-
path = NULL;
231+
fd.reset(std::fopen(path.get(), "r"));
248232
if (!fd)
249233
return false;
250234

@@ -258,16 +242,11 @@ class cgroup_info {
258242

259243
static bool try_read_cgroup_v2_num_cpus_from(const char* dir, int& num_cpus) {
260244
std::size_t pathlen = strlen(dir) + 10;
261-
char *path = (char*)calloc(pathlen, 1);
262-
if (std::snprintf(path, pathlen, "%s/cpu.max", dir) < 0) {
263-
free(path);
264-
path = NULL;
245+
std::unique_ptr<char[]> path(new char[pathlen]);
246+
if (std::snprintf(path.get(), pathlen, "%s/cpu.max", dir) < 0)
265247
return false;
266-
}
267248

268-
unique_file_t fd(std::fopen(path, "r"), &close_file);
269-
free(path);
270-
path = NULL;
249+
unique_file_t fd(std::fopen(path.get(), "r"), &close_file);
271250
if (!fd)
272251
return false;
273252

@@ -296,7 +275,7 @@ class cgroup_info {
296275
{
297276
int num_cpus = error_value; // Initialize to an impossible value
298277
std::size_t dirlen = strlen(mnt_dir) + rel_path_size + 2;
299-
char *dir = NULL;
278+
std::unique_ptr<char[]> dir(new char[dirlen]);
300279
if (!std::strncmp(mnt_type, "cgroup2", 7)) { // Found cgroup v2 mount entry
301280
// At first, try reading CPU quota directly
302281
if (try_read_cgroup_v2_num_cpus_from(mnt_dir, num_cpus))
@@ -306,11 +285,8 @@ class cgroup_info {
306285
cache_relative_path_for(cgroup_fd, paths_cache);
307286

308287
// Now try reading including relative path
309-
dir = (char*)calloc(dirlen, 1);
310-
if (std::snprintf(dir, dirlen, "%s/%s", mnt_dir, paths_cache.v2_relative_path) >= 0)
311-
try_read_cgroup_v2_num_cpus_from(dir, num_cpus);
312-
free(dir);
313-
dir = NULL;
288+
if (std::snprintf(dir.get(), dirlen, "%s/%s", mnt_dir, paths_cache.v2_relative_path) >= 0)
289+
try_read_cgroup_v2_num_cpus_from(dir.get(), num_cpus);
314290
return num_cpus;
315291
}
316292

@@ -322,11 +298,8 @@ class cgroup_info {
322298
if (!*paths_cache.v1_relative_path)
323299
cache_relative_path_for(cgroup_fd, paths_cache);
324300

325-
dir = (char*)calloc(dirlen, 1);
326-
if (std::snprintf(dir, dirlen, "%s/%s", mnt_dir, paths_cache.v1_relative_path) >= 0)
327-
try_read_cgroup_v1_num_cpus_from(dir, num_cpus);
328-
free(dir);
329-
dir = NULL;
301+
if (std::snprintf(dir.get(), dirlen, "%s/%s", mnt_dir, paths_cache.v1_relative_path) >= 0)
302+
try_read_cgroup_v1_num_cpus_from(dir.get(), num_cpus);
330303
return num_cpus;
331304
}
332305

test/tbb/test_dynamic_link.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,15 +167,15 @@ TEST_CASE("Test dynamic_link with existing library") {
167167
TEST_CASE("Test dynamic_link with bad library") {
168168
const char* lib_name = TEST_LIBRARY_NAME("stub_unsigned");
169169
const std::size_t size = tbb::detail::r1::abs_path(lib_name, NULL, 1) + 1;
170-
char *path = (char*)calloc(size, 1);
170+
std::unique_ptr<char[]> path(new char[size]);
171171
const std::size_t msg_size = size + 128; // Path to the file + message
172-
char *msg = (char*)calloc(msg_size, 1);
172+
std::unique_ptr<char[]> msg(new char[msg_size]);
173173

174174
#if !__TBB_WIN8UI_SUPPORT
175-
const std::size_t len = tbb::detail::r1::abs_path(lib_name, path, size);
175+
const std::size_t len = tbb::detail::r1::abs_path(lib_name, path.get(), size);
176176
REQUIRE_MESSAGE((0 < len && len < size), "The path to the library is not built");
177-
std::snprintf(msg, msg_size, "Test prerequisite is not held - the path \"%s\" must exist", path);
178-
REQUIRE_MESSAGE(tbb::detail::r1::file_exists(path), msg);
177+
std::snprintf(msg.get(), msg_size, "Test prerequisite is not held - the path \"%s\" must exist", path.get());
178+
REQUIRE_MESSAGE(tbb::detail::r1::file_exists(path.get()), msg.get());
179179
#endif
180180

181181
// The library exists, check that it will not be loaded.
@@ -191,21 +191,17 @@ TEST_CASE("Test dynamic_link with bad library") {
191191
const bool link_result = tbb::detail::r1::dynamic_link(lib_name, table,
192192
sizeof(table) / sizeof(table[0]),
193193
/*handle*/nullptr, load_flags);
194-
std::snprintf(msg, msg_size, "The library \"%s\" was loaded but should not have been.", path);
194+
std::snprintf(msg.get(), msg_size, "The library \"%s\" was loaded but should not have been.", path.get());
195195

196196
// Expectation is that the library will not be loaded because:
197197
// a) On Windows the library is unsigned
198198
// b) On Linux the library does not have exported symbols
199199
const bool expected_link_result = false;
200200

201-
REQUIRE_MESSAGE(expected_link_result == link_result, msg);
201+
REQUIRE_MESSAGE(expected_link_result == link_result, msg.get());
202202
REQUIRE_MESSAGE(nullptr == handler, "The symbol should not be changed.");
203203
// TODO: Verify the warning message contains "TBB dynamic link warning: The module
204204
// \".*stub_unsigned.*.dll\" is unsigned or has invalid signature."
205-
free(msg);
206-
msg = NULL;
207-
free(path);
208-
path = NULL;
209205
}
210206

211207
#endif // __TBB_DYNAMIC_LOAD_ENABLED

0 commit comments

Comments
 (0)