LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-24-2006, 09:59 AM   #1
astropirhana
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian 3.X
Posts: 14

Rep: Reputation: 0
how to not use GNU "C" Library


I am reading about the way Linux Systems Access a null pointer. The GNU "C" implementation allows you to read it, but not write to it.
Code:
char *tst = (char*) 0;
printf("Reading a null pointer gives you %s.\n", tst);
sprintf(tst, "Write to null pointer");
//so you get:
$./tst
Reading a null pointer gives you (null).
Segmentation fault.

If you read it as a string you get "(null)\0". If you are not using the GNU "C" library, you can do neither and you get your segmentation fault on the read.

//so you get
$./tst
Segmentation fault.

How do I eliminate GNU "C" Library to execute this code?

astro
 
Old 08-24-2006, 10:25 AM   #2
cupubboy
Member
 
Registered: May 2003
Location: Bucharest,Romania
Distribution: Fedora Core 7
Posts: 109

Rep: Reputation: 15
I'm more of a c++ guy .. than a c ..

but this line

Code:
char *tst = (char*) 0;
Seems terribly wrong to me
so you have 0 wich is an literal constant and you cast that to a pointer is this legal??

wouldn't it be correct to have

Code:
Whatever * myVar = NULL; // or 0
??
 
Old 08-24-2006, 10:28 AM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
No, you cannot read it. The function printf is cautious enough to check whether you passed a null pointer (which you probably shouldn't), and in that case does NOT dereference it but instead prints "(null)".

What are you trying to do?
 
Old 08-24-2006, 10:31 AM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
The printf function happens to check if the pointer you give it is null, and prints "(null)" if so. There's nothing fundamental in C or linux that makes reading a null pointer give you "(null)". In fact, if printf() had actually used the null pointer, it would have segfaulted.

edit: oops, quigi answered first
 
Old 08-24-2006, 10:36 AM   #5
astropirhana
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian 3.X
Posts: 14

Original Poster
Rep: Reputation: 0
I am reading about abusing memory and the NULL pointer.

The code is from a book. And the book says the GNU "C" Lib allows you to read the pointer but not write to the memory that it points to.

Other implementations of the "C" Library dont allow you to read the null pointer.

So how do I swap out GNU C Lib for some other using gcc?

Thanks for all your responses

astro

Last edited by astropirhana; 08-24-2006 at 10:48 AM.
 
Old 08-24-2006, 10:54 AM   #6
astropirhana
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian 3.X
Posts: 14

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by Quigi
No, you cannot read it. The function printf is cautious enough to check whether you passed a null pointer (which you probably shouldn't), and in that case does NOT dereference it but instead prints "(null)".

What are you trying to do?

I think this is a great answer!

How do I use a different C Library from GNU C Lib?

ahoodin
 
Old 08-24-2006, 12:10 PM   #7
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
Thanks. Aluser's reply is more accurate than your book.

Normally you'd strive to write portable code, that doesn't rely on a particular implementation's quirks. So I assume you are into experimenting and just want to see that SEGV. (You could get one easily with "char oops = *tst;" -- that illustrates that you cannot read through the null pointer.)

To answer your question, read "man gcc", particularly about the linker option -nostdlib; you'll have to supply a replacement library. Disclaimer: I haven't done that.
 
Old 08-24-2006, 12:14 PM   #8
astropirhana
LQ Newbie
 
Registered: Jul 2006
Distribution: Debian 3.X
Posts: 14

Original Poster
Rep: Reputation: 0
Thanks alot guys!

astro
 
Old 08-24-2006, 12:18 PM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> The code is from a book. And the book says the GNU "C" Lib allows you to read the pointer but not write to the memory that it points to.

your book must be leaving out an important illustration that would show you why it is could be possible to read or write to a NULL ptr. a NULL ptr points to memory that is just as usable as any other memory if the OS would let it. when memory is laid out for your program at runtime, the lowest memory 0-?? (usually a couple of addresses above 0) are set aside for a NULL area.


anyone correct me if im wrong but i think the runtime address space looks like so


--- high address

heap

---
...
... extra space for heap to grow
...
---

stack

---
NULL address
---


so in theory it would be possible to read or write to null, but it is typically not accessible.

what i am not sure about is if it is read only or not. i thought it was unreadable, completely unreadable. that is why you segfault, because you cant read that adderess. but i have not had a reason to dig into that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: The GNU "Lesser" General Public License gets some love LXer Syndicated Linux News 0 07-18-2006 05:54 PM
conversion of dynamic ".so" library to ststic ".a" library aashaysood Linux - Hardware 2 07-02-2006 11:15 PM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 02:32 PM
Can you explain the difference between "Free Software (GNU)" and "Open Source"? vharishankar General 5 03-03-2005 09:40 AM
how should I respond to "Encryption key for doc.gpg?" in GNU Emacs? Joseph Schiller Programming 1 10-24-2003 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration