List Info

Thread: $./a.out Memory fault / gcc




$./a.out Memory fault / gcc
user name
2007-04-01 06:36:04
Hi, can someone tell why this is not working on NetBSD?
(I'm running NetBSD/i386 3.1)

$cat test.c
#include<stdio.h>

void read(){
    printf("function: readn");
}

void write(){
    printf("function: writen");
}

int main(){
    read();
    write();

    return 1;
}

$gcc test.c
$
$./a.out
Memory fault
$


It works perfect on Linux and FreeBSD.

Is read and write protected words?

Re: $./a.out Memory fault / gcc
country flaguser name
United States
2007-04-01 08:09:10
  Is read and write protected words?

They are names of system calls and should not be used in
normal user
programs, but they are not reserved words in the language.

man 2 read
man 2 write

printf apparently gets turned into puts, and that ends up
calling
write:

#10 0x08048707 in write ()
#11 0xbbbc589c in __swrite () from /usr/lib/libc.so.12
#12 0xbbbc2e2c in __sflush () from /usr/lib/libc.so.12
#13 0xbbbc2ec1 in fflush () from /usr/lib/libc.so.12
#14 0xbbbc2d5c in __sfvwrite () from /usr/lib/libc.so.12
#15 0xbbb929f1 in puts () from /usr/lib/libc.so.12
#16 0x08048707 in write ()

  It works perfect on Linux and FreeBSD.

That's interesting, but perhaps somehow the way the program
is linked
allows the printf library routine to call the write system
call, or
perhaps it calls writev.

I don't have the C spec handy, but I'd say that redefining
standard
library function names should result in undefined behavior. 
Undefined
behavior includes a segmentation fault, so NetBSD seems ok
here.

There's a harder question of how this could be caught in a
more
friendly way.

-- 

    Greg Troxel <gdtir.bbn.com>

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )