flat assembler
Message board for the users of flat assembler.

Index > Heap > Error handling in structured programming

Author
Thread Post new topic Reply to topic
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
i was now coding in C in my work, and i wanted to have working error handling, without leaks. Look how the resulting code looked like:

Code:
                        char* name = SVKP_StringHash(SVKP_company);
                        if (!name) return 0;
                        
                        //try to open registry key Hash(Company)
                        reg = SVKP_RegOpen(from, name);
                        if (!reg) {
                                SVKP_MemFree(name);
                                return -1;
                        }

                        //free Hash(Comapny) from memory
                        if (!SVKP_MemFree(name)) {
                                SVKP_RegClose(reg);
                                return 0;
                        }

                        //get Hash(AppName)
                        name = SVKP_StringHash(SVKP_appname);
                        if (!name) {
                                SVKP_RegClose(reg);
                                return 0;
                        }

                        //open registry key Hash(Company)/Hash(AppName)
                        reg2 = SVKP_RegOpen(reg, name);
                        if (!reg2) {
                                SVKP_MemFree(name);
                                SVKP_RegClose(reg);
                                return -1;
                        }

                        //free Hash(AppName) from memory
                        if (!SVKP_MemFree(name) {
                                SVKP_RegClose(reg);
                                SVKP_RegClose(reg2);
                                return 0;
                        }

                        //close registry key "Hash(Company)" handle
                        if (!SVKP_RegClose(reg)) {
                                SVKP_RegClose(reg2);
                                return 0;
                        }
    

I think this is the clearest way to do it. What are then all those nested call features for? Really i understand why they introduced structured error handling, unfortunately C++ compiles it to some extremelly crappy overbloated code.

I'm looking forward to your opinions
Post 20 Apr 2006, 12:12
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Vasilev Vjacheslav



Joined: 11 Aug 2004
Posts: 392
Vasilev Vjacheslav
i think you can use same return error code (e.g. only -1)
Post 20 Apr 2006, 16:42
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
0 is error, -1 is not found. another shitness of C, you can directly return only one value, in which you must store the value itself + at least additional error-status boolean. Means you have to pack at least two things to one thing, thus reducing that thing.

You can see how it ended up with windoze - some errors returned as 0, some as non-0, some as -1, some as 7FFFFFF, some in argument-pointed variable and some in GetLastError. Only general method is to always call GetLastError, but that is extremelly ugly and trashes your return value in eax
Post 20 Apr 2006, 18:07
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
you could do something like

Code:
#define REPORT_ERROR -1

typedef struct {
    char *detail;
    int error;
}error_t;

error_t e_handler;

unsigned long SomeFunc( void ) {
     if( SOMETHING_FAILED ) {
         e_handler.detail = "i like C coding";
         e_handler.error  = ERROR_CODE;
         return REPORT_ERROR;
     }
}
//---

void main( void ) {
    if( SomeFunc( ) == REPORT_ERROR ) {
        add_log( "%i %s", e_handler.error, e_handler.detail );
    }
}
//---
    

_________________
redghost.ca
Post 21 Apr 2006, 12:19
View user's profile Send private message AIM Address MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
how can you say #define REPORT_ERROR -1? What about func that needs to return signed integer? and do you find code like this clear?

Code:
if (-1 != (a = SomeFunc())) {
  doSomeClearing();
  return;
}    
Post 21 Apr 2006, 13:26
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
vid wrote:
how can you say #define REPORT_ERROR -1? What about func that needs to return signed integer? and do you find code like this clear?

Code:
if (-1 != (a = SomeFunc())) {
  doSomeClearing();
  return;
}    


solution:
Code:
#define REPORT_ERROR 0    


or just

Code:
if ( !( a = SomeFunc( ) ) ) {
   //failed: check error struct
}
    


or take it further

Code:
#define ASSIGN_ERROR !
   
if( ASSIGN_ERROR( a = Func( ) ) ) {
    //check error struct
}
    


Cool

_________________
redghost.ca
Post 21 Apr 2006, 13:38
View user's profile Send private message AIM Address MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
0 is valid returnable integer too. Best applicable C solution is global variable. But it is still quite messy. IMO returning error status in same variable as return value is one of biggest programming design mistakes in history.
Post 21 Apr 2006, 13:51
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Kermil



Joined: 26 Oct 2005
Posts: 35
Location: Russia
Kermil
2vid, why do you think so?
Post 21 Apr 2006, 14:14
View user's profile Send private message ICQ Number Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
do you know any other solution how to be able to return pointers, signed, and unsigned numbers with same interface?
Post 21 Apr 2006, 14:21
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
vid wrote:
do you know any other solution how to be able to return pointers, signed, and unsigned numbers with same interface?


use inline asm and check for the carry flag which you set then check the error # in eax Razz

_________________
redghost.ca
Post 21 Apr 2006, 17:39
View user's profile Send private message AIM Address MSN Messenger Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
RedGhost: That is not a solution, it's ugly to have code like that. Also in inline assembly you cannot create C statement, so you have to jump at label. etc etc
Post 21 Apr 2006, 17:57
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
RedGhost



Joined: 18 May 2005
Posts: 443
Location: BC, Canada
RedGhost
vid wrote:
RedGhost: That is not a solution, it's ugly to have code like that. Also in inline assembly you cannot create C statement, so you have to jump at label. etc etc


well it was just a joke but you can do

Code:
__asm jmp OK

OK:
    //C code here
    


so if you really wanted to you could do something like

Code:

#define err_call( call, err )\
    call;\
    __asm {\
        __asm jc err\
    }
//---

#define set_carry( ) __asm { __asm stc }

int foo( int a, int b, int c ) {
    if( ERROR ) {
        set_carry( );
        return ERROR_CODE;
    }
}
//---
        


err_call( foo( 1, 2, 3 ), foo_ERROR );

foo_ERROR:
    


but this might make the source seem messy

i think using a struct/variable is the only solution

_________________
redghost.ca
Post 21 Apr 2006, 18:03
View user's profile Send private message AIM Address MSN Messenger Reply with quote
Kermil



Joined: 26 Oct 2005
Posts: 35
Location: Russia
Kermil
vid wrote:
how can you say #define REPORT_ERROR -1? What about func that needs to return signed integer? and do you find code like this clear?

Code:
if (-1 != (a = SomeFunc())) {
  doSomeClearing();
  return;
}    


I don't understand essence of a question. If you want return some signed, unsigned integer or pointer, then transfer pointer on the variable to function.

Code:
int SomeFunc(int *a)
{
    *a = -72;
    //...
    return -1;
}

int b;

if (-1 != (a = SomeFunc(&b))) {
  doSomeClearing();
  return;
}
else doSomethingElse(b);

    


Last edited by Kermil on 22 Apr 2006, 09:15; edited 1 time in total
Post 22 Apr 2006, 07:46
View user's profile Send private message ICQ Number Reply with quote
Borsuc



Joined: 29 Dec 2005
Posts: 2466
Location: Bucharest, Romania
Borsuc
vid wrote:
0 is valid returnable integer too. Best applicable C solution is global variable. But it is still quite messy. IMO returning error status in same variable as return value is one of biggest programming design mistakes in history.

That's a problem I really hate in C -- you can only return one value (in registers).

anyway, vid, returning in global variables is not a mistake. Don't tell yourself "it is evil", because it can be useful or necessary to understand low-level things, believe me. So, it can sometimes be very useful. Wink
Post 22 Apr 2006, 09:03
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid
The_Grey_Beast wrote:
anyway, vid, returning in global variables is not a mistake. Don't tell yourself "it is evil", because it can be useful or necessary to understand low-level things, believe me. So, it can sometimes be very useful. Wink

I am not that kind of guy, i use global error variable in FASMLIB (but additionaly error status is returned in CF for easy use). It's somewhat slower but most of times that really doesn't matter in ASM or even C.
Post 22 Apr 2006, 09:29
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on YouTube, Twitter.

Website powered by rwasa.