Mercurial > vim
comparison src/charset.c @ 16706:77bcb5055fec v8.1.1355
patch 8.1.1355: obvious mistakes are accepted as valid expressions
commit https://github.com/vim/vim/commit/16e9b85113e0b354ece1cb4f5fcc7866850f3685
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun May 19 19:59:35 2019 +0200
patch 8.1.1355: obvious mistakes are accepted as valid expressions
Problem: Obvious mistakes are accepted as valid expressions.
Solution: Be more strict about parsing numbers. (Yasuhiro Matsumoto,
closes #3981)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sun, 19 May 2019 20:00:09 +0200 |
parents | 78faa25f9698 |
children | ef00b6bc186b |
comparison
equal
deleted
inserted
replaced
16705:033ac0bfd3ba | 16706:77bcb5055fec |
---|---|
1774 * If "what" contains STR2NR_BIN recognize binary numbers | 1774 * If "what" contains STR2NR_BIN recognize binary numbers |
1775 * If "what" contains STR2NR_OCT recognize octal numbers | 1775 * If "what" contains STR2NR_OCT recognize octal numbers |
1776 * If "what" contains STR2NR_HEX recognize hex numbers | 1776 * If "what" contains STR2NR_HEX recognize hex numbers |
1777 * If "what" contains STR2NR_FORCE always assume bin/oct/hex. | 1777 * If "what" contains STR2NR_FORCE always assume bin/oct/hex. |
1778 * If maxlen > 0, check at a maximum maxlen chars. | 1778 * If maxlen > 0, check at a maximum maxlen chars. |
1779 * If strict is TRUE, check the number strictly. return *len = 0 if fail. | |
1779 */ | 1780 */ |
1780 void | 1781 void |
1781 vim_str2nr( | 1782 vim_str2nr( |
1782 char_u *start, | 1783 char_u *start, |
1783 int *prep, /* return: type of number 0 = decimal, 'x' | 1784 int *prep, // return: type of number 0 = decimal, 'x' |
1784 or 'X' is hex, '0' = octal, 'b' or 'B' | 1785 // or 'X' is hex, '0' = octal, 'b' or 'B' |
1785 is bin */ | 1786 // is bin |
1786 int *len, /* return: detected length of number */ | 1787 int *len, // return: detected length of number |
1787 int what, /* what numbers to recognize */ | 1788 int what, // what numbers to recognize |
1788 varnumber_T *nptr, /* return: signed result */ | 1789 varnumber_T *nptr, // return: signed result |
1789 uvarnumber_T *unptr, /* return: unsigned result */ | 1790 uvarnumber_T *unptr, // return: unsigned result |
1790 int maxlen) /* max length of string to check */ | 1791 int maxlen, // max length of string to check |
1792 int strict) // check strictly | |
1791 { | 1793 { |
1792 char_u *ptr = start; | 1794 char_u *ptr = start; |
1793 int pre = 0; /* default is decimal */ | 1795 int pre = 0; // default is decimal |
1794 int negative = FALSE; | 1796 int negative = FALSE; |
1795 uvarnumber_T un = 0; | 1797 uvarnumber_T un = 0; |
1796 int n; | 1798 int n; |
1799 | |
1800 if (len != NULL) | |
1801 *len = 0; | |
1797 | 1802 |
1798 if (ptr[0] == '-') | 1803 if (ptr[0] == '-') |
1799 { | 1804 { |
1800 negative = TRUE; | 1805 negative = TRUE; |
1801 ++ptr; | 1806 ++ptr; |
1834 } | 1839 } |
1835 } | 1840 } |
1836 } | 1841 } |
1837 } | 1842 } |
1838 | 1843 |
1839 /* | 1844 // Do the conversion manually to avoid sscanf() quirks. |
1840 * Do the string-to-numeric conversion "manually" to avoid sscanf quirks. | |
1841 */ | |
1842 n = 1; | 1845 n = 1; |
1843 if (pre == 'B' || pre == 'b' || what == STR2NR_BIN + STR2NR_FORCE) | 1846 if (pre == 'B' || pre == 'b' || what == STR2NR_BIN + STR2NR_FORCE) |
1844 { | 1847 { |
1845 /* bin */ | 1848 /* bin */ |
1846 if (pre != 0) | 1849 if (pre != 0) |
1905 ++ptr; | 1908 ++ptr; |
1906 if (n++ == maxlen) | 1909 if (n++ == maxlen) |
1907 break; | 1910 break; |
1908 } | 1911 } |
1909 } | 1912 } |
1913 // Check for an alpha-numeric character immediately following, that is | |
1914 // most likely a typo. | |
1915 if (strict && n - 1 != maxlen && ASCII_ISALNUM(*ptr)) | |
1916 return; | |
1910 | 1917 |
1911 if (prep != NULL) | 1918 if (prep != NULL) |
1912 *prep = pre; | 1919 *prep = pre; |
1913 if (len != NULL) | 1920 if (len != NULL) |
1914 *len = (int)(ptr - start); | 1921 *len = (int)(ptr - start); |