Ticket #4446: 0001-Fix-off-by-one-error-in-paragraph-formatting-code.patch

File 0001-Fix-off-by-one-error-in-paragraph-formatting-code.patch, 1.4 KB (added by proski, 14 months ago)

Patch

  • src/editor/format.c

    From b600ada6bcdf2cf2aba704d1f49209c39497f866 Mon Sep 17 00:00:00 2001
    From: Pavel Roskin <plroskin@gmail.com>
    Date: Fri, 17 Feb 2023 16:34:19 -0800
    Subject: [PATCH] Fix off-by-one error in paragraph formatting code
    
    The default margin is 72 characters, but the editor would keep 73 character
    long lines without breaking them.
    
    The visual length of strings was calculated incorrectly. The way "for" loop
    was implemented, the byte length would be incremented before exiting the
    loop. That would correspond to a character that doesn't fit the line.
    
    Increment the byte length in a separate statement.
    ---
     src/editor/format.c | 5 +++--
     1 file changed, 3 insertions(+), 2 deletions(-)
    
    diff --git a/src/editor/format.c b/src/editor/format.c
    index abf79f49b..0084b1ee2 100644
    a b static inline off_t 
    219219line_pixel_length (unsigned char *t, off_t b, off_t l, gboolean utf8) 
    220220{ 
    221221    off_t xn, x;                /* position counters */ 
    222     off_t char_length;          /* character length in bytes */ 
     222    off_t char_length = 0;      /* character length in bytes */ 
    223223 
    224224#ifndef HAVE_CHARSET 
    225225    (void) utf8; 
    226226#endif 
    227227 
    228     for (xn = 0, x = 0; xn <= l; x = xn, b += char_length) 
     228    for (xn = 0, x = 0; xn <= l; x = xn) 
    229229    { 
    230230        char *tb; 
    231231 
     232        b += char_length; 
    232233        tb = (char *) t + b; 
    233234        char_length = 1; 
    234235