Changeset 473

Show
Ignore:
Timestamp:
08/09/07 10:01:25 (1 year ago)
Author:
takkaria
Message:

Fix bug where trying to add a new score to a non-existent scorefile crashes.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/files.c

    r469 r473  
    30303030        new = file_open(new_name, MODE_WRITE, FTYPE_RAW); 
    30313031        lok = file_open(lok_name, MODE_WRITE, FTYPE_RAW); 
    3032         file_lock(lok); 
     3032        if (new && lok) 
     3033                file_lock(lok); 
    30333034        safe_setuid_drop(); 
    30343035 
     3036        if (!new || !lok) return -1; 
    30353037 
    30363038        /* Determine where the score should go */ 
    3037         slot = highscore_where(old, score); 
    3038  
    3039         /* Read entries from the old and write them to the new */ 
    3040         for (i = 0; (i < MAX_HISCORES) && !error; i++) 
    3041         { 
    3042                 if (!highscore_seek(old, i)) return (-1); 
    3043  
    3044                 /* Insert the new one at the right slot */ 
    3045                 if (i == slot) 
    3046                 { 
    3047                         if (!file_write(new, (const char *)score, sizeof(high_score))) 
    3048                         { 
    3049                                 error = TRUE; 
    3050                                 slot = -1; 
    3051                         } 
    3052                 } 
    3053  
    3054                 /* Read old one, write again */ 
    3055                 if (highscore_read(old, &tmpscore)) 
    3056                 { 
    3057                         if (!file_write(new, (const char *)&tmpscore, sizeof(high_score))) 
    3058                         { 
    3059                                 error = TRUE; 
    3060                                 slot = -1; 
    3061                         } 
     3039        if (old) 
     3040        { 
     3041                slot = highscore_where(old, score); 
     3042 
     3043                /* Read entries from the old and write them to the new */ 
     3044                for (i = 0; (i < MAX_HISCORES) && !error; i++) 
     3045                { 
     3046                        if (!highscore_seek(old, i)) return (-1); 
     3047 
     3048                        /* Insert the new one at the right slot */ 
     3049                        if (i == slot) 
     3050                        { 
     3051                                if (!file_write(new, (const char *)score, sizeof(high_score))) 
     3052                                { 
     3053                                        error = TRUE; 
     3054                                        slot = -1; 
     3055                                } 
     3056                        } 
     3057         
     3058                        /* Read old one, write again */ 
     3059                        if (highscore_read(old, &tmpscore)) 
     3060                        { 
     3061                                if (!file_write(new, (const char *)&tmpscore, sizeof(high_score))) 
     3062                                { 
     3063                                        error = TRUE; 
     3064                                        slot = -1; 
     3065                                } 
     3066                        } 
     3067                } 
     3068 
     3069                file_close(old); 
     3070        } 
     3071        else 
     3072        { 
     3073                slot = 0; 
     3074                if (!file_write(new, (const char *)score, sizeof(high_score))) 
     3075                { 
     3076                        error = TRUE; 
     3077                        slot = -1; 
    30623078                } 
    30633079        } 
    30643080 
    30653081        file_close(new); 
    3066         file_close(old); 
    30673082 
    30683083        /* Move things around */