LinuxQuestions.org
Visit Jeremy's Blog.
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
 
LinkBack Search this Thread
Old 11-24-2009, 07:26 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Rep: Reputation: 91
stop press: size is important


as a tangent from another post,
variable arrays in C

variable arrays in C

Code:
    unsigned long sz;
    printf("Enter size:");
    scanf(" %lu", &sz );
    printf("size %lu\n", sz);

    char array[sz];
the above array is made on the stack.

there seems no indication on how to check your size isn't too
big (oo-er!).
i.e. in the above I'll get a core dump if I make it too big (snigger!).

Last edited by bigearsbilly; 11-24-2009 at 07:29 AM.
 
Old 11-24-2009, 08:44 AM   #2
Guttorm
Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 938

Rep: Reputation: 140Reputation: 140
Hmm. Stack overflows lead to crashes. It doesn't matter if the array is variable length or not. Try this:

char array[100*1024*1024];
 
Old 11-24-2009, 12:06 PM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,016

Original Poster
Rep: Reputation: 91
yeah I know.
I was just remarking there seems no way to tell before hand if it'll blow up.
so it's a useful but dangerous extension.

i.e. is there no safe way to use this feature?
 
Old 11-24-2009, 01:36 PM   #4
Guttorm
Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 938

Rep: Reputation: 140Reputation: 140
Well, there are always limits. You could perhaps check with getrlimit to get the stack size, but then again what's the point? The function alloca does the same thing, it could return NULL, but it doesn't.

You could use malloc, but it has limits as well. Passing a too big value to malloc can be worse than a simple crash. (I'm thinking a lot of swapping - not sure if it's better than a simple crash.)

Nevertheless it can be made safe:

Code:
#include <string.h>
#include <stdio.h> 
#include <stdlib.h>
#include <signal.h>

void die(int sig)
{
  fprintf(stderr,"Number too big!\n");
  exit(1);
}

void scaryfunc(void)
{
  unsigned long size;
  printf("How many megabytes to allocate?\n");
  scanf("%lu",&size);
  size *= 1024*1024;
  char s[size];
  strcpy(s,"Big buffer");
  printf("%s\n",s);
}

int main(void)
{
  stack_t ss;

  ss.ss_sp = malloc(SIGSTKSZ);
  ss.ss_size = SIGSTKSZ;
  ss.ss_flags = 0;
  sigaltstack(&ss, NULL);
  struct sigaction act;
  memset (&act, '\0', sizeof(act));
  act.sa_handler = die;
  act.sa_flags = SA_ONSTACK;   
  sigaction(SIGSEGV,&act,NULL);
  scaryfunc();
  return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Press to Continue incomingid Linux - Newbie 4 04-02-2009 08:18 AM
Firefox 3, automatically reduces window size? How to stop auto-resizing? newtovanilla Linux - Newbie 1 07-08-2008 09:40 PM
Key press Majestros Programming 2 02-14-2007 04:42 AM
Stop a download after specified file size RodWC General 3 08-19-2006 07:45 AM
LXer: http://www.clusterresources.com/pages/press/press-releases.php LXer Syndicated Linux News 0 04-12-2006 08:21 AM


All times are GMT -5. The time now is 05:55 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration