c - Invalid write of size 8 after a malloc -
i working on project school , having issues code. purpose of programm implement plugin manager search in directory "*_plugin.so" file , add plugin descriptor simple linked list.
the c code :
//struct of single node typedef struct _chainon_ { plugin_descriptor * desc; struct _chainon_ * next; } chainon; // manager contains sentry node & number of plugins contained list struct plugin_manager_t { int nbelts; chainon * sentinel; }; typedef struct { const char * m_name; // nom du filtre const char * m_description; // description de l'effet du filtre filter_function m_filtre; // fonction de réalisation du filtre } plugin_descriptor;
now register_plugin function, called while programm find new plugin in directory, calls init_ function call register_plugin :
void init_(plugin_manager * pm) { register_plugin(pm, "null_filter", "exemple de filtre inutile", null_filter); }
and supposed add new plug list :
void register_plugin(plugin_manager * pm, const char filter_name[], const char filter_description[], filter_function the_filter) { chainon * n = (chainon *)malloc(sizeof(chainon)); //new node want add linked list n->desc = null; n->next = null; n->desc->m_name = filter_name; n->desc->m_description = filter_description; n->desc->m_filtre = the_filter; chainon * current = pm->sentinel; for(int i=0;i<pm->nbelts;i++){ current=current->next; i++; } current->next = n; }
and getting valgrind while execute programm :
> ==7022== invalid write of size 8 > ==7022== @ 0x4015a7: register_plugin (pluginmanager.cc:165) > ==7022== 0x66e1bdc: init_ (null_filter_plugin.cc:23) > ==7022== 0x401483: discover_plugins (pluginmanager.cc:113) > ==7022== 0x401187: main (main.cc:17) > ==7022== address 0x0 not stack'd, malloc'd or (recently) free'd > ==7022== > ==7022== > ==7022== process terminating default action of signal 11 (sigsegv) > ==7022== access not within mapped region @ address 0x0 > ==7022== @ 0x4015a7: register_plugin (pluginmanager.cc:165) > ==7022== 0x66e1bdc: init_ (null_filter_plugin.cc:23) > ==7022== 0x401483: discover_plugins (pluginmanager.cc:113) > ==7022== 0x401187: main (main.cc:17) > ==7022== if believe happened result of stack > ==7022== overflow in program's main thread (unlikely > ==7022== possible), can try increase size of > ==7022== main thread stack using --main-stacksize= flag. > ==7022== main thread stack size used in run 8388608.
i novice @ c programming, use c++.
but not understand why not initialize "n->desc->name" since allocated memory malloc , initialized null ?
any appreciate !
thank you
your code has several problems, of them minor problems , others causing posted valgrind output,
is not problem, it's don't need cast return value of
malloc()
chainon *n = malloc(sizeof(chainon));
is ok, no need cast.
you need check
malloc()
succeeded, not assume did, under normal situations not fail, in case of failure program not handle that, , in case has sensitive data needs stored in hard drive or other situation clean exit needed, cause lot of problems program users, should ensure program exits cleanly, hence checking return value ofmalloc()
thing do, check againstnull
right after every callmalloc()
, handle according situation failure occurs.you don't allocate space struct members, every pointer must point valid memory before dereferencing it, must ensure point valid memory, uninitialized pointers can't checked in cases going initialize pointer after possible check, initialize
null
.you in 1 case, dereference
null
pointer, undefined behavior.
using recommendations above, function has re-written this*
void register_plugin(plugin_manager * pm, const char *const filter_name, const char *const filter_description, filter_function the_filter) { chainon *chainon; plugin_descriptor *descriptor; chainon *current int i; if (pm == null) return; chainon = malloc(sizeof(*chainon)); if (chainon == null) return; chainon->next = null; descriptor = malloc(sizeof(*descriptor)); if (descriptor == null) { free(chainon); return; } chainon->desc = descriptor; descriptor->m_name = filter_name; descriptor->m_description = filter_description; descripotor->m_filtre = the_filter; current = pm->sentinel; if (current == null) return; for(i = 0 ; ((i < pm->nbelts) && (current->next != null)) ; ++i) current = current->next; current->next = chainon; }
*some of things changed not necessary. think it's better way.
Comments
Post a Comment