diff options
| author | Justin M. Keyes <justinkz@gmail.com> | 2018-04-27 09:25:02 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-04-27 09:25:02 +0200 |
| commit | 53f11dcfc7139fe6c8a6b114db4bfec5d91005a9 (patch) | |
| tree | e9bfbaf10fc6681bbbcba0a0eabdce8a6f6840b5 /src/nvim/ex_cmds.c | |
| parent | 009ccfe170ada2c78ca7feabda567a7e901fb30b (diff) | |
| parent | 4ce8521ee4a72e050bd187c2986708c5f98c7442 (diff) | |
| download | rneovim-53f11dcfc7139fe6c8a6b114db4bfec5d91005a9.tar.gz rneovim-53f11dcfc7139fe6c8a6b114db4bfec5d91005a9.tar.bz2 rneovim-53f11dcfc7139fe6c8a6b114db4bfec5d91005a9.zip | |
Merge #8218 'Fix errors reported by PVS'
closes #4983
Diffstat (limited to 'src/nvim/ex_cmds.c')
| -rw-r--r-- | src/nvim/ex_cmds.c | 142 |
1 files changed, 73 insertions, 69 deletions
diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index c396b5891a..f575d58f05 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -109,82 +109,86 @@ typedef struct { # include "ex_cmds.c.generated.h" #endif -/* - * ":ascii" and "ga". - */ -void do_ascii(exarg_T *eap) +/// ":ascii" and "ga" implementation +void do_ascii(const exarg_T *const eap) { - int c; - int cval; - char buf1[20]; - char buf2[20]; - char_u buf3[7]; int cc[MAX_MCO]; - int ci = 0; - int len; - const bool l_enc_utf8 = enc_utf8; - - if (l_enc_utf8) - c = utfc_ptr2char(get_cursor_pos_ptr(), cc); - else - c = gchar_cursor(); + int c = utfc_ptr2char(get_cursor_pos_ptr(), cc); if (c == NUL) { MSG("NUL"); return; } - IObuff[0] = NUL; - if (!has_mbyte || (enc_dbcs != 0 && c < 0x100) || c < 0x80) { - if (c == NL) /* NUL is stored as NL */ + size_t iobuff_len = 0; + + int ci = 0; + if (c < 0x80) { + if (c == NL) { // NUL is stored as NL. c = NUL; - if (c == CAR && get_fileformat(curbuf) == EOL_MAC) - cval = NL; /* NL is stored as CR */ - else - cval = c; - if (vim_isprintc_strict(c) && (c < ' ' - || c > '~' - )) { + } + const int cval = (c == CAR && get_fileformat(curbuf) == EOL_MAC + ? NL // NL is stored as CR. + : c); + char buf1[20]; + if (vim_isprintc_strict(c) && (c < ' ' || c > '~')) { + char_u buf3[7]; transchar_nonprint(buf3, c); vim_snprintf(buf1, sizeof(buf1), " <%s>", (char *)buf3); - } else + } else { buf1[0] = NUL; - if (c >= 0x80) - vim_snprintf(buf2, sizeof(buf2), " <M-%s>", - (char *)transchar(c & 0x7f)); - else - buf2[0] = NUL; - vim_snprintf((char *)IObuff, IOSIZE, - _("<%s>%s%s %d, Hex %02x, Octal %03o"), - transchar(c), buf1, buf2, cval, cval, cval); - if (l_enc_utf8) - c = cc[ci++]; - else - c = 0; - } - - /* Repeat for combining characters. */ - while (has_mbyte && (c >= 0x100 || (l_enc_utf8 && c >= 0x80))) { - len = (int)STRLEN(IObuff); - /* This assumes every multi-byte char is printable... */ - if (len > 0) - IObuff[len++] = ' '; - IObuff[len++] = '<'; - if (l_enc_utf8 && utf_iscomposing(c) -# ifdef USE_GUI - && !gui.in_use -# endif - ) - IObuff[len++] = ' '; /* draw composing char on top of a space */ - len += (*mb_char2bytes)(c, IObuff + len); - vim_snprintf((char *)IObuff + len, IOSIZE - len, - c < 0x10000 ? _("> %d, Hex %04x, Octal %o") - : _("> %d, Hex %08x, Octal %o"), c, c, c); - if (ci == MAX_MCO) + } + char buf2[20]; + buf2[0] = NUL; + iobuff_len += ( + vim_snprintf((char *)IObuff + iobuff_len, sizeof(IObuff) - iobuff_len, + _("<%s>%s%s %d, Hex %02x, Octal %03o"), + transchar(c), buf1, buf2, cval, cval, cval)); + c = cc[ci++]; + } + +#define SPACE_FOR_DESC (1 + 1 + 1 + MB_MAXBYTES + 16 + 4 + 3 + 3 + 1) + // Space for description: + // - 1 byte for separator (starting from second entry) + // - 1 byte for "<" + // - 1 byte for space to draw composing character on (optional, but really + // mostly required) + // - up to MB_MAXBYTES bytes for character itself + // - 16 bytes for raw text ("> , Hex , Octal "). + // - at least 4 bytes for hexadecimal representation + // - at least 3 bytes for decimal representation + // - at least 3 bytes for octal representation + // - 1 byte for NUL + // + // Taking into account MAX_MCO and characters which need 8 bytes for + // hexadecimal representation, but not taking translation into account: + // resulting string will occupy less then 400 bytes (conservative estimate). + // + // Less then 1000 bytes if translation multiplies number of bytes needed for + // raw text by 6, so it should always fit into 1025 bytes reserved for IObuff. + + // Repeat for combining characters, also handle multiby here. + while (c >= 0x80 && iobuff_len < sizeof(IObuff) - SPACE_FOR_DESC) { + // This assumes every multi-byte char is printable... + if (iobuff_len > 0) { + IObuff[iobuff_len++] = ' '; + } + IObuff[iobuff_len++] = '<'; + if (utf_iscomposing(c)) { + IObuff[iobuff_len++] = ' '; // Draw composing char on top of a space. + } + iobuff_len += utf_char2bytes(c, IObuff + iobuff_len); + iobuff_len += ( + vim_snprintf((char *)IObuff + iobuff_len, sizeof(IObuff) - iobuff_len, + (c < 0x10000 + ? _("> %d, Hex %04x, Octal %o") + : _("> %d, Hex %08x, Octal %o")), c, c, c)); + if (ci == MAX_MCO) { break; - if (l_enc_utf8) - c = cc[ci++]; - else - c = 0; + } + c = cc[ci++]; + } + if (ci != MAX_MCO && c != 0) { + xstrlcpy((char *)IObuff + iobuff_len, " ...", sizeof(IObuff) - iobuff_len); } msg(IObuff); @@ -2018,13 +2022,13 @@ int getfile(int fnum, char_u *ffname, char_u *sfname, int setpm, linenr_T lnum, } if (other && !forceit && curbuf->b_nwindows == 1 && !buf_hide(curbuf) && curbufIsChanged() && autowrite(curbuf, forceit) == FAIL) { - if (p_confirm && p_write) - dialog_changed(curbuf, FALSE); + if (p_confirm && p_write) { + dialog_changed(curbuf, false); + } if (curbufIsChanged()) { - if (other) - --no_wait_return; + no_wait_return--; EMSG(_(e_nowrtmsg)); - retval = 2; /* file has been changed */ + retval = 2; // File has been changed. goto theend; } } |
