Skip to content

Commit 2b9586a

Browse files
authored
Fix clang compilation errors due to variable length arrays (#264)
There are some occurrences of variable length arrays. We need to change them to dynamic allocation to avoid runtime memory issues. This commit just fix some of those occurrences by using C++ dynamic memory allocation. Signed-off-by: Julio Faracco <jcfaracco@gmail.com>
1 parent 34abee3 commit 2b9586a

1 file changed

Lines changed: 23 additions & 11 deletions

File tree

sym/bcc_proc.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ static bool which_so_in_process(const char* libname, int pid, char* libpath) {
476476
char endline[4096], *mapname = NULL, *newline;
477477
char mappings_file[128];
478478
const size_t search_len = strlen(libname) + strlen("/lib.");
479-
char search1[search_len + 1];
480-
char search2[search_len + 1];
479+
char *search1 = new char[search_len + 1];
480+
char *search2 = new char[search_len + 1];
481481

482482
snprintf(mappings_file, sizeof(mappings_file), "/proc/%ld/maps", (long)pid);
483483
FILE *fp = fopen(mappings_file, "r");
@@ -507,39 +507,51 @@ static bool which_so_in_process(const char* libname, int pid, char* libpath) {
507507
}
508508
} while (ret != EOF);
509509

510+
delete[] search1;
511+
delete[] search2;
512+
510513
fclose(fp);
511514
return found;
512515
}
513516

514517
char *bcc_procutils_which_so(const char *libname, int pid) {
515518
const size_t soname_len = strlen(libname) + strlen("lib.so");
516-
char soname[soname_len + 1];
519+
char *soname = new char[soname_len + 1];
520+
char *ret = NULL;
517521
char libpath[4096];
518522
int i;
519523

520-
if (strchr(libname, '/'))
521-
return strdup(libname);
524+
if (strchr(libname, '/')) {
525+
ret = strdup(libname);
526+
goto done;
527+
}
522528

523-
if (pid && which_so_in_process(libname, pid, libpath))
524-
return strdup(libpath);
529+
if (pid && which_so_in_process(libname, pid, libpath)) {
530+
ret = strdup(libpath);
531+
goto done;
532+
}
525533

526534
if (lib_cache_count < 0)
527-
return NULL;
535+
goto done;
528536

529537
if (!lib_cache_count && load_ld_cache(LD_SO_CACHE) < 0) {
530538
lib_cache_count = -1;
531-
return NULL;
539+
goto done;
532540
}
533541

534542
snprintf(soname, soname_len + 1, "lib%s.so", libname);
535543

536544
for (i = 0; i < lib_cache_count; ++i) {
537545
if (!strncmp(lib_cache[i].libname, soname, soname_len) &&
538546
match_so_flags(lib_cache[i].flags)) {
539-
return strdup(lib_cache[i].path);
547+
ret = strdup(lib_cache[i].path);
548+
goto done;
540549
}
541550
}
542-
return NULL;
551+
552+
done:
553+
delete[] soname;
554+
return ret;
543555
}
544556

545557
void bcc_procutils_free(const char *ptr) {

0 commit comments

Comments
 (0)