From b6a8dcd922efef74fd956c06ebdb769d249a475b Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Mon, 27 May 2024 20:13:18 +0800 Subject: [PATCH] fixes #22852; fixes #23435; fixes #23645; SIGSEGV when slicing string or seq[T] with index out of range (#23279) follow up https://github.com/nim-lang/Nim/pull/23013 fixes #22852 fixes #23435 fixes #23645 reports rangeDefect correctly ```nim /workspaces/Nim/test9.nim(1) test9 /workspaces/Nim/lib/system/indices.nim(116) [] /workspaces/Nim/lib/system/fatal.nim(53) sysFatal Error: unhandled exception: value out of range: -2 notin 0 .. 9223372036854775807 [RangeDefect] ``` (cherry picked from commit c615828ccb2b309ac03aab93d94b40af6b4079ff) --- compiler/cgen.nim | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/compiler/cgen.nim b/compiler/cgen.nim index 14b0c1a341..d1e167c35f 100644 --- a/compiler/cgen.nim +++ b/compiler/cgen.nim @@ -1063,7 +1063,11 @@ proc allPathsAsgnResult(p: BProc; n: PNode): InitResultEnum = if result != Unknown: return result of nkAsgn, nkFastAsgn, nkSinkAsgn: if n[0].kind == nkSym and n[0].sym.kind == skResult: - if not containsResult(n[1]): result = InitSkippable + if not containsResult(n[1]): + if allPathsAsgnResult(p, n[1]) == InitRequired: + result = InitRequired + else: + result = InitSkippable else: result = InitRequired elif containsResult(n): result = InitRequired @@ -1141,6 +1145,10 @@ proc allPathsAsgnResult(p: BProc; n: PNode): InitResultEnum = allPathsInBranch(n[i]) of nkRaiseStmt: result = InitRequired + of nkChckRangeF, nkChckRange64, nkChckRange: + # TODO: more checks might need to be covered like overflow, indexDefect etc. + # bug #22852 + result = InitRequired else: for i in 0..