LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 11-24-2009, 06:26 AM   #1
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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 06:29 AM.
 
Old 11-24-2009, 07:44 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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, 11:06 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Original Poster
Rep: Reputation: 239Reputation: 239Reputation: 239
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, 12:36 PM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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



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
Press to Continue incomingid Linux - Newbie 4 04-02-2009 07:18 AM
Firefox 3, automatically reduces window size? How to stop auto-resizing? newtovanilla Linux - Newbie 1 07-08-2008 08:40 PM
Key press Majestros Programming 2 02-14-2007 03:42 AM
Stop a download after specified file size RodWC General 3 08-19-2006 06:45 AM
LXer: http://www.clusterresources.com/pages/press/press-releases.php LXer Syndicated Linux News 0 04-12-2006 07:21 AM

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

All times are GMT -5. The time now is 07:29 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