stack smashing

when program runs causes stack smashing

Page 1 of 1

5 Replies - 1466 Views - Last Post: 01 May 2009 - 06:33 PM Rate Topic: -----

#1 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

stack smashing

Posted 01 May 2009 - 06:26 PM

#include <stdio.h>
#include <string.h>

int main() {
	char message[10];
	int i, count;
	
	strcpy(message, "hello world");
	
	printf("Repeat how many times? ");
	scanf("%d", &count);
	
	for(i = 0; i < count; i++)
		printf("%3d - %s\n", i, message);
	}
	

When this runs, it runs fine, compiled fine with no warnings, but as soon as it finishes, it causes this output:

Repeat how many times? 5
0 - hello world
1 - hello world
2 - hello world
3 - hello world
4 - hello world
*** stack smashing detected ***: ./input terminated
======= Backtrace: =========

Some stuff happens here, and than this happens after stack smashing is detected:

======= Memory map: ========

With a bunch of random memory addresses.

Is This A Good Question/Topic? 0
  • +

Replies To: stack smashing

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: stack smashing

Posted 01 May 2009 - 06:27 PM

Hello World is more then 10 characters.

11 characters plus the null terminator '\0' is 12 and your array can only hold 10, writing past the length of the array, corrupting the stack.


edited for a typo, should have said 10

This post has been edited by KYA: 01 May 2009 - 06:31 PM

Was This Post Helpful? 0
  • +
  • -

#3 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: stack smashing

Posted 01 May 2009 - 06:30 PM

Oh, very nice, thank you. I was really confused why I was accessing random areas of memory, suppose that's what a buffer overflow does huh?
Was This Post Helpful? 0
  • +
  • -

#4 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 994
  • View blog
  • Posts: 4,158
  • Joined: 14-February 08

Re: stack smashing

Posted 01 May 2009 - 06:30 PM

What KYA said :^:
Was This Post Helpful? 0
  • +
  • -

#5 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3088
  • View blog
  • Posts: 19,136
  • Joined: 14-September 07

Re: stack smashing

Posted 01 May 2009 - 06:30 PM

Well, you're lucky. It would be worse if it was subtle, didn't crash the program, and just gave weird numbers and such at random intervals :P
Was This Post Helpful? 0
  • +
  • -

#6 IngeniousHax  Icon User is offline

  • |>|20-514<|{3|2

Reputation: 78
  • View blog
  • Posts: 1,358
  • Joined: 28-March 09

Re: stack smashing

Posted 01 May 2009 - 06:33 PM

Yeah that'd be no good at all. I know I had learned about this in my C++ class and im obviously trying to write C, but I didn't think about stepping out of the arrays bounds.

This post has been edited by IngeniousHax: 01 May 2009 - 06:33 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1