From 9d68727a84bf31d858287f25ef839b331288dfe7 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 2 Apr 2007 18:28:38 +0000 Subject: [PATCH] Rewrite substr() to properly clamp its argument ranges and handle negative start arguments as offset-from-end values --- simgear/nasal/lib.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/simgear/nasal/lib.c b/simgear/nasal/lib.c index fd8451be..0d4253f9 100644 --- a/simgear/nasal/lib.c +++ b/simgear/nasal/lib.c @@ -128,14 +128,21 @@ static naRef f_cmp(naContext c, naRef me, int argc, naRef* args) static naRef f_substr(naContext c, naRef me, int argc, naRef* args) { - naRef src = argc > 1 ? args[0] : naNil(); - naRef startR = argc > 1 ? naNumValue(args[1]) : naNil(); - naRef lenR = argc > 2 ? naNumValue(args[2]) : naNil(); - int start, len; - if(!naIsString(src) || naIsNil(startR)) ARGERR(); - start = (int)startR.num; - len = naIsNil(lenR) ? (naStr_len(src) - start) : (int)lenR.num; - if(len < 0) ARGERR(); + int start, len, srclen; + naRef src = argc > 0 ? args[0] : naNil(); + naRef startr = argc > 1 ? naNumValue(args[1]) : naNil(); + naRef lenr = argc > 2 ? naNumValue(args[2]) : naNil(); + if(!naIsString(src)) ARGERR(); + if(naIsNil(startr) || !naIsNum(startr)) ARGERR(); + if(!naIsNil(lenr) && !naIsNum(lenr)) ARGERR(); + srclen = naStr_len(src); + start = (int)startr.num; + len = naIsNum(lenr) ? (int)lenr.num : (srclen - start); + if(start < 0) start += srclen; + if(start < 0) start = len = 0; + if(start >= srclen) start = len = 0; + if(len < 0) len = 0; + if(len > srclen - start) len = srclen - start; return naStr_substr(naNewString(c), src, start, len); } -- 2.39.5