Changeset 2591


Ignore:
Timestamp:
07/27/08 10:25:32 (5 years ago)
Author:
pterjan
Message:
  • Fix a double free and free everything in case of error
File:
1 edited

Legend:

Unmodified
Added
Removed
  • neercs/trunk/src/main.c

    r2589 r2591  
    8484    int i, args, s=0; 
    8585    long long unsigned int last_key_time = 0; 
    86     int mainret = 0; 
     86    int mainret = -1; 
    8787    int attach = 0, forceattach = 0; 
    8888    int *to_grab = NULL; 
     
    9797    { 
    9898        fprintf(stderr, "Environment variable SHELL not set and no arguments given. kthxbye.\n"); 
    99         return -1; 
     99        goto end; 
    100100    } 
    101101 
     
    125125    { 
    126126        fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 
    127         return -1; 
     127        goto end; 
    128128    } 
    129129    screen_list->recurrent_list->count = 0; 
     
    179179                { 
    180180                    fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 
    181                     return -1; 
     181                    goto end; 
    182182                } 
    183183            } 
     
    192192            { 
    193193                fprintf(stderr, "Attaching can only be requested once\n"); 
    194                 return -1; 
     194                goto end; 
    195195            } 
    196196            if(myoptarg) 
     
    206206        case 'h': /* --help */ 
    207207            usage(argc, argv); 
    208             return 0; 
     208            mainret = 0; 
     209            goto end; 
    209210            break; 
    210211        case 'v': /* --version */ 
    211212            version(); 
    212             return 0; 
     213            mainret = 0; 
     214            goto end; 
    213215            break; 
    214216        default: 
    215217            fprintf(stderr, "Unknown argument #%d\n", myoptind); 
    216             return -1; 
     218            goto end; 
    217219            break; 
    218220        } 
     
    225227        { 
    226228            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 
    227             return -1; 
     229            goto end; 
    228230        } 
    229231        for(i=0; i<(argc-1) - s; i++) 
     
    250252        { 
    251253            fprintf(stderr, "-R can not be associated with commands or pids!\n"); 
    252             return -1; 
     254            goto end; 
    253255        } 
    254256 
     
    259261            for(i=0; sockets[i]; i++); 
    260262            i--; 
    261             screen_list->s_socket_path = sockets[i]; 
     263            screen_list->s_socket_path = strdup(sockets[i]); 
    262264            session = connect_server(screen_list); 
    263265            if(session) 
     
    267269                screen_list->dp = caca_create_display(screen_list->cv); 
    268270                if(!screen_list->dp) 
    269                     return 1; 
     271                    goto end; 
    270272                caca_set_cursor(screen_list->dp, 1); 
    271273 
     
    294296        } 
    295297        if(forceattach && !attach) 
    296             return -1; 
     298            goto end; 
    297299    } 
    298300 
     
    307309        { 
    308310            fprintf(stderr, "Can't allocate memory at %s:%d\n", __FUNCTION__, __LINE__); 
    309             return -1; 
     311            goto end; 
    310312        } 
    311313    } 
     
    327329        { 
    328330            fprintf(stderr, "Failed to create child process\n"); 
    329             return -1; 
     331            goto end; 
    330332        } 
    331333        if(pid == 0) 
     
    361363        screen_list->dp = caca_create_display(screen_list->cv); 
    362364        if(!screen_list->dp) 
    363             return 1; 
     365            goto end; 
    364366        caca_set_cursor(screen_list->dp, 1); 
    365367 
     
    417419 
    418420    /* Clean up */ 
    419     if(screen_list->dp) 
    420         caca_free_display(screen_list->dp); 
    421  
    422     cucul_free_canvas(screen_list->cv); 
    423  
    424     for(i = 0; i < screen_list->count; i++) 
    425     { 
    426         destroy_screen(screen_list->screen[i]); 
    427     } 
    428  
    429     if(screen_list->s_socket_path) 
    430         free(screen_list->s_socket_path); 
    431  
    432     if(screen_list->c_socket_path) 
    433     { 
    434         unlink(screen_list->c_socket_path); 
    435         free(screen_list->c_socket_path); 
    436     } 
    437  
    438     if(screen_list->c_socket) 
    439         close(screen_list->c_socket); 
    440  
    441     if(screen_list->s_socket) 
    442         close(screen_list->s_socket); 
    443  
    444     if(screen_list->screen) free(screen_list->screen); 
    445  
    446  
    447     struct option *option = screen_list->config; 
    448  
    449     while(option) 
    450     { 
    451         struct option *kromeugnon = option; 
    452         option = option->next; 
    453         if(kromeugnon->key)   free(kromeugnon->key); 
    454         if(kromeugnon->value) free(kromeugnon->value); 
    455         free(kromeugnon); 
    456     } 
    457  
    458     for(i=0; i<screen_list->recurrent_list->count; i++) 
    459     { 
    460         remove_recurrent(screen_list->recurrent_list, i); 
    461         i = 0; 
    462     } 
    463  
    464     if(screen_list->recurrent_list->recurrent) free(screen_list->recurrent_list->recurrent); 
    465     if(screen_list->recurrent_list)            free(screen_list->recurrent_list); 
    466  
    467     if(screen_list->session_name) 
    468         free(screen_list->session_name); 
    469  
     421    mainret = 0; 
     422end: 
    470423    if(screen_list) 
     424    { 
     425        if(screen_list->dp) 
     426            caca_free_display(screen_list->dp); 
     427 
     428        if(screen_list->cv) 
     429            cucul_free_canvas(screen_list->cv); 
     430 
     431        for(i = 0; i < screen_list->count; i++) 
     432        { 
     433            destroy_screen(screen_list->screen[i]); 
     434        } 
     435 
     436        if(screen_list->s_socket_path) 
     437            free(screen_list->s_socket_path); 
     438 
     439        if(screen_list->c_socket_path) 
     440        { 
     441            unlink(screen_list->c_socket_path); 
     442            free(screen_list->c_socket_path); 
     443        } 
     444 
     445        if(screen_list->c_socket) 
     446            close(screen_list->c_socket); 
     447 
     448        if(screen_list->s_socket) 
     449            close(screen_list->s_socket); 
     450 
     451        if(screen_list->screen) 
     452            free(screen_list->screen); 
     453 
     454        struct option *option = screen_list->config; 
     455 
     456        while(option) 
     457        { 
     458            struct option *kromeugnon = option; 
     459            option = option->next; 
     460            if(kromeugnon->key)   free(kromeugnon->key); 
     461            if(kromeugnon->value) free(kromeugnon->value); 
     462            free(kromeugnon); 
     463        } 
     464 
     465        for(i=0; i<screen_list->recurrent_list->count; i++) 
     466        { 
     467            remove_recurrent(screen_list->recurrent_list, i); 
     468            i = 0; 
     469        } 
     470 
     471        if(screen_list->recurrent_list->recurrent) 
     472            free(screen_list->recurrent_list->recurrent); 
     473        if(screen_list->recurrent_list) 
     474            free(screen_list->recurrent_list); 
     475 
     476        if(screen_list->session_name) 
     477            free(screen_list->session_name); 
     478 
    471479        free(screen_list); 
     480    } 
    472481 
    473482    return mainret; 
Note: See TracChangeset for help on using the changeset viewer.