should i use try/finally or not

Hello,

as I am working on windows driver development for over year but still find
difficulty whether should i use try/finally or not.

Is there any concrete documentation from MS?

Or should i continue using of try/finally?

Is there pro and cons of try/finally?

You should not if you don’t want.

You must use try/except in some cases but there is no any requirement for try/finally.

It’s a stylistic issue, more than anything.

In MY opinion, try/finally is a terrible programming practice. But there are people that love it, and argue that its ability to clean-up (and drop the right locks, for example) is a useful one. I just find it confusing.

It’s up to you… There’s no special guidance from the driver world in this regard.

Peter
OSR
@OSRDrivers

> In MY opinion, try/finally is a terrible programming practice.

Hardly surprising, taking into consideration your opinion (in fact, not only yours) on the language by which this particular feature was, apparently, inspired. Certainly, try/finally construct is not as awful as try/catch/throw one, but still it is sort of confusing.OTOH, if you are just desperate to do something ugly and confusing you can do the same abomination in ANSI C as well - setjmp/longjmp pair is going to be your aid here. For example, consider the following “masterpiece” that I have shamelessly copied from
http://www.di.unipi.it/~nids/docs/longjump_try_trow_catch.html

#include <stdio.h>
#include <setjmp.h>

#define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
#define CATCH } else {
#define ETRY } }while(0)
#define THROW longjmp(ex_buf__, 1)

int
main(int argc, char** argv)
{
TRY
{
printf(“In Try Statement\n”);
THROW;
printf(“I do not appear\n”);
}
CATCH
{
printf(“Got Exception!\n”);
}
ETRY;

return 0;
}

Anton Bassov</setjmp.h></stdio.h>

Thanks All,

@peter - how try/finally is a terrible programming practice?, Did you mean that __try __finally will consume more stack and it is wrong, so use of SEH should be limited to for API’s which throws exception.

Or is there any other reasons as well?

Does SEH prohibit the compiler from making optimizations to the code?

I looked yesterday. _try/__finally consumes an entire 16 bytes of stack. I
suppose that might be super critical in some situation.

Mark Roddy

On Wed, Oct 5, 2016 at 2:36 AM, wrote:

> Thanks All,
>
> @peter - how try/finally is a terrible programming practice?, Did you mean
> that try finally will consume more stack and it is wrong, so use of SEH
> should be limited to for API’s which throws exception.
>
> Or is there any other reasons as well?
>
> Does SEH prohibit the compiler from making optimizations to the code?
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:>

Yes SEH does prevent the optimizer from working on code in the block

From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Mark Roddy
Sent: Wednesday, October 5, 2016 3:46 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] should i use try/finally or not

I looked yesterday. _try/ finally consumes an entire 16 bytes of stack. I suppose that might be super critical in some situation.

Mark Roddy

On Wed, Oct 5, 2016 at 2:36 AM, > wrote:
Thanks All,

@peter - how try/finally is a terrible programming practice?, Did you mean that
try __finally will consume more stack and it is wrong, so use of SEH should be limited to for API’s which throws exception.

Or is there any other reasons as well?

Does SEH prohibit the compiler from making optimizations to the code?


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at</http:></http:></http:>

Doron, what about preventing SDV and code analysis tools? I think goto statement interfered to them, so there should be the same with except/finally I guess.

One more reasons not to throw handlers here and there is security. EXCEPTION_RECORD structs might help to bypass stack buffer overflow mitigations. So, unless you know that the compiler and target OS are capable of exploitation hardening better use handlers with care.

I don’t know of an code analysis issues, but that doesn’t mean they don’t exist.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@yahoo.com
Sent: Wednesday, October 5, 2016 2:26 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] should i use try/finally or not

Doron, what about preventing SDV and code analysis tools? I think goto statement interfered to them, so there should be the same with except/finally I guess.

One more reasons not to throw handlers here and there is security. EXCEPTION_RECORD structs might help to bypass stack buffer overflow mitigations. So, unless you know that the compiler and target OS are capable of exploitation hardening better use handlers with care.


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:>

Whoa… What?

I am not aware of any issue with “goto” and either SDV or CA. If this is a known or documented issue, I’d really like to know about it.

