473,387 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Help on setjmp & longjmp for strings

Andr3w
42
Hi,

I'd like to know if there is a way to implement setjmp & longjmp to recover from the following scenario:
Expand|Select|Wrap|Line Numbers
  1. // allocate buffer
  2. char *m = malloc(10);
  3.  
  4. // get buffer
  5. gets_s(m , sizeof(m));
  6.  
Now if the user types more than 10 chars we have a segmentation error but when I do the following it doesn't find it:
Expand|Select|Wrap|Line Numbers
  1. // define our error handler
  2.         void static __cdecl pStrHandler( int pSig );
  3.  
  4.     // dummy var to save the env
  5.     jmp_buf env_b;
  6.  
  7.     // entry point for our application
  8.     int main( int argc, char **argv)
  9.     {
  10.         // declare more vars
  11.         // ..
  12.         //
  13.         int marker; // marker for env jump
  14.         char b[10]; // buffer to raise exception
  15.  
  16.         typedef void (*ptrTSigHndlr)(int);
  17.  
  18.         // set up a pointer to the OldHandler in order to replace it
  19.         ptrTSigHndlr OldHanlder;
  20.  
  21.         // let’s check if the signal handler is allocated ok
  22.         if ( (OldHandler = signal ( SIGSEGV, pStrHandler ) )  )
  23.         {
  24.             // failed to set up signal hanlder, abort
  25.             abort();
  26.         }
  27.  
  28.         // save the environment for recovery to env_b buf
  29.         marker = set_jmp(env_b);
  30.  
  31.         // try the potential error code
  32.         if ( marker == 0 )
  33.         {
  34.  
  35.             gets_s(b, sizeof(b)); // now input as many chars as you want
  36.         }
  37.         else {  // do stuff you want, maybe a lookup error code function? 
  38. }
  39.  
  40.  
  41. return 0;    
  42. }
  43.  
  44. //define our handler…
  45. void static __cdecl pStrHandler( int pSig )
  46. {
  47.   // error handling code here 
  48. }
  49.  
Can you help me solve this? I works fine for 1 byte char but I want for char arrays...

Any help is great :)
Nov 6 '07 #1
6 1643
weaknessforcats
9,208 Expert Mod 8TB
I think gets_s is a Microsoft whiz-bang. That being the case I think you need to call _set_invalid_parameter_handler.

I don't think there's a long jump in your scenario.
Nov 6 '07 #2
Andr3w
42
Well this gives me errors even if i use gets, I thought about going for
Expand|Select|Wrap|Line Numbers
  1. if( gets(a /* buff */) == NULL)
  2. {
  3.    // do stuff
  4. }
  5.  
but this doesn't work..in VS6 is leaves the leak go on (and only if you use the value gets an error) but in VS8 at least the debbuger informs you of the assertion error. While in visual studio 8 I can use the _set_invalid_parameter_hanlder() and _invalid_parameter_hanlder I can't in Visual Studio 6. So I have to go with gets and setjmp/longjmp, fine by me but setjmp and longjmp don't work (the above code is sufficient to illustrate my problem if you change the gets_s to gets and increase the buffer to a value larger than 1)

Please help a bit ;)
Nov 12 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
// allocate buffer
char *m = malloc(10);

// get buffer
gets_s(m , sizeof(m));
While we are chatting along I just noticed your original code above.

You are aware that sizeof(m) is always 4 ?? That is, it's the size of the address.
Nov 12 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
I never did understand why you are using VC++ 6.0. Even with all the service packs, it doesn't work. There are bugs in the templates, partial specializtion is not supported not is covariance, etc.

The current release is 8.0 and 9.0 is almost here.

As to get_s(), it is designed to validate the buffer size and gag if it is too small. That assumes you know the correct buffer size at the outset. If you do not, then I suggest you implement a function that will accept up to, say 10, characters and if there is no end-of-line or end-of file condition, that it packs away the first 10 and accepts another 10. That way it can keep packing away groups of 10 unitl it sees and end-of-line or end-of-file.

This way you know the buffer size and you realloc as necessary until the inout is processed.

What do you think?
Nov 12 '07 #5
Andr3w
42
Actually I am not using VS6 I am using VS9 (beta) but I am asking for compatibility for VS6 because they have it on my uni and they want their c files to be run on VS6.. I know it's hell crappy it's like 10 years old... Well that's why I am doing all this fuzz, _set_invalid_parameter() works neat and I used it before I posted here. The thing is I wanted to get the job done using setjmp and longjmp. Also in my code above I have actually done that mistake... :P
Nov 13 '07 #6
weaknessforcats
9,208 Expert Mod 8TB
You are mentioning they. As in users that have VS6 and are using your code??

Maybe you should build a library using VS9 and give them the library and a header file. They can merrily go using VC6 and linking with your library.
Nov 13 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Thomas Baruchel | last post by:
Hi, wondering about: func1: setjmp() ; func2(); func2: {FILE *f; f = fopen(); func3(); fclose(f)} func3 : if() longjmp; else return; Note that FILE *fis a local variable in func2.
6
by: someone | last post by:
I have *thought* that setjmp/longjmp() should take a pointer to jmp_buf. And the calling function should hold the actual struct data. But ... I trid on both Win32 and Linux, it seems that...
12
by: Michael B Allen | last post by:
Should setjmp/longjmp really be used in a fast mundane ANSI C piece of code? Or is it frowned apon like goto? I have a need but I don't want to use something that is costly, isn't supported...
5
by: candy | last post by:
hi all, Consider the following C code: void funct(){ }
20
by: JS | last post by:
When setjmp is called how can the if statement evaluate to true or false when setjmp only returns 0 or non-zero? struct pcb { void *(*start_routine) (void *); void *arg; jmp_buf state; int...
8
by: Zheng Da | last post by:
I wrote a simple one as follow: typedef struct __myjmp_buf { int efp; int epc; }myjmp_buf; int mysetjmp(myjmp_buf env) {
4
by: Sreekanth | last post by:
Hi All I am trying to write a code in which I have to access an array of jmp_buf so i have declared it as jmp_buf mybuf Now when i am doing a longjmp like
15
by: rover8898 | last post by:
Hello all, I used setjmp() in a recent of program of mine (it is not completed, so I have not the chance to test it out yet). I am not very profocient in C coding as are some of my co-workers....
3
by: no_click_there | last post by:
Hi, I'm learning to use the setjmp/longjmp functions and still can't really grasp what's going on here. I'd basically like to be able to jump back and forth from (or to) two given functions. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.