The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
.
google unix.com



High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
segmentation fault rockgal High Level Programming 8 12-05-2006 12:16 PM
Segmentation Fault compbug UNIX for Dummies Questions & Answers 3 04-21-2006 10:43 AM
segmentation fault wojtyla High Level Programming 3 02-19-2005 02:53 PM
Segmentation fault jshaulis AIX 1 06-01-2004 04:16 PM
segmentation fault omran High Level Programming 2 08-01-2003 08:19 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-08-2008
lagigliaivan lagigliaivan is offline
Registered User
  
 

Join Date: May 2008
Posts: 45
Why not a segmentation fault??

Hi,

Why I don't receive a segmentation fault in the following sample.

int main(void)
{
char buff[10];
sprintf(buff,"Hello world");
printf("%s\n",buff);
}

If I define a buffer of 10 elements and I'm trying to put inside it twelve elements, Should I receive a sigsev signal???

Thanks in advance!
  #2 (permalink)  
Old 05-08-2008
Zarnick Zarnick is offline
Registered User
  
 

Join Date: May 2004
Location: Brazil
Posts: 40
Actually, not quite.
SegFault (in this case) goes when the EIP register point's to a invalid memory address to execute, and just 2 bytes aren't enough to reach the EIP, however try with 500 chars , you should get your SegFault.
  #3 (permalink)  
Old 05-08-2008
lagigliaivan lagigliaivan is offline
Registered User
  
 

Join Date: May 2008
Posts: 45
Thanks!

Hi Zarnick,

So, you mean that when I overwrite the return instruction pointer on the procedure stack I should receive a SegFault, but if I don't reach it I don't? and why not 2 bytes aren't enough??

Sorry, I don't want to be annoying!

Thanks!!
  #4 (permalink)  
Old 05-08-2008
shamrock shamrock is offline Forum Advisor  
Registered User
  
 

Join Date: Oct 2007
Location: USA
Posts: 750
snprintf() anyone

Another reason to use snprintf() instead of sprintf() because sprintf() does not check for bounds.
  #5 (permalink)  
Old 05-08-2008
Zarnick Zarnick is offline
Registered User
  
 

Join Date: May 2004
Location: Brazil
Posts: 40
I wouldn't know for sure why 2 bytes are just not enough, I think it's something to do with the pointers used to allocate the enough memory for your char[10] string. But I can be wrong on this one.

And at least from what I can remember, I never saw a segfault that wasn't about the EIP register being overruned, as always, I can be wrong.
  #6 (permalink)  
Old 05-08-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Shamrock -
He is trying to create a SIGSEGV. To test some other code. This corrupts the stack on HPUX PA_RISC boxes, and then dumps core:
Code:
#include <stdlib.h>
#include <string.h>

int main()
{
	char buff[10]={0x0};
	char *p=buff;
	p-=20;
    memset(p,0x0, sizeof(buff));
    return 0;	
}
  #7 (permalink)  
Old 05-08-2008
lagigliaivan lagigliaivan is offline
Registered User
  
 

Join Date: May 2008
Posts: 45
Thanks!

Hi All,

Certainly I'm fixing an application with this type of code and I've replaced the sprintf by snprintf function, but I have to explain to my boss why I'm doing it and I need to show him the application crashing.

The original message in the printf function overcomes the boundaries only for 2 bytes and it doesn't crash, I don't know how to do it and I don't know if its behavior is deterministic or not.

Thanks to all of you for your support!!!!!
Closed Thread

Bookmarks

Tags
linux

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:38 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0