Peter
OSR
@OSRDrivers

I don?t know anything specific about goto, but the latest MS (VS 2015 based) compilers are much worse than the previous generation (VS 2012 in my case) for code analysis at both false positives and false negatives. In particular any function that employs dependent variables is in rough shape re the output. Now one can argue that dependent variables should be avoided, but it is precisely this kind of code that can be difficult to follow manually where static analysis ought to provide the most benefit. I was about to cite some egregious examples except it seems that after I installed update 3, many have been resolved. The problem that anything declared in a header file is doomed remains though. Like this one

Acquires_lock(pQueue->cs)

__inline void QUEUE_Lock(In QUEUE* const pQueue)

{

#ifdef _DEBUG

if(!pQueue->bLock)

{

DebugBreak();

}

#endif

EnterCriticalSection(&pQueue->cs);

}

Severity Code Description Project File Line Suppression State

Warning C26135 Missing annotation Acquires_lock(pQueue->cs) at function ‘QUEUE_Lock’. CORE c:\aprg\fix platform\src\lib\core\queue\queue.h 363 Active

Sent from Mailhttps: for Windows 10

From: xxxxx@osr.commailto:xxxxx
Sent: October 5, 2016 6:25 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: RE:[ntdev] should i use try/finally or not



Whoa… What?

I am not aware of any issue with “goto” and either SDV or CA. If this is a known or documented issue, I’d really like to know about it.

Peter
OSR
@OSRDrivers


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:>

I never heard of anything about code analysis issues with GOTO’s. When
Prefast came out, as a former compiler guy I talked to a lot of the
principals of that team, and much of the guts of the Visual C Compiler are
used for tool. I would love to see an example of what someone claims messes
up the code analysis (note I am not talking about SDV), because I’ve sure
never heard this claim.

Don Burn
Windows Driver Consulting
Website: http://www.windrvr.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Doron Holan
Sent: Wednesday, October 05, 2016 6:11 PM
To: Windows System Software Devs Interest List
Subject: RE: RE:[ntdev] should i use try/finally or not

I don’t know of an code analysis issues, but that doesn’t mean they don’t
exist.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of xxxxx@yahoo.com
Sent: Wednesday, October 5, 2016 2:26 PM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] should i use try/finally or not

Doron, what about preventing SDV and code analysis tools? I think goto
statement interfered to them, so there should be the same with
except/finally I guess.

One more reasons not to throw handlers here and there is security.
EXCEPTION_RECORD structs might help to bypass stack buffer overflow
mitigations. So, unless you know that the compiler and target OS are capable
of exploitation hardening better use handlers with care.


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:


NTDEV is sponsored by OSR

Visit the list online at:
http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software
drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at
http:</http:></http:></http:></http:></http:></http:>

Doron, do you have any more information about this? A link or something?
Does it prevent any optimization for the entire block, or certain
optimizations? I haven’t heard of this before, and was just adding some SEH
to my driver after reading some parts of Rajeev Nagar’s book.

On Wed, Oct 5, 2016 at 9:31 AM, Doron Holan
wrote:

> Yes SEH does prevent the optimizer from working on code in the block
>
>
>
> From: xxxxx@lists.osr.com [mailto:bounce-617220-26293@
> lists.osr.com] *On Behalf Of *Mark Roddy
> Sent: Wednesday, October 5, 2016 3:46 AM
> To: Windows System Software Devs Interest List
> Subject: Re: [ntdev] should i use try/finally or not
>
>
>
> I looked yesterday. _try/ finally consumes an entire 16 bytes of stack. I
> suppose that might be super critical in some situation.
>
>
>
>
> Mark Roddy
>
>
>
> On Wed, Oct 5, 2016 at 2:36 AM, wrote:
>
> Thanks All,
>
> @peter - how try/finally is a terrible programming practice?, Did you mean
> that
try __finally will consume more stack and it is wrong, so use of SEH
> should be limited to for API’s which throws exception.
>
> Or is there any other reasons as well?
>
> Does SEH prohibit the compiler from making optimizations to the code?
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
>
>
>
> — NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars
> on crash dump analysis, WDF, Windows internals and software drivers!
> Details at To unsubscribe, visit the List Server section of OSR Online at
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:></http:></http:>

