Skip to content

Commit

Permalink
mod stats: Fix unhandled corner-case
Browse files Browse the repository at this point in the history
Do not crash on NULL pointer input.
  • Loading branch information
liviuchircu authored and razvancrainea committed Nov 22, 2017
1 parent 7f74df9 commit aa8e489
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
9 changes: 9 additions & 0 deletions mem/f_malloc.c
Expand Up @@ -142,14 +142,23 @@ inline static unsigned long big_hash_idx(unsigned long s)
#endif

#ifdef SHM_EXTRA_STATS
#include "module_info.h"
void set_stat_index (void *ptr, unsigned long idx) {
struct fm_frag *f;

if (!ptr)
return;

f = (struct fm_frag *)((char*)ptr - sizeof(struct fm_frag));
f->statistic_index = idx;
}

unsigned long get_stat_index(void *ptr) {
struct fm_frag *f;

if (!ptr)
return GROUP_IDX_INVALID;

f = (struct fm_frag *)((char*)ptr - sizeof(struct fm_frag));
return f->statistic_index;
}
Expand Down
9 changes: 9 additions & 0 deletions mem/hp_malloc.c
Expand Up @@ -178,14 +178,23 @@ unsigned long frag_size(void* p){
}

#ifdef SHM_EXTRA_STATS
#include "module_info.h"
void set_stat_index (void *ptr, unsigned long idx) {
struct hp_frag *f;

if (!ptr)
return;

f = (struct hp_frag *)((char*)ptr - sizeof(struct hp_frag));
f->statistic_index = idx;
}

unsigned long get_stat_index(void *ptr) {
struct hp_frag *f;

if (!ptr)
return GROUP_IDX_INVALID;

f = (struct hp_frag *)((char*)ptr - sizeof(struct hp_frag));
return f->statistic_index;
}
Expand Down
4 changes: 2 additions & 2 deletions mem/module_info.c
Expand Up @@ -97,11 +97,11 @@ int init_new_stat(stat_var* stat) {
#undef MALLOC_UNSAFE
}

inline void update_module_stats(long mem_used, long real_used, int frags, int group_idx) {
inline void update_module_stats(long mem_used, long real_used, int frags, unsigned long group_idx) {

unsigned long local_max, global_max;

if (mem_skip_stats)
if (mem_skip_stats || group_idx == GROUP_IDX_INVALID)
return;

#ifdef SHM_SHOW_DEFAULT_GROUP
Expand Down
4 changes: 3 additions & 1 deletion mem/module_info.h
Expand Up @@ -33,6 +33,8 @@
#define STAT_PREFIX "shmem_"
#define STAT_PREFIX_LEN 12

#define GROUP_IDX_INVALID ((unsigned long)-1)

extern struct multi_str* mod_names;
extern unsigned int mem_free_idx;
extern void* main_handle;
Expand All @@ -54,7 +56,7 @@ struct multi_str{

int set_mem_idx(char* mod_name, int mem_free_idx);

void update_module_stats(long mem_used, long real_used, int frags, int group_idx);
void update_module_stats(long mem_used, long real_used, int frags, unsigned long group_idx);

int alloc_group_stat(void);

Expand Down
6 changes: 5 additions & 1 deletion mem/q_malloc.c
Expand Up @@ -175,12 +175,16 @@ unsigned long frag_size(void* p){
}

#ifdef SHM_EXTRA_STATS
#include "module_info.h"
void set_stat_index (void *ptr, unsigned long idx) {
if (!ptr)
return;

FRAG(ptr)->statistic_index = idx;
}

unsigned long get_stat_index(void *ptr) {
return FRAG(ptr)->statistic_index;
return !ptr ? GROUP_IDX_INVALID : FRAG(ptr)->statistic_index;
}
#endif

Expand Down

0 comments on commit aa8e489

Please sign in to comment.