From: divverent Date: Thu, 5 Mar 2015 12:15:43 +0000 (+0000) Subject: Rephrase VM_altstr_prepare to no longer annoy Coverity. X-Git-Tag: xonotic-v0.8.1~8^2~2 X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=cad099a6ef73f318b6c5655072e46141e3524636;p=xonotic%2Fdarkplaces.git Rephrase VM_altstr_prepare to no longer annoy Coverity. (I think the previous code had no bug, but was hard to follow) git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@12201 d7cf8633-e32d-0410-b094-e92efae38249 --- diff --git a/prvm_cmds.c b/prvm_cmds.c index 9776b9ac..332fe75b 100644 --- a/prvm_cmds.c +++ b/prvm_cmds.c @@ -4425,23 +4425,26 @@ string altstr_prepare(string) */ void VM_altstr_prepare(prvm_prog_t *prog) { - char *out; const char *instr, *in; int size; char outstr[VM_STRINGTEMP_LENGTH]; + int outpos; VM_SAFEPARMCOUNT( 1, VM_altstr_prepare ); instr = PRVM_G_STRING( OFS_PARM0 ); - for( out = outstr, in = instr, size = sizeof(outstr) - 1 ; size && *in ; size--, in++, out++ ) - if( *in == '\'' && size > 1) { - *out++ = '\\'; - *out = '\''; - size--; - } else - *out = *in; - *out = 0; + for (in = instr, outpos = 0; *in && outpos < sizeof(outstr) - 1; ++in) + { + if (*in == '\'' && outpos < sizeof(outstr) - 2) + { + out[outpos++] = '\\'; + out[outpos++] = '\''; + } + else + out[outpos++] = *in; + } + out[outpos++] = 0; PRVM_G_INT( OFS_RETURN ) = PRVM_SetTempString(prog, outstr ); }