Re: what am I missing?

new topic     » goto parent     » topic index » view thread      » older message » newer message
mattlewis said...
jacques_desch said...

the extra byte is at end of allocated struct. Matt code give this on Linpus lite compiled with gcc 4.1.2

termios2: 44 
c_iflag:  0xbfc9e408 
c_oflag:  0xbfc9e40c 
c_cflag:  0xbfc9e410 
c_lflag:  0xbfc9e414 
c_line:   0xbfc9e418 
c_cc:     0xbfc9e419 
c_ispeed: 0xbfc9e42c 
c_ospeed: 0xbfc9e430 
So there is no padding between fields but allocationg 44 crash my program. Well I will take it as a mystery and go on with TERMIOS_SIZE=45

That's odd. What happens if you do the same in C? Meaning, can you malloc 44 bytes and not have it crash?

It sounds like the code is writing out of bounds somewhere. Actually, I'd recommend running it under valgrind, so see where the illegal access is happening. Sounds like a bug in the library. If you do any C coding on Linux, valgrind can be your best friend for tracking down weird memory issues.

Matt

Matt, Following you suggestion, here de C code I tested and it crash too, but only if I free t pointer. The same is true in my euphoria binding.

#include <stdio.h> 
#include <asm/termbits.h> 
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 
#include <stdlib.h> 
 
int main(){ 
int fd, fnVal; 
struct termios2 *t; 
  fd = open("/dev/tty0",O_RDONLY); 
  printf("file handle %d\n", fd); 
  t = malloc(sizeof(struct termios2)); 
  fnVal = tcgetattr(fd, t); 
  printf("ispeed %d, ospeed %d\n",t->c_ispeed, t->c_ospeed); 
  free(t); // it doesn't crash if I don't free t. 
  puts("t freed\n"); 
  close(fd); 
} 
 
 
Now the question is why freeing t, which is no more used, crash the program?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu