Skip to content

Commit 231a2f6

Browse files
MB-60819: Check return value of malloc_info
Check return value of malloc_info and if buf == nullptr before searching within. Change-Id: I7656561ef88156d3bd5b9593e7ee444f7bcda705 Reviewed-on: https://review.couchbase.org/c/cbft/+/206056 Tested-by: Abhi Dangeti <[email protected]> Reviewed-by: <[email protected]> Well-Formed: Build Bot <[email protected]> Well-Formed: Restriction Checker
1 parent 054e69a commit 231a2f6

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

c_heap_mem_usage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C" {
1616
size_t get_total_heap_bytes();
1717
#else
1818
size_t get_total_heap_bytes() {
19-
return(size_t)0L;
19+
return (size_t)0L;
2020
}
2121
#endif
2222

c_heap_mem_usage_linux.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ size_t get_attribute_value(const char *str, const char *first_substr, const char
5252
}
5353

5454

55-
size_t get_total_heap_bytes(){
55+
size_t get_total_heap_bytes() {
5656
// ref: https://gist.github.com/tadeu/95013963c64da4cd74a2c6f4fa4fd553
5757
// There are three functions in Linux libc API to retrieve heap
5858
// information: `malloc_stats`, `mallinfo` and `malloc_info`.
@@ -72,11 +72,20 @@ size_t get_total_heap_bytes(){
7272
size_t buf_size = 0;
7373

7474
FILE* f = open_memstream(&buf, &buf_size);
75+
if (f == NULL) {
76+
return (size_t)0;
77+
}
78+
7579
// this doesn't include the golang or the other process's stats
7680
// as per local testing
77-
malloc_info(0, f);
81+
int rv = malloc_info(0, f);
7882
fclose(f);
7983

84+
// https://man7.org/linux/man-pages/man3/malloc_info.3.html
85+
if ((rv != 0) || (buf == NULL)) {
86+
return (size_t)0;
87+
}
88+
8089
// We are only interested in totals, so we skip everything until the
8190
// closing of the <heap>...</heap> block.
8291
const char* pos = strstr(buf, "</heap>");
@@ -96,6 +105,7 @@ size_t get_total_heap_bytes(){
96105

97106
free(buf);
98107
buf = NULL;
108+
99109
return allocated_mem;
100110
}
101111
#endif

0 commit comments

Comments
 (0)