Hi,
I'd like to know if there is a way to implement setjmp & longjmp to recover from the following scenario: -
// allocate buffer
-
char *m = malloc(10);
-
-
// get buffer
-
gets_s(m , sizeof(m));
-
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: -
// define our error handler
-
void static __cdecl pStrHandler( int pSig );
-
-
// dummy var to save the env
-
jmp_buf env_b;
-
-
// entry point for our application
-
int main( int argc, char **argv)
-
{
-
// declare more vars
-
// ..
-
//
-
int marker; // marker for env jump
-
char b[10]; // buffer to raise exception
-
-
typedef void (*ptrTSigHndlr)(int);
-
-
// set up a pointer to the OldHandler in order to replace it
-
ptrTSigHndlr OldHanlder;
-
-
// let’s check if the signal handler is allocated ok
-
if ( (OldHandler = signal ( SIGSEGV, pStrHandler ) ) )
-
{
-
// failed to set up signal hanlder, abort
-
abort();
-
}
-
-
// save the environment for recovery to env_b buf
-
marker = set_jmp(env_b);
-
-
// try the potential error code
-
if ( marker == 0 )
-
{
-
-
gets_s(b, sizeof(b)); // now input as many chars as you want
-
}
-
else { // do stuff you want, maybe a lookup error code function?
-
}
-
-
-
return 0;
-
}
-
-
//define our handler…
-
void static __cdecl pStrHandler( int pSig )
-
{
-
// error handling code here
-
}
-
Can you help me solve this? I works fine for 1 byte char but I want for char arrays...
Any help is great :)
6 1643
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.
Well this gives me errors even if i use gets, I thought about going for -
if( gets(a /* buff */) == NULL)
-
{
-
// do stuff
-
}
-
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 ;)
// 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.
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?
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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.
|
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...
|
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...
|
by: candy |
last post by:
hi all,
Consider the following C code:
void funct(){
}
|
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...
|
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)
{
|
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
|
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....
|
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.
...
|
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,...
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |