Thread: erm wat? malloc error

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839

    erm wat? malloc error

    I got this after compiling my program:
    Code:
    mitsy.elf: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
    Aborted (core dumped)
    Not quite sure what is casuing this, I have only one instance of calloc being used in this function
    Code:
    void* InitSegment(
    	segment_t *this, segment_t *prev,
    	u16 upto, char const *txt, u16 Tsize ) {
    	this->pos = prev ? prev->pos + prev->mem.size : 0;
    	this->mem.buff = calloc( upto, Tsize );
    	if ( !this->mem.buff ) {
    		memset( this, 0, sizeof(segment_t) );
    		return NULL;
    	}
    	this->mem.size = upto * Tsize;
    	this->mem.upto = upto;
    	this->mem.used = 0;
    	this->txt = txt;
    	return this->mem.buff;
    }
    malloc & realloc are never used so I don't even understand how this error can occur, I get that calloc probably uses malloc under the hood but how does an error like above even occur?

    Edit: Shoulda mentioned this before but since the edit it now only says segmentation fault, trying to find GUI debugger mode for geany, the only one I found so far has a dead link
    Last edited by awsdert; 04-05-2019 at 03:32 AM. Reason: removed a cast left over from converted function, switched unconverted sizeof to Tsize

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,728
    Maybe you write to a part of memory you shouldn't, thus corrupting the memory block's metadata?
    Devoted my life to programming...

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    Ended up using codeblocks depsite it's clunkiness and poorly documented variables, anyways the segfault is appearing here:
    Code:
    phent = &phead[1];
    phent->p_type = ELPH_PT_LOAD; // < This line is the apparent cause
    Can't imagin how though, phead is initialised to 2 program headers and a failure leads to freeing allocated memory and jumping to the exit

  4. #4
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    839
    Still don't know what caused it but managed to resolve the error by changing from multiple allocations to a big allocation after calculating the need memory:
    Code:
    void* Setup64( segment_t *segv, u16 segments, u16 symbols, u16 headers, ... ) {
    	u16 size = 1, i = 0;
    	segment_t *ehead_seg = &segv[SEG_EHEAD];
    	segment_t *shead_seg = &segv[SEG_SHEAD];
    	segment_t *spool_seg = &segv[SEG_SPOOL];
    	segment_t *names_seg = &segv[SEG_NAMES];
    	segment_t *phead_seg = &segv[SEG_PHEAD];
    	segment_t *coded_seg = &segv[SEG_CODED];
    	segment_t *this, *prev;
    	char const *str = NULL;
    	char *pool;
    	va_list args;
    	va_start( args, headers );
    	str = va_arg( args, char const * );
    	while ( str ) {
    		segoffsetv[i] = strlen( str ) + 1;
    		size += segoffsetv[i++];
    		spool_seg->mem.upto++;
    		spool_seg->mem.used++;
    		str = va_arg( args, char const * );
    	} size += 1;
    	va_end( args );
    	ehead_seg->mem.size = sizeof(ehead64_t);
    	ehead_seg->mem.upto = ehead_seg->mem.used = 1;
    	shead_seg->mem.size = sizeof(shead64_t) * segments;
    	shead_seg->mem.upto = segments;
    	spool_seg->mem.size = size;
    	names_seg->mem.size = sizeof(named64_t) * symbols;
    	names_seg->mem.upto = symbols;
    	phead_seg->mem.size = sizeof(phead64_t) * headers;
    	phead_seg->mem.upto = headers;
    	coded_seg->mem.size = sizeof(begin_t);
    	coded_seg->mem.upto = coded_seg->mem.used = 1;
    	for ( i = 0, size = 0; i < SEG_COUNT; ++i ) {
    		size += segv[i].mem.size;
    	}
    	ehead_seg->mem.buff = calloc( 1, size );
    	if ( !ehead_seg->mem.buff ) return NULL;
    	for ( i = 1; i < SEG_COUNT; ++i ) {
    		prev = &segv[i-1];
    		this = &segv[i];
    		this->pos = prev->pos + prev->mem.size;
    		this->mem.buff = &(((char*)ehead_seg->mem.buff)[this->pos]);
    	}
    	va_start( args, headers );
    	str = va_arg( args, char const * );
    	pool = (char*)spool_seg->mem.buff;
    	i = 0;
    	while ( str ) {
    		strcpy( &pool[segoffsetv[i++]], str );
    		str = va_arg( args, char const * );
    	} size += 1;
    	va_end( args );
    	return ehead_seg->mem.buff;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in malloc
    By prathiksa in forum C Programming
    Replies: 8
    Last Post: 11-02-2012, 10:22 AM
  2. malloc() pointer error
    By in_ship in forum C Programming
    Replies: 4
    Last Post: 08-11-2012, 02:55 AM
  3. Segmentation error... malloc. Please help
    By Rollo in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 09:34 AM
  4. malloc, iterations, bus error and more
    By z0diark in forum C Programming
    Replies: 8
    Last Post: 12-01-2009, 04:51 AM

Tags for this Thread