473,403 Members | 2,354 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,403 software developers and data experts.

Memory (read) access violation handling in C

I have a structure defined like this:

struct foo
{
unsigned int magic ;
void *mydata ;
};

I have macros and defines like this :

#define MAGIC (0xFABF00D)
#define ISVALID_PTR(ptr) if(ptr)((ptr)->magic == MAGIC) ? 1 : 0) \ else 0

When a "bad pointer" - ( an arbitrary integer for example) is passed to
the macro, my library crashes (as one may well expect).

I am providing this C interface into another language, where there is a
great possibility of misuse and integers may be passed (accidentally) to
my functions, which use this validation macro above. I want my library
to be robust in the presence of such errors - in otherwords, I need to
be able to handle memory (READ) access violations gracefully and to be
able to recover from them - it is fairly trivial to do this in C++, but
I can't seem to find a way to do this in C.

Any solutions ?

Jun 18 '06 #1
4 4024
Bit byte wrote:
#define ISVALID_PTR(ptr) if(ptr)((ptr)->magic == MAGIC) ? 1 : 0)


That macro can't do anything.
It's part of an if statement, so it has no value,
and the statement has no side effects.
I would write that, this way:

#define ISVALID_PTR(ptr) (ptr != NULL && ptr -> magic == MAGIC)
--
pete
Jun 18 '06 #2
Bit byte <fl**@flop.com> writes:
I have a structure defined like this:

struct foo
{
unsigned int magic ;
void *mydata ;
};

I have macros and defines like this :

#define MAGIC (0xFABF00D)
#define ISVALID_PTR(ptr) if(ptr)((ptr)->magic == MAGIC) ? 1 : 0) \ else 0

When a "bad pointer" - ( an arbitrary integer for example) is passed
to the macro, my library crashes (as one may well expect).

I am providing this C interface into another language, where there is
a great possibility of misuse and integers may be passed
(accidentally) to my functions, which use this validation macro
above. I want my library to be robust in the presence of such errors -
in otherwords, I need to be able to handle memory (READ) access
violations gracefully and to be able to recover from them - it is
fairly trivial to do this in C++, but I can't seem to find a way to do
this in C.


There's no way in C to test whether a pointer is valid. You can
easily test whether a pointer is null, but if it's non-null garbage,
any attempt even to look at its value invokes undefined behavior.

<OT>I'm surprised that C++ provides a way to detect this; I would have
thought it also says this is undefined behavior.</OT>

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 18 '06 #3

Bit byte wrote:
I have a structure defined like this:

struct foo
{
unsigned int magic ;
void *mydata ;
};

I have macros and defines like this :

#define MAGIC (0xFABF00D)
#define ISVALID_PTR(ptr) if(ptr)((ptr)->magic == MAGIC) ? 1 : 0) \ else 0

When a "bad pointer" - ( an arbitrary integer for example) is passed to
the macro, my library crashes (as one may well expect).

I am providing this C interface into another language, where there is a
great possibility of misuse and integers may be passed (accidentally) to
my functions, which use this validation macro above. I want my library
to be robust in the presence of such errors - in otherwords, I need to
be able to handle memory (READ) access violations gracefully and to be
able to recover from them - it is fairly trivial to do this in C++, but
I can't seem to find a way to do this in C.

Any solutions ?


An arbitrary integer may happen to have a valid value for a pointer. So
I
don't think that there is a completely reliable way to check whether a
pointer
points to garbage. But if your concern is to check whether a pointer
points
to an area of memory from which your programme is not allowed to read
then the only possible **non-standard/non-portable** solution I can
think of is the following: it may be that your operating system sends
some
specific signal to a programme if it tries to read from memory it is
not
supposed to access. If this is the case then you should be able to do
something
useful using the signal() function. So I would suggest that you check
your
operating system's documentation.

Jun 18 '06 #4
On Sun, 18 Jun 2006 19:56:45 GMT, Keith Thompson <ks***@mib.org>
wrote:
Bit byte <fl**@flop.com> writes:
I am providing this C interface into another language, where there is
a great possibility of misuse and integers may be passed
(accidentally) to my functions, which use this validation macro
above. I want my library to be robust in the presence of such errors -
in otherwords, I need to be able to handle memory (READ) access
violations gracefully and to be able to recover from them - it is
fairly trivial to do this in C++, but I can't seem to find a way to do
this in C.


There's no way in C to test whether a pointer is valid. You can
easily test whether a pointer is null, but if it's non-null garbage,
any attempt even to look at its value invokes undefined behavior.

Not in standard C, the topic here. However, the conversion from
pointer types to (suitable) integers is nonnormatively "intended to
be consistent with the addressing structure of the execution
environment" and on all or nearly so platforms even indeterminate or
dangling pointers do give _some_ value which can usefully be analyzed
but only in a platform-dependent way.
<OT>I'm surprised that C++ provides a way to detect this; I would have
thought it also says this is undefined behavior.</OT>


Standard C++ does not. It does however provide syntax and mechanism
for exceptions, and _some_ implementations which are able to catch
invalid memory accesses, or other 'hardware' faults like zerodivide,
choose to have them raise platform-defined exceptions -- which can
then be handled using basically standard constructs.

_On these platforms_ using (standard C) signal() to establish a
handler for SIGSEGV or similar is very likely to work as well.

As already noted this is only a half-measure; on almost all systems it
is trivial to construct, and often easy to get by accident, pointer
values which don't cause an immediate fault but are nonetheless
invalid and when used cause horribly bad results. (I once had a really
bad Heisenbug of this type; if the program was run under the debugger
the invalid pointer it used happened to be benign, but if run without
the debugger it got a different invalid pointer which caused a crash.)

- David.Thompson1 at worldnet.att.net
Jun 26 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bengt Richter | last post by:
Python 2.3.2 (#49, Oct 2 2003, 20:02:00) on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import time >>> time.mktime((1969, 12, 31, 17, 0, 0, 0, 0, 0))...
15
by: Steven Reddie | last post by:
I understand that access violations aren't part of the standard C++ exception handling support. On Windows, a particular MSVC compiler option enables Microsoft's Structured Exception Handling...
0
by: Steven Reddie | last post by:
In article <slrnbnj19j.av.juergen@monocerus.manannan.org>, Juergen Heinzl wrote: >In article <f93791bd.0309282133.650da850@posting.google.com>, Steven Reddie wrote: >> I understand that access...
3
by: Kyle Teague | last post by:
I have a list of pointers to structs as a private member of a class. If I call begin() in the same function as I added the data then no access violation occurs. However, if I try to call begin() in...
1
by: BillyO | last post by:
In the attached code fragment I have a buffer overflow and a memory access violation. When I run the code .Net fails to verify the IL because of the buffer overflow and I get an exception as...
12
by: aling | last post by:
Have following code snip: struct struc { int member1; int member2; } ; printf("&((struc*)0)->member2=%p\n", &((struc*)0)->member2); In VC7.1, the output is 4, the offset of member2 in struc.
6
by: mangesh | last post by:
1 - How to cach invalid memory access exception ? Does standard library provide any help ? 2 - Also when one write catch(...) { //........... } what is wirtten inside catch block . How do...
1
by: Beorne | last post by:
I have imported a corporate image handling COM object in my C# project. To access in a fast way the memory of the image there is a method that returns a pointer to the memory (in byte) of the...
2
by: gaurav kashyap | last post by:
Dear all, I have a server program that listens to a particular port and a number of client programs that connect to the server. Now i want to put some data in form of python list in main...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.