diff options
author | Haru <haru@dotalux.com> | 2015-12-20 20:41:19 +0100 |
---|---|---|
committer | Haru <haru@dotalux.com> | 2015-12-20 20:41:57 +0100 |
commit | 851e97a20701bf2800491690838f654a9a6638e6 (patch) | |
tree | 25d2c1092592e78809a429651e829873e131b495 /src/map | |
parent | 6a8fdfaae0bcfd230109019484e568edf15570eb (diff) | |
download | hercules-851e97a20701bf2800491690838f654a9a6638e6.tar.gz hercules-851e97a20701bf2800491690838f654a9a6638e6.tar.bz2 hercules-851e97a20701bf2800491690838f654a9a6638e6.tar.xz hercules-851e97a20701bf2800491690838f654a9a6638e6.zip |
Fixed a memory leak in the explode script command
Minor refactoring
Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/map')
-rw-r--r-- | src/map/script.c | 34 |
1 files changed, 16 insertions, 18 deletions
diff --git a/src/map/script.c b/src/map/script.c index 4010a7398..f438de3ab 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -14918,17 +14918,14 @@ BUILDIN(explode) const char delimiter = script_getstr(st, 4)[0]; int32 id; size_t len = strlen(str); - int i = 0, j = 0; + int i = 0, j = 0, k = 0; int start; - char *temp; - const char* name; + char *temp = NULL; + const char *name; TBL_PC* sd = NULL; - temp = (char*)aMalloc(len + 1); - - if( !data_isreference(data) ) - { + if (!data_isreference(data)) { ShowError("script:explode: not a variable\n"); script->reportdata(data); st->state = END; @@ -14939,34 +14936,35 @@ BUILDIN(explode) start = reference_getindex(data); name = reference_getname(data); - if( !is_string_variable(name) ) - { + if (!is_string_variable(name)) { ShowError("script:explode: not string array\n"); script->reportdata(data); st->state = END; return false;// data type mismatch } - if( not_server_variable(*name) ) - { + if (not_server_variable(*name)) { sd = script->rid2sd(st); - if( sd == NULL ) + if (sd == NULL) return true;// no player attached } - while(str[i] != '\0') { - if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index + temp = aMalloc(len + 1); + + for (i = 0; str[i] != '\0'; i++) { + if (str[i] == delimiter && (int64)start + k < (int64)(SCRIPT_MAX_ARRAYSIZE-1)) { // FIXME[Haru]: SCRIPT_MAX_ARRAYSIZE should really be unsigned (and INT32_MAX) + //break at delimiter but ignore after reaching last array index temp[j] = '\0'; - script->set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, start + k), name, (void*)temp, reference_getref(data)); + k++; j = 0; - ++i; } else { - temp[j++] = str[i++]; + temp[j++] = str[i]; } } //set last string temp[j] = '\0'; - script->set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data)); + script->set_reg(st, sd, reference_uid(id, start + k), name, (void*)temp, reference_getref(data)); aFree(temp); return true; |