Solved

Fundamental Question:Error Handling

Posted on 2004-04-08
9
236 Views
Last Modified: 2010-04-15
I have a complex, nested struct. Under certain conditions,
some pointers in that struct may become bad, and
memory access error occurs.

The name of the struct is pSeg, and now I just do
if ( ! pSeg ) return;
but that doesn't protect me from, let's say, pSeg->Line[0] being inaccessible.

How do I verify the struct and its members before trying
to access it?
0
Comment
Question by:jd9288
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
The only way u can verify the struct is by accessing it.

What u can do is replace the malloc fn. by a wrapper say mymalloc()
Then in this mymalloc() u can set flags according to whether the struct has been allocated or not.

Then,using thses flags,u can verify the struct before accessing it.

0
 
LVL 11

Assisted Solution

by:griessh
griessh earned 50 total points
Comment Utility
Hi jd9288,

>> some pointers in that struct may become bad
Before you use these pointers you have to test them for validity.
Or
Make sure when tey 'become bad' they are set to NULL so you can test like

if (ptr) {
  do something;
else
  return error;
}

Now you only have to define what 'become bad' really means :-)

======
Werner
0
 
LVL 9

Expert Comment

by:ankuratvb
Comment Utility
U can set these flags when the pointers are allocated.
The flags would be part of the struct itself so that each struct object would have its own flag.

U can have one variable and use bitwise operations to have 1 bit for 1 pointer rather than having 1 variable for 1 pointer.
0
 

Author Comment

by:jd9288
Comment Utility
A little more on how the struct is created:

I get that struct from a DLL, it's being populated by reading and parsing several
text files. If errors occur during that parsing, some struct members and pointers are
never initialized. There are no flags to indicate that a specific part of the struct is not valid,
and I have no control over the process.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 9

Assisted Solution

by:ankuratvb
ankuratvb earned 100 total points
Comment Utility
Can u put in a function of ur own that sets the struct values before the parsing starts.

If that can be done,u can initialize the pointers to NULL and the members to some specific value which can be checked to show that this member is uninitialized after the parsing process.
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 100 total points
Comment Utility
Hi jd9288,
> If errors occur during that parsing, some struct members and pointers are
> never initialized. There are no flags to indicate that a specific part
> of the struct is not valid,
> and I have no control over the process.

Shoot the guy who wrote a DLL with such crappy error handling :-/
Bad luck.


Cheers,
Stefan
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
Assuming you provide the memory which is then filled by the DLL with data, you could try initializing your memory with zeros first. Then you know which pointers have been initialized, because they're non-zero.

However, you should still shoot the guy who wrote a DLL with such crappy error handling.
0
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
And, did shooting the guy who wrote a DLL with such crappy error handling help? :-)))
0
 

Author Comment

by:jd9288
Comment Utility
It didn't help the program, but it sure made me feel better.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Dll Dynamic way c/c++ 9 133
stack 22 155
Passing a array as parameter - C 2 77
C++ :Change value from  DisableCMD registry 4 9
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now