From d2d3581e744fe47a8c317a6383007e96f75427bd Mon Sep 17 00:00:00 2001 From: Renaud Fivet Date: Wed, 7 Jan 2015 14:47:13 +0800 Subject: [PATCH] Redimension result char array when doing &lef, &mid, &rig. --- eval.c | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/eval.c b/eval.c index 9f1b05e..23a784f 100644 --- a/eval.c +++ b/eval.c @@ -384,21 +384,38 @@ char *gtfun(char *fname) int sz ; sz = atoi( arg2) ; - if( sz >= ressize) - sz = ressize - 1 ; + if( sz >= ressize) { + free( result) ; + result = malloc( sz + 1) ; + ressize = sz + 1 ; + } + strncpy( result, arg1, sz) ; result[ sz] = 0 ; return result ; } - case UFRIGHT: - return (strcpy(result, - &arg1[(strlen(arg1) - atoi(arg2))])); + case UFRIGHT: { + int sz ; + + sz = atoi( arg2) ; + if( sz >= ressize) { + free( result) ; + result = malloc( sz + 1) ; + ressize = sz + 1 ; + } + + return strcpy( result, &arg1[ strlen( arg1) - sz]) ; + } case UFMID: { int sz ; sz = atoi( arg3) ; - if( sz >= ressize) - sz = ressize - 1 ; + if( sz >= ressize) { + free( result) ; + result = malloc( sz + 1) ; + ressize = sz + 1 ; + } + strncpy( result, &arg1[ atoi( arg2) - 1], sz) ; result[ sz] = 0 ; return result ;