From f0021db2ff66a1f5d14ca805daa567bf8f48bb92 Mon Sep 17 00:00:00 2001 From: Douglas Clowes Date: Fri, 24 Jul 2015 13:50:26 +1000 Subject: [PATCH] Fix some weird code in strrepc of task.c So little code, so many faults: * It calls strchr from the beginning each time (it could use ++ptr as the start) * If cFrom and cTo are the same it loops forever * if int is 32bits && ptr is 64bits it may break (many are, and the cast truncates the pointer) * if sizeof(char) > 1 it breaks badly --- trace.c | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/trace.c b/trace.c index 74a05b44..44d3bde5 100644 --- a/trace.c +++ b/trace.c @@ -191,15 +191,11 @@ static int strrepc(char *pszStr, char cFrom, char cTo) /*----------------------------------------------------------------*/ - while( 0 != ( ptr = strchr( pszStr, cFrom ) ) ) - - { /* WHILE cFrom occurs in pszStr */ - - pszStr[ (int) ptr - (int) pszStr ] = cTo ; - - /*- Replace next cFrom with cTo */ - - iReturn++ ; /*- count */ + for (ptr = pszStr; ptr && *ptr; ++ptr) { + if (*ptr == cFrom) { + *ptr = cTo; + ++iReturn; + } } return( iReturn ) ;