Ticket #2861: 0001-Additional-error-handling-in-edit_save_block.patch

File 0001-Additional-error-handling-in-edit_save_block.patch, 1.8 KB (added by zaytsev, 12 years ago)
  • src/editor/editcmd.c

    From 68d6dfb9996a5e143a981c6c902009fa68414e71 Mon Sep 17 00:00:00 2001
    From: "Yury V. Zaytsev" <yury@shurup.com>
    Date: Sat, 4 Aug 2012 18:55:07 +0200
    Subject: [PATCH] Additional error handling in edit_save_block()
    
    If edit->column_highlight is on and the disk is so full that the editor
    is unable even to write the magic (r <= 0), then subsequent if (len)
    check might fail, since the value of len is undefined.
    
    The solution is to initialize len with a non-zero value, so that the
    function properly returns an error value in all cases (adding an
    explicit return 0; is also possible, but then one must take care of
    closing file descriptors, which is less convenient).
    
    Signed-off-by: Yury V. Zaytsev <yury@shurup.com>
    ---
     src/editor/editcmd.c |    7 +++++--
     1 file changed, 5 insertions(+), 2 deletions(-)
    
    diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c
    index 4ca6cee..ec20cbf 100644
    a b edit_ok_to_exit (WEdit * edit) 
    29082908int 
    29092909edit_save_block (WEdit * edit, const char *filename, long start, long finish) 
    29102910{ 
    2911     int len, file; 
     2911    int len = 1, file; 
    29122912    vfs_path_t *vpath; 
    29132913 
    29142914    vpath = vfs_path_from_str (filename); 
    29152915    file = mc_open (vpath, O_CREAT | O_WRONLY | O_TRUNC, 
    29162916                    S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH | O_BINARY); 
    29172917    vfs_path_free (vpath); 
     2918 
    29182919    if (file == -1) 
    29192920        return 0; 
    29202921 
    29212922    if (edit->column_highlight) 
    29222923    { 
    29232924        int r; 
    2924  
    29252925        r = mc_write (file, VERTICAL_MAGIC, sizeof (VERTICAL_MAGIC)); 
     2926 
    29262927        if (r > 0) 
    29272928        { 
    29282929            unsigned char *block, *p; 
    edit_save_block (WEdit * edit, const char *filename, long start, long finish) 
    29572958        g_free (buf); 
    29582959    } 
    29592960    mc_close (file); 
     2961 
    29602962    if (len) 
    29612963        return 0; 
     2964 
    29622965    return 1; 
    29632966} 
    29642967