comparison src/mbyte.c @ 34074:1629cc65d78d v9.1.0006

patch 9.1.0006: is*() and to*() function may be unsafe Commit: https://github.com/vim/vim/commit/184f71cc6868a240dc872ed2852542bbc1d43e28 Author: Keith Thompson <Keith.S.Thompson@gmail.com> Date: Thu Jan 4 21:19:04 2024 +0100 patch 9.1.0006: is*() and to*() function may be unsafe Problem: is*() and to*() function may be unsafe Solution: Add SAFE_* macros and start using those instead (Keith Thompson) Use SAFE_() macros for is*() and to*() functions The standard is*() and to*() functions declared in <ctype.h> have undefined behavior for negative arguments other than EOF. If plain char is signed, passing an unchecked value from argv for from user input to one of these functions has undefined behavior. Solution: Add SAFE_*() macros that cast the argument to unsigned char. Most implementations behave sanely for negative arguments, and most character values in practice are non-negative, but it's still best to avoid undefined behavior. The change from #13347 has been omitted, as this has already been separately fixed in commit ac709e2fc0db6d31abb7da96f743c40956b60c3a (v9.0.2054) fixes: #13332 closes: #13347 Signed-off-by: Keith Thompson <Keith.S.Thompson@gmail.com> Signed-off-by: Christian Brabandt <cb@256bit.org>
author Christian Brabandt <cb@256bit.org>
date Thu, 04 Jan 2024 21:30:04 +0100
parents 6d7a054bf2e3
children d7cfd8fb1d75
comparison
equal deleted inserted replaced
34073:7d9c9731e78e 34074:1629cc65d78d
4626 STRMOVE(p + 4, p + 3); 4626 STRMOVE(p + 4, p + 3);
4627 p[3] = '-'; 4627 p[3] = '-';
4628 } 4628 }
4629 4629
4630 // "iso-8859n" -> "iso-8859-n" 4630 // "iso-8859n" -> "iso-8859-n"
4631 if (STRNCMP(p, "iso-8859", 8) == 0 && isdigit(p[8])) 4631 if (STRNCMP(p, "iso-8859", 8) == 0 && SAFE_isdigit(p[8]))
4632 { 4632 {
4633 STRMOVE(p + 9, p + 8); 4633 STRMOVE(p + 9, p + 8);
4634 p[8] = '-'; 4634 p[8] = '-';
4635 } 4635 }
4636 4636
4703 // Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn", 4703 // Exception: "ja_JP.EUC" == "euc-jp", "zh_CN.EUC" = "euc-cn",
4704 // "ko_KR.EUC" == "euc-kr" 4704 // "ko_KR.EUC" == "euc-kr"
4705 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL) 4705 if ((p = (char *)vim_strchr((char_u *)s, '.')) != NULL)
4706 { 4706 {
4707 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0 4707 if (p > s + 2 && STRNICMP(p + 1, "EUC", 3) == 0
4708 && !isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_') 4708 && !SAFE_isalnum((int)p[4]) && p[4] != '-' && p[-3] == '_')
4709 { 4709 {
4710 // copy "XY.EUC" to "euc-XY" to buf[10] 4710 // copy "XY.EUC" to "euc-XY" to buf[10]
4711 STRCPY(buf + 10, "euc-"); 4711 STRCPY(buf + 10, "euc-");
4712 buf[14] = p[-2]; 4712 buf[14] = p[-2];
4713 buf[15] = p[-1]; 4713 buf[15] = p[-1];
4719 } 4719 }
4720 for (i = 0; i < (int)sizeof(buf) - 1 && s[i] != NUL; ++i) 4720 for (i = 0; i < (int)sizeof(buf) - 1 && s[i] != NUL; ++i)
4721 { 4721 {
4722 if (s[i] == '_' || s[i] == '-') 4722 if (s[i] == '_' || s[i] == '-')
4723 buf[i] = '-'; 4723 buf[i] = '-';
4724 else if (isalnum((int)s[i])) 4724 else if (SAFE_isalnum(s[i]))
4725 buf[i] = TOLOWER_ASC(s[i]); 4725 buf[i] = TOLOWER_ASC(s[i]);
4726 else 4726 else
4727 break; 4727 break;
4728 } 4728 }
4729 buf[i] = NUL; 4729 buf[i] = NUL;