This is an implementation detail. The entire block will not be optimized as the compiler can’t know which instruction will raise an exception, so it plays it safe.

Get Outlook for Androidhttps:

________________________________
From: xxxxx@lists.osr.com on behalf of Jeremy Hurren
Sent: Wednesday, October 5, 2016 8:58:11 PM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] should i use try/finally or not

Doron, do you have any more information about this? A link or something? Does it prevent any optimization for the entire block, or certain optimizations? I haven’t heard of this before, and was just adding some SEH to my driver after reading some parts of Rajeev Nagar’s book.

On Wed, Oct 5, 2016 at 9:31 AM, Doron Holan > wrote:
Yes SEH does prevent the optimizer from working on code in the block

From: xxxxx@lists.osr.commailto:xxxxx [mailto:xxxxx@lists.osr.commailto:xxxxx] On Behalf Of Mark Roddy
Sent: Wednesday, October 5, 2016 3:46 AM
To: Windows System Software Devs Interest List >
Subject: Re: [ntdev] should i use try/finally or not

I looked yesterday. _try/__finally consumes an entire 16 bytes of stack. I suppose that might be super critical in some situation.

Mark Roddy

On Wed, Oct 5, 2016 at 2:36 AM, > wrote:
Thanks All,

@peter - how try/finally is a terrible programming practice?, Did you mean that try finally will consume more stack and it is wrong, so use of SEH should be limited to for API’s which throws exception.

Or is there any other reasons as well?

Does SEH prohibit the compiler from making optimizations to the code?


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at</http:></http:></http:></http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:>

thanks all,

After reading all posts and some hand on POC.

My considerations -

1] Avoid use of __try/__finally, only in special cases we should use of them.
And
2] Use GOTO statement for structured coding(cleanup and closing handle etc…)

So Am I on right track ?

Agreed. When the compiler was re-written, the guts of how CA works required similar revision. My experience, like Mr, Bond’s, s that the new compiler’s CA does a *much* poorer job than the previous version. Having said that, it gets better with each revision.

Now… having said all that… I’d still like to hear somebody tell me that using a goto does anything bad to CA.

Peter
OSR
@OSRDrivers

  1. wrap the goto in TRY FINALLY LEAVE macros and make everybody happy :slight_smile:

Mark Roddy

On Thu, Oct 6, 2016 at 6:31 AM, wrote:

> thanks all,
>
> After reading all posts and some hand on POC.
>
> My considerations -
>
> 1] Avoid use of try/ finally, only in special cases we should use of
> them.
> And
> 2] Use GOTO statement for structured coding(cleanup and closing handle
> etc…)
>
> So Am I on right track ?
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:>

I use the __try __finally pattern quite often. I also regularly use the goto abortNN pattern and the choice depends mostly on the kind of function being written.

when writing a function that acquires a resource that must be freed in the same context, the __try __finally pattern works quite well. usually, the inner block is simply a function call so the ability to optimize the code by the compiler is quite irrelevant for example

pResouce = LookupXWithReferencebyY(y)

if(pResource == NULL)

{

Sent from Outlookhttp:


From: xxxxx@lists.osr.com on behalf of Doron Holan
Sent: October 6, 2016 12:46:57 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] should i use try/finally or not

This is an implementation detail. The entire block will not be optimized as the compiler can’t know which instruction will raise an exception, so it plays it safe.

Get Outlook for Androidhttps:


From: xxxxx@lists.osr.com on behalf of Jeremy Hurren
Sent: Wednesday, October 5, 2016 8:58:11 PM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] should i use try/finally or not

Doron, do you have any more information about this? A link or something? Does it prevent any optimization for the entire block, or certain optimizations? I haven’t heard of this before, and was just adding some SEH to my driver after reading some parts of Rajeev Nagar’s book.

On Wed, Oct 5, 2016 at 9:31 AM, Doron Holan > wrote:
Yes SEH does prevent the optimizer from working on code in the block

From: xxxxx@lists.osr.commailto:xxxxx [mailto:xxxxx@lists.osr.commailto:xxxxx] On Behalf Of Mark Roddy
Sent: Wednesday, October 5, 2016 3:46 AM
To: Windows System Software Devs Interest List >
Subject: Re: [ntdev] should i use try/finally or not

I looked yesterday. _try/ finally consumes an entire 16 bytes of stack. I suppose that might be super critical in some situation.

Mark Roddy

On Wed, Oct 5, 2016 at 2:36 AM, > wrote:
Thanks All,

@peter - how try/finally is a terrible programming practice?, Did you mean that
try __finally will consume more stack and it is wrong, so use of SEH should be limited to for API’s which throws exception.

Or is there any other reasons as well?

Does SEH prohibit the compiler from making optimizations to the code?


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:></http:></http:></http:></http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:></http:>

Notwithstanding the probity of my responses, it is possible that I will live long enough to make any sense of the new MS mail. I thought I was at least in the upper half of the IQ range, but my recent experiences suggest not

Sent from Mailhttps: for Windows 10

From: Marion Bondmailto:xxxxx
Sent: October 6, 2016 9:24 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: Re: [ntdev] should i use try/finally or not

I use the try finally pattern quite often. I also regularly use the goto abortNN pattern and the choice depends mostly on the kind of function being written.

when writing a function that acquires a resource that must be freed in the same context, the try finally pattern works quite well. usually, the inner block is simply a function call so the ability to optimize the code by the compiler is quite irrelevant for example

pResouce = LookupXWithReferencebyY(y)

if(pResource == NULL)

{

Sent from Outlookhttp:


From: xxxxx@lists.osr.com on behalf of Doron Holan
Sent: October 6, 2016 12:46:57 AM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] should i use try/finally or not

This is an implementation detail. The entire block will not be optimized as the compiler can’t know which instruction will raise an exception, so it plays it safe.

Get Outlook for Androidhttps:


From: xxxxx@lists.osr.com on behalf of Jeremy Hurren
Sent: Wednesday, October 5, 2016 8:58:11 PM
To: Windows System Software Devs Interest List
Subject: Re: [ntdev] should i use try/finally or not

Doron, do you have any more information about this? A link or something? Does it prevent any optimization for the entire block, or certain optimizations? I haven’t heard of this before, and was just adding some SEH to my driver after reading some parts of Rajeev Nagar’s book.

On Wed, Oct 5, 2016 at 9:31 AM, Doron Holan > wrote:
Yes SEH does prevent the optimizer from working on code in the block

From: xxxxx@lists.osr.commailto:xxxxx [mailto:xxxxx@lists.osr.commailto:xxxxx] On Behalf Of Mark Roddy
Sent: Wednesday, October 5, 2016 3:46 AM
To: Windows System Software Devs Interest List >
Subject: Re: [ntdev] should i use try/finally or not

I looked yesterday. _try/ finally consumes an entire 16 bytes of stack. I suppose that might be super critical in some situation.

Mark Roddy

On Wed, Oct 5, 2016 at 2:36 AM, > wrote:
Thanks All,

@peter - how try/finally is a terrible programming practice?, Did you mean that
try __finally will consume more stack and it is wrong, so use of SEH should be limited to for API’s which throws exception.

Or is there any other reasons as well?

Does SEH prohibit the compiler from making optimizations to the code?


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:

— NTDEV is sponsored by OSR Visit the list online at: MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers! Details at To unsubscribe, visit the List Server section of OSR Online at


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:></http:></http:></http:></http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:></http:></mailto:xxxxx></mailto:xxxxx></https:>

Marion Bond wrote:

I use the __try __finally pattern quite often. I also regularly use
the goto abortNN pattern and the choice depends mostly on the kind
of function being written.

when writing a function that acquires a resource that must be freed in
the same context, the __try __finally pattern works quite well.
usually, the inner block is simply a function call so the ability to
optimize the code by the compiler is quite irrelevant for example

pResouce = LookupXWithReferencebyY(y)

if(pResource == NULL)

{

I cannot help but point out that C++, with RAII and scope-based
destructors, supports all of this quite naturally.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.