Orbiter-Forum  

Go Back   Orbiter-Forum > Projects > ORBITER: 2010-P1 and newer > Bug
Register Blogs Orbinauts List Social Groups FAQ Projects Mark Forums Read

oapiGetObjectType can access violate Issue Tools
issueid=1357 01-15-2018 10:32 PM
Scientist
oapiGetObjectType can access violate
passing a stale pointer to oapiGetObjectType can throw Access Violation. Expected behavior is OBJTP_INVALID for all invalid OBJHANDLES

Not a big issue, as you can code around it, but if you pass an invalid OBJHANDLE into oapiGetObjectType(), then it can throw an access violation, rather than giving you the expected OBJTP_INVALID.

I initially assumed a simple try-catch would catch it:

Code:
try {
  objTyp = oapiGetObjectType( hObj );
}
catch (...) {
 objTyp = OBJTP_INVALID;
}
It turns out that this will *not* catch an Access Violation unless you set your VC++ settings to enable SEH exceptions: i.e. Set Project Properties -> C/C++ -> Code Generation -> Modify the Enable C++ Exceptions to "Yes With SEH Exceptions" ( = /EHa flag). You can also set a reminder for future users with a #pragma warning (default : 4571) which will get the compiler to output a warning on the catch line if you are not set for SEH exceptions.

So ... it's trivial to code around this access violation, but it'a reasonable to expect the core Orbiter.exe to do this for you on an Object Type checking call. Hence reporting as a bug.
Issue Details
Project ORBITER: 2010-P1 and newer
Status Unconfirmed
Priority 8
Affected Version 121202
Fixed Version (none)
Users able to reproduce bug 0
Users unable to reproduce bug 0
Assigned Users (none)
Tags (none)

01-16-2018 03:33 PM
O-F Administrator
 
As a side note, in my experience, catching access violations in C++ and trying to continue is never a good idea, because anytime you execute code that reads from or writes to an invalid/trashed/incorrect pointer, then undefined behavior can occur, such as trashing some accessible memory before finally throwing an access violation when reaching the end of the memory page. So while a program can catch access violations and try to keep running, that's a dangerous thing to rely on except in very specific circumstances where you know exactly what the code inside the catch block is doing.
Reply
01-17-2018 01:36 AM
Scientist
 
Quote:
Originally Posted by dbeachy1
 As a side note, in my experience, catching access violations in C++ and trying to continue is never a good idea, because anytime you execute code that reads from or writes to an invalid/trashed/incorrect pointer, then undefined behavior can occur, such as trashing some accessible memory before finally throwing an access violation when reaching the end of the memory page. So while a program can catch access violations and try to keep running, that's a dangerous thing to rely on except in very specific circumstances where you know exactly what the code inside the catch block is doing.
Yup I definitely agree. Hence reporting this as a bug, because I would rather it were cleanly handled.

Context to the discussion: Enjo and I were discussing the most canonical way to pass information about a base, a vessel, or a body, across the all-new Module Messaging Ext message handling code (the one that will shortly be released and will eliminate Error 126 once and for all once MFDs code to the new interface ... with LoadLibrary and GetProcAddress, as we discussed, Doug!). So ... you could

(1) send a C char* for the literal name,

(2) an int representing an object index, or

(3) an OBJHANDLE pointing to the object.

Three edge cases:

(A) object gets renamed,

(B) object gets destroyed, or

(C) another unrelated object gets destroyed which changes the index of a prior-created object.


(1) dies on A and B, but survives C. (2) dies on B and C, but survives A. (3) dies on B, but survives A and C. So OBJHANDLE looks the best bet, but you need to detect if you are pointing into nothingness. Poking bad object addresses with my debugger just prior to the call (to try to simulate an object destruction), I could break oapiGetObjectType quite reliably. I assume the code is just chasing a bad pointer, so hopefully the try/catch/continue is not leaking too badly.
Reply
Reply

Issue Tools
Subscribe to this issue

All times are GMT. The time now is 10:55 PM.

Quick Links Need Help?


About Us | Rules & Guidelines | TOS Policy | Privacy Policy

Orbiter-Forum is hosted at Orbithangar.com
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
Copyright 2007 - 2017, Orbiter-Forum.com. All rights reserved.