Missing prototype and resulting coredump

R

Rakesh UV

Hi,
If i am not putting the function prototype of a function
returning a pointer, i get a core dump.Though this will happen less
probably on 32 bit machine

example
int main(int argc , char **argv)
{
char *base = basename(argv[0]) ;
printf("%s",base);
return 0;

}

The compiler assumes that basename is returning an int and thus we
loose 32 bit of the actual address, because pointer is 64 bit and
integer is 32 bit in 64 bit machines

I am working on
Os linux x86_64 2.6.9
Machine Hp
compiler gcc 3.46

Is there any option in GCC to make the default return value as long so
that i can preserve the actual address returned
or
do we have any other way in C to make it right
I know that putting the prototype would solve the problem, but
unfortunately
there are huge number of files


Rakesh UV
 
I

Ian Collins

Rakesh said:
I know that putting the prototype would solve the problem, but
unfortunately
there are huge number of files
Time to warm up your favorite text editor. Can't you add the
prototype(s) to an appropriate header and the add said header to all the
files with a script?

Porting smelly code from 32 to 64 bit is fraught with problems, lack of
prototypes is one you can easily fix.
 
J

Jack Klein

Hi,
If i am not putting the function prototype of a function
returning a pointer, i get a core dump.Though this will happen less
probably on 32 bit machine

example
int main(int argc , char **argv)
{
char *base = basename(argv[0]) ;

If your compiler does not issue a diagnostic for this code, either it
is badly broken, or you did not tell it to operate as an actual C
compiler. A diagnostic is required.
printf("%s",base);
return 0;

}

The compiler assumes that basename is returning an int and thus we
loose 32 bit of the actual address, because pointer is 64 bit and
integer is 32 bit in 64 bit machines

And what if 64 bit addresses are returned in a completely different
manner and you are really losing all 64 bits of the address?

I am working on
Os linux x86_64 2.6.9
Machine Hp
compiler gcc 3.46

Is there any option in GCC to make the default return value as long so

Why? The code has undefined behavior. It is wrong. It is garbage.
that i can preserve the actual address returned
or

What if 64 bit long long is returned in a different register than a 64
bit pointer?
do we have any other way in C to make it right

There is no way in C to make it right. The code has undefined
behavior. If a function returns anything other than an int, and you
do not have a prototype in scope, they there is no way to avoid
undefined behavior. Period. The only way to make it right is to have
a prototype in scope. Period.
I know that putting the prototype would solve the problem, but
unfortunately
there are huge number of files

That's why there are programs that can search and replace in huge
numbers of files.
Rakesh UV

Don't EVER port code this bad to another platform without fixing it.

And reeducate the programmers who wrote it. If they won't accept
reeducation, fire them.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
B

Barry Schwarz

Hi,
If i am not putting the function prototype of a function
returning a pointer, i get a core dump.Though this will happen less
probably on 32 bit machine

example
int main(int argc , char **argv)
{
char *base = basename(argv[0]) ;
printf("%s",base);
return 0;

}

The compiler assumes that basename is returning an int and thus we
loose 32 bit of the actual address, because pointer is 64 bit and
integer is 32 bit in 64 bit machines

I am working on
Os linux x86_64 2.6.9
Machine Hp
compiler gcc 3.46

Is there any option in GCC to make the default return value as long so
that i can preserve the actual address returned

Even if there were such an option, your code would still invoke
undefined behavior. This is true anytime you cause the compiler to
make an assumption that is not valid. Why do you assume the compiler
will return a pointer using the same mechanism it uses to return an
integer? What is so onerous about providing a prototype that you are
willing to run this risk?
or
do we have any other way in C to make it right
I know that putting the prototype would solve the problem, but
unfortunately
there are huge number of files

That's what headers are for.


Remove del for email
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top