diff options
Diffstat (limited to 'src/common')
39 files changed, 497 insertions, 196 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c index dbe121940..c84b447e8 100644 --- a/src/common/HPM.c +++ b/src/common/HPM.c @@ -103,6 +103,7 @@ void hplugin_export_symbol(void *value, const char *name) void *hplugin_import_symbol(char *name, unsigned int pID) { int i; + nullpo_retr(NULL, name); ARR_FIND(0, VECTOR_LENGTH(HPM->symbols), i, strcmp(VECTOR_INDEX(HPM->symbols, i)->name, name) == 0); if (i != VECTOR_LENGTH(HPM->symbols)) @@ -133,6 +134,7 @@ bool hplugin_iscompatible(char* version) { bool hplugin_exists(const char *filename) { int i; + nullpo_retr(false, filename); for (i = 0; i < VECTOR_LENGTH(HPM->plugins); i++) { if (strcmpi(VECTOR_INDEX(HPM->plugins, i)->filename,filename) == 0) return true; @@ -259,6 +261,7 @@ void hplugins_addToHPData(enum HPluginDataTypes type, uint32 pluginID, struct hp return; } store = *storeptr; + nullpo_retv(store); /* duplicate check */ ARR_FIND(0, VECTOR_LENGTH(store->entries), i, VECTOR_INDEX(store->entries, i)->pluginID == pluginID && VECTOR_INDEX(store->entries, i)->classid == classid); @@ -447,7 +450,8 @@ bool hplugins_addconf(unsigned int pluginID, enum HPluginConfType type, char *na return true; } -struct hplugin *hplugin_load(const char* filename) { +struct hplugin *hplugin_load(const char* filename) +{ struct hplugin *plugin; struct hplugin_info *info; struct HPMi_interface **HPMi; @@ -563,6 +567,7 @@ struct hplugin *hplugin_load(const char* filename) { /* id */ plugin->hpi->pid = plugin->idx; /* core */ + plugin->hpi->memmgr = HPMiMalloc; #ifdef CONSOLE_INPUT plugin->hpi->addCPCommand = console->input->addCommand; #endif // CONSOLE_INPUT @@ -596,6 +601,7 @@ struct hplugin *hplugin_load(const char* filename) { void hplugin_unload(struct hplugin* plugin) { int i; + nullpo_retv(plugin); if (plugin->filename) aFree(plugin->filename); if (plugin->dll) @@ -622,7 +628,8 @@ CMDLINEARG(loadplugin) /** * Reads the plugin configuration and loads the plugins as necessary. */ -void hplugins_config_read(void) { +void hplugins_config_read(void) +{ struct config_t plugins_conf; struct config_setting_t *plist = NULL; const char *config_filename = "conf/plugins.conf"; // FIXME hardcoded name @@ -788,6 +795,7 @@ const char *HPM_file2ptr(const char *file) { int i; + nullpo_retr(NULL, file); ARR_FIND(0, HPM->filenames.count, i, HPM->filenames.data[i].addr == file); if (i != HPM->filenames.count) { return HPM->filenames.data[i].name; @@ -801,19 +809,29 @@ const char *HPM_file2ptr(const char *file) return HPM->filenames.data[i].name; } -void* HPM_mmalloc(size_t size, const char *file, int line, const char *func) { + +void* HPM_mmalloc(size_t size, const char *file, int line, const char *func) +{ return iMalloc->malloc(size,HPM_file2ptr(file),line,func); } -void* HPM_calloc(size_t num, size_t size, const char *file, int line, const char *func) { + +void* HPM_calloc(size_t num, size_t size, const char *file, int line, const char *func) +{ return iMalloc->calloc(num,size,HPM_file2ptr(file),line,func); } -void* HPM_realloc(void *p, size_t size, const char *file, int line, const char *func) { + +void* HPM_realloc(void *p, size_t size, const char *file, int line, const char *func) +{ return iMalloc->realloc(p,size,HPM_file2ptr(file),line,func); } -void* HPM_reallocz(void *p, size_t size, const char *file, int line, const char *func) { + +void* HPM_reallocz(void *p, size_t size, const char *file, int line, const char *func) +{ return iMalloc->reallocz(p,size,HPM_file2ptr(file),line,func); } -char* HPM_astrdup(const char *p, const char *file, int line, const char *func) { + +char* HPM_astrdup(const char *p, const char *file, int line, const char *func) +{ return iMalloc->astrdup(p,HPM_file2ptr(file),line,func); } @@ -849,6 +867,7 @@ bool hplugins_get_battle_conf(const char *w1, int *value) { int i; + nullpo_retr(false, w1); nullpo_retr(false, value); ARR_FIND(0, VECTOR_LENGTH(HPM->config_listeners[HPCT_BATTLE]), i, strcmpi(w1, VECTOR_INDEX(HPM->config_listeners[HPCT_BATTLE], i).key) == 0); @@ -880,7 +899,7 @@ bool hplugins_parse_conf(const struct config_t *config, const char *filename, en for (i = 0; i < VECTOR_LENGTH(HPM->config_listeners[point]); i++) { const struct HPConfListenStorage *entry = &VECTOR_INDEX(HPM->config_listeners[point], i); const char *config_name = entry->key; - const char *str = buf; + const char *str = NULL; if ((setting = libconfig->lookup(config, config_name)) == NULL) { if (!imported && entry->required) { ShowWarning("Missing configuration '%s' in file %s!\n", config_name, filename); @@ -1012,9 +1031,11 @@ void hplugin_data_store_create(struct hplugin_data_store **storeptr, enum HPlugi /** * Called by HPM->DataCheck on a plugins incoming data, ensures data structs in use are matching! **/ -bool HPM_DataCheck(struct s_HPMDataCheck *src, unsigned int size, int version, char *name) { +bool HPM_DataCheck(struct s_HPMDataCheck *src, unsigned int size, int version, char *name) +{ unsigned int i, j; + nullpo_retr(false, src); if (version != datacheck_version) { ShowError("HPMDataCheck:%s: DataCheck API version mismatch %d != %d\n", name, datacheck_version, version); return false; @@ -1039,7 +1060,8 @@ bool HPM_DataCheck(struct s_HPMDataCheck *src, unsigned int size, int version, c return true; } -void HPM_datacheck_init(const struct s_HPMDataCheck *src, unsigned int length, int version) { +void HPM_datacheck_init(const struct s_HPMDataCheck *src, unsigned int length, int version) +{ unsigned int i; datacheck_version = version; @@ -1055,11 +1077,13 @@ void HPM_datacheck_init(const struct s_HPMDataCheck *src, unsigned int length, i } } -void HPM_datacheck_final(void) { +void HPM_datacheck_final(void) +{ db_destroy(datacheck_db); } -void hpm_init(void) { +void hpm_init(void) +{ int i; datacheck_db = NULL; datacheck_data = NULL; @@ -1070,8 +1094,8 @@ void hpm_init(void) { HPM->off = false; - memcpy(&iMalloc_HPM, iMalloc, sizeof(struct malloc_interface)); HPMiMalloc = &iMalloc_HPM; + *HPMiMalloc = *iMalloc; HPMiMalloc->malloc = HPM_mmalloc; HPMiMalloc->calloc = HPM_calloc; HPMiMalloc->realloc = HPM_realloc; @@ -1151,6 +1175,7 @@ void hpm_final(void) return; } + void hpm_defaults(void) { HPM = &HPM_s; diff --git a/src/common/HPMDataCheck.h b/src/common/HPMDataCheck.h index d0e23811c..0a4af75dd 100644 --- a/src/common/HPMDataCheck.h +++ b/src/common/HPMDataCheck.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2014-2016 Hercules Dev Team + * Copyright (C) 2014-2017 Hercules Dev Team * * Hercules is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by diff --git a/src/common/HPMSymbols.inc.h b/src/common/HPMSymbols.inc.h index 7ccb960de..d4a103b88 100644 --- a/src/common/HPMSymbols.inc.h +++ b/src/common/HPMSymbols.inc.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2013-2016 Hercules Dev Team + * Copyright (C) 2013-2017 Hercules Dev Team * * Hercules is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -155,9 +155,6 @@ struct loginif_interface *loginif; #ifdef MAP_MAIL_H /* mail */ struct mail_interface *mail; #endif // MAP_MAIL_H -#ifdef COMMON_MEMMGR_H /* iMalloc */ -struct malloc_interface *iMalloc; -#endif // COMMON_MEMMGR_H #ifdef MAP_MAP_H /* map */ struct map_interface *map; #endif // MAP_MAP_H @@ -405,9 +402,6 @@ if ((server_type&(SERVER_TYPE_CHAR)) && !HPM_SYMBOL("loginif", loginif)) return #ifdef MAP_MAIL_H /* mail */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("mail", mail)) return "mail"; #endif // MAP_MAIL_H -#ifdef COMMON_MEMMGR_H /* iMalloc */ -if ((server_type&(SERVER_TYPE_ALL)) && !HPM_SYMBOL("iMalloc", iMalloc)) return "iMalloc"; -#endif // COMMON_MEMMGR_H #ifdef MAP_MAP_H /* map */ if ((server_type&(SERVER_TYPE_MAP)) && !HPM_SYMBOL("map", map)) return "map"; #endif // MAP_MAP_H diff --git a/src/common/HPMi.h b/src/common/HPMi.h index 19b9b20a5..143c325c1 100644 --- a/src/common/HPMi.h +++ b/src/common/HPMi.h @@ -231,6 +231,7 @@ struct HPMi_interface { /* Hooking */ struct HPMHooking_interface *hooking; + struct malloc_interface *memmgr; }; #ifdef HERCULES_CORE #define HPM_SYMBOL(n, s) (HPM->share((s), (n)), true) diff --git a/src/common/atomic.h b/src/common/atomic.h index 82d579bf4..b370052a9 100644 --- a/src/common/atomic.h +++ b/src/common/atomic.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) rAthena Project (www.rathena.org) * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/cbasetypes.h b/src/common/cbasetypes.h index 98c3552c4..2c36c23bc 100644 --- a/src/common/cbasetypes.h +++ b/src/common/cbasetypes.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * * Hercules is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -109,6 +109,14 @@ # define __attribute__(x) #endif +/// Feature/extension checking macros +#ifndef __has_extension /* Available in clang and gcc >= 3 */ +#define __has_extension(x) 0 +#endif +#ifndef __has_feature /* Available in clang and gcc >= 5 */ +#define __has_feature(x) __has_extension(x) +#endif + ////////////////////////////////////////////////////////////////////////// // portable printf/scanf format macros and integer definitions // NOTE: Visual C++ uses <inttypes.h> and <stdint.h> provided in /3rdparty @@ -441,4 +449,23 @@ typedef char bool; /** Support macros for marking structs as unavailable */ #define UNAVAILABLE_STRUCT int8 HERC__unavailable_struct +/** Static assertion (only on compilers that support it) */ +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L +// C11 version +#define STATIC_ASSERT(ex, msg) _Static_assert(ex, msg) +#elif __has_feature(c_static_assert) +// Clang support (as per http://clang.llvm.org/docs/LanguageExtensions.html) +#define STATIC_ASSERT(ex, msg) _Static_assert(ex, msg) +#elif defined(__GNUC__) && GCC_VERSION >= 40700 +// GCC >= 4.7 is known to support it +#define STATIC_ASSERT(ex, msg) _Static_assert(ex, msg) +#elif defined(_MSC_VER) +// MSVC doesn't support it, but it accepts the C++ style version +#define STATIC_ASSERT(ex, msg) static_assert(ex, msg) +#else +// Otherise just ignore it until it's supported +#define STATIC_ASSERT(ex, msg) +#endif + + #endif /* COMMON_CBASETYPES_H */ diff --git a/src/common/conf.c b/src/common/conf.c index 9188affa4..96b9bff9f 100644 --- a/src/common/conf.c +++ b/src/common/conf.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -373,6 +373,80 @@ int config_lookup_mutable_string(const struct config_t *config, const char *name return CONFIG_FALSE; } +/** + * Wrapper for config_setting_get_int64() using defined-size variables + * + * @see config_setting_get_int64_real() + */ +int64 config_setting_get_int64_real(const struct config_setting_t *setting) +{ + return (int64)config_setting_get_int64(setting); +} + +/** + * Wrapper for config_setting_lookup_int64() using defined-size variables + * + * @see config_setting_lookup_int64() + */ +int config_setting_lookup_int64_real(const struct config_setting_t *setting, const char *name, int64 *value) +{ + long long int lli = 0; + + if (config_setting_lookup_int64(setting, name, &lli) != CONFIG_TRUE) + return CONFIG_FALSE; + + *value = (int64)lli; + + return CONFIG_TRUE; +} + +/** + * Wrapper for config_setting_set_int64() using defined-size variables + * + * @see config_setting_set_int64() + */ +int config_setting_set_int64_real(struct config_setting_t *setting, int64 value) +{ + return config_setting_set_int64(setting, (long long int)value); +} + +/** + * Wrapper for config_setting_get_int64_elem() using defined-size variables + * + * @see config_setting_get_int64_elem() + */ +int64 config_setting_get_int64_elem_real(const struct config_setting_t *setting, int idx) +{ + return (int64)config_setting_get_int64_elem(setting, idx); +} + +/** + * Wrapper for config_setting_set_int64_elem() using defined-size variables + * + * @see config_setting_set_int64_elem() + */ +struct config_setting_t *config_setting_set_int64_elem_real(struct config_setting_t *setting, int idx, int64 value) +{ + return config_setting_set_int64_elem(setting, idx, (long long int)value); +} + +/** + * Wrapper for config_lookup_int64() using defined-size variables + * + * @see config_lookup_int64() + */ +int config_lookup_int64_real(const struct config_t *config, const char *filepath, int64 *value) +{ + long long int lli = 0; + + if (config_lookup_int64(config, filepath, &lli) != CONFIG_TRUE) + return CONFIG_FALSE; + + *value = (int64)lli; + + return CONFIG_TRUE; +} + void libconfig_defaults(void) { libconfig = &libconfig_s; @@ -393,20 +467,20 @@ void libconfig_defaults(void) { libconfig->destroy = config_destroy; /* */ libconfig->setting_get_int = config_setting_get_int; - libconfig->setting_get_int64 = config_setting_get_int64; + libconfig->setting_get_int64 = config_setting_get_int64_real; libconfig->setting_get_float = config_setting_get_float; libconfig->setting_get_bool = config_setting_get_bool; libconfig->setting_get_string = config_setting_get_string; /* */ libconfig->setting_lookup = config_setting_lookup; libconfig->setting_lookup_int = config_setting_lookup_int; - libconfig->setting_lookup_int64 = config_setting_lookup_int64; + libconfig->setting_lookup_int64 = config_setting_lookup_int64_real; libconfig->setting_lookup_float = config_setting_lookup_float; libconfig->setting_lookup_bool = config_setting_lookup_bool; libconfig->setting_lookup_string = config_setting_lookup_string; /* */ libconfig->setting_set_int = config_setting_set_int; - libconfig->setting_set_int64 = config_setting_set_int64; + libconfig->setting_set_int64 = config_setting_set_int64_real; libconfig->setting_set_float = config_setting_set_float; libconfig->setting_set_bool = config_setting_set_bool; libconfig->setting_set_string = config_setting_set_string; @@ -415,13 +489,13 @@ void libconfig_defaults(void) { libconfig->setting_get_format = config_setting_get_format; /* */ libconfig->setting_get_int_elem = config_setting_get_int_elem; - libconfig->setting_get_int64_elem = config_setting_get_int64_elem; + libconfig->setting_get_int64_elem = config_setting_get_int64_elem_real; libconfig->setting_get_float_elem = config_setting_get_float_elem; libconfig->setting_get_bool_elem = config_setting_get_bool_elem; libconfig->setting_get_string_elem = config_setting_get_string_elem; /* */ libconfig->setting_set_int_elem = config_setting_set_int_elem; - libconfig->setting_set_int64_elem = config_setting_set_int64_elem; + libconfig->setting_set_int64_elem = config_setting_set_int64_elem_real; libconfig->setting_set_float_elem = config_setting_set_float_elem; libconfig->setting_set_bool_elem = config_setting_set_bool_elem; libconfig->setting_set_string_elem = config_setting_set_string_elem; @@ -441,7 +515,7 @@ void libconfig_defaults(void) { libconfig->lookup = config_lookup; /* */ libconfig->lookup_int = config_lookup_int; - libconfig->lookup_int64 = config_lookup_int64; + libconfig->lookup_int64 = config_lookup_int64_real; libconfig->lookup_float = config_lookup_float; libconfig->lookup_bool = config_lookup_bool; libconfig->lookup_string = config_lookup_string; diff --git a/src/common/conf.h b/src/common/conf.h index f2bfcac62..bd6acc4be 100644 --- a/src/common/conf.h +++ b/src/common/conf.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -46,7 +46,7 @@ struct libconfig_interface { void (*destroy) (struct config_t *config); int (*setting_get_int) (const struct config_setting_t *setting); - long long (*setting_get_int64) (const struct config_setting_t *setting); + int64 (*setting_get_int64) (const struct config_setting_t *setting); double (*setting_get_float) (const struct config_setting_t *setting); int (*setting_get_bool) (const struct config_setting_t *setting); @@ -55,12 +55,12 @@ struct libconfig_interface { struct config_setting_t * (*setting_lookup) (struct config_setting_t *setting, const char *name); int (*setting_lookup_int) (const struct config_setting_t *setting, const char *name, int *value); - int (*setting_lookup_int64) (const struct config_setting_t *setting, const char *name, long long *value); + int (*setting_lookup_int64) (const struct config_setting_t *setting, const char *name, int64 *value); int (*setting_lookup_float) (const struct config_setting_t *setting, const char *name, double *value); int (*setting_lookup_bool) (const struct config_setting_t *setting, const char *name, int *value); int (*setting_lookup_string) (const struct config_setting_t *setting, const char *name, const char **value); - int (*setting_set_int) (struct config_setting_t *setting ,int value); - int (*setting_set_int64) (struct config_setting_t *setting, long long value); + int (*setting_set_int) (struct config_setting_t *setting, int value); + int (*setting_set_int64) (struct config_setting_t *setting, int64 value); int (*setting_set_float) (struct config_setting_t *setting, double value); int (*setting_set_bool) (struct config_setting_t *setting, int value); int (*setting_set_string) (struct config_setting_t *setting, const char *value); @@ -69,12 +69,12 @@ struct libconfig_interface { short (*setting_get_format) (const struct config_setting_t *setting); int (*setting_get_int_elem) (const struct config_setting_t *setting, int idx); - long long (*setting_get_int64_elem) (const struct config_setting_t *setting, int idx); + int64 (*setting_get_int64_elem) (const struct config_setting_t *setting, int idx); double (*setting_get_float_elem) (const struct config_setting_t *setting, int idx); int (*setting_get_bool_elem) (const struct config_setting_t *setting, int idx); const char * (*setting_get_string_elem) (const struct config_setting_t *setting, int idx); struct config_setting_t * (*setting_set_int_elem) (struct config_setting_t *setting, int idx, int value); - struct config_setting_t * (*setting_set_int64_elem) (struct config_setting_t *setting, int idx, long long value); + struct config_setting_t * (*setting_set_int64_elem) (struct config_setting_t *setting, int idx, int64 value); struct config_setting_t * (*setting_set_float_elem) (struct config_setting_t *setting, int idx, double value); struct config_setting_t * (*setting_set_bool_elem) (struct config_setting_t *setting, int idx, int value); struct config_setting_t * (*setting_set_string_elem) (struct config_setting_t *setting, int idx, const char *value); @@ -93,7 +93,7 @@ struct libconfig_interface { struct config_setting_t * (*lookup) (const struct config_t *config, const char *filepath); int (*lookup_int) (const struct config_t *config, const char *filepath, int *value); - int (*lookup_int64) (const struct config_t *config, const char *filepath, long long *value); + int (*lookup_int64) (const struct config_t *config, const char *filepath, int64 *value); int (*lookup_float) (const struct config_t *config, const char *filepath, double *value); int (*lookup_bool) (const struct config_t *config, const char *filepath, int *value); int (*lookup_string) (const struct config_t *config, const char *filepath, const char **value); diff --git a/src/common/console.c b/src/common/console.c index f1b4523e2..0f79b9494 100644 --- a/src/common/console.c +++ b/src/common/console.c @@ -68,7 +68,8 @@ struct { /*====================================== * CORE : Display title *--------------------------------------*/ -void display_title(void) { +void display_title(void) +{ const char *vcstype = sysinfo->vcstype(); ShowMessage("\n"); @@ -99,7 +100,7 @@ void display_title(void) { */ void display_gplnotice(void) { - ShowInfo("Hercules, Copyright (C) 2012-2015, Hercules Dev Team and others.\n"); + ShowInfo("Hercules, Copyright (C) 2012-2016, Hercules Dev Team and others.\n"); ShowInfo("Licensed under the GNU General Public License, version 3 or later.\n"); } @@ -130,21 +131,24 @@ int console_parse_key_pressed(void) /** * Stops server **/ -CPCMD_C(exit,server) { +CPCMD_C(exit, server) +{ core->runflag = 0; } /** * Displays ERS-related statistics (Entry Reusage System) **/ -CPCMD_C(ers_report,server) { +CPCMD_C(ers_report, server) +{ ers_report(); } /** * Displays memory usage **/ -CPCMD_C(mem_report,server) { +CPCMD_C(mem_report, server) +{ #ifdef USE_MEMMGR memmgr_report(line?atoi(line):0); #endif @@ -171,7 +175,8 @@ CPCMD(help) * [Ind/Hercules] * Displays current malloc usage */ -CPCMD_C(malloc_usage,server) { +CPCMD_C(malloc_usage, server) +{ unsigned int val = (unsigned int)iMalloc->usage(); ShowInfo("malloc_usage: %.2f MB\n",(double)(val)/1024); } @@ -180,7 +185,8 @@ CPCMD_C(malloc_usage,server) { * Skips an sql update * Usage: sql update skip UPDATE-FILE.sql **/ -CPCMD_C(skip,update) { +CPCMD_C(skip, update) +{ if( !line ) { ShowDebug("usage example: sql update skip 2013-02-14--16-15.sql\n"); return; @@ -311,6 +317,7 @@ void console_parse_create(char *name, CParseFunc func) char sublist[CP_CMD_LENGTH * 5]; struct CParseEntry *cmd; + nullpo_retv(name); safestrncpy(sublist, name, CP_CMD_LENGTH * 5); tok = strtok(sublist,":"); @@ -364,6 +371,7 @@ void console_parse_list_subs(struct CParseEntry *cmd, unsigned char depth) { int i; char msg[CP_CMD_LENGTH * 2]; + nullpo_retv(cmd); Assert_retv(cmd->type == CPET_CATEGORY); for (i = 0; i < VECTOR_LENGTH(cmd->u.children); i++) { struct CParseEntry *child = VECTOR_INDEX(cmd->u.children, i); @@ -391,6 +399,7 @@ void console_parse_sub(char *line) char sublist[CP_CMD_LENGTH * 5]; int i; + nullpo_retv(line); memcpy(bline, line, 200); tok = strtok(line, " "); @@ -444,9 +453,12 @@ void console_parse_sub(char *line) } ShowError("Is only a category, type '"CL_WHITE"%s help"CL_RESET"' to list its subcommands\n",sublist); } -void console_parse(char* line) { + +void console_parse(char *line) +{ int c, i = 0, len = MAX_CONSOLE_INPUT - 1;/* we leave room for the \0 :P */ + nullpo_retv(line); while( (c = fgetc(stdin)) != EOF ) { if( --len == 0 ) break; @@ -458,7 +470,9 @@ void console_parse(char* line) { line[i++] = '\0'; } -void *cThread_main(void *x) { + +void *cThread_main(void *x) +{ while( console->input->ptstate ) {/* loopx */ if( console->input->key_pressed() ) { char input[MAX_CONSOLE_INPUT]; @@ -483,7 +497,9 @@ void *cThread_main(void *x) { return NULL; } -int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { + +int console_parse_timer(int tid, int64 tick, int id, intptr_t data) +{ int i; EnterSpinLock(console->input->ptlock); for(i = 0; i < cinput.count; i++) { @@ -494,7 +510,9 @@ int console_parse_timer(int tid, int64 tick, int id, intptr_t data) { mutex->cond_signal(console->input->ptcond); return 0; } -void console_parse_final(void) { + +void console_parse_final(void) +{ if( console->input->ptstate ) { InterlockedDecrement(&console->input->ptstate); mutex->cond_signal(console->input->ptcond); @@ -506,7 +524,9 @@ void console_parse_final(void) { mutex->destroy(console->input->ptmutex); } } -void console_parse_init(void) { + +void console_parse_init(void) +{ cinput.count = 0; console->input->ptstate = 1; @@ -524,6 +544,7 @@ void console_parse_init(void) { timer->add_func_list(console->input->parse_timer, "console_parse_timer"); timer->add_interval(timer->gettick() + 1000, console->input->parse_timer, 0, 0, 500);/* start listening in 1s; re-try every 0.5s */ } + void console_setSQL(struct Sql *SQL_handle) { console->input->SQL = SQL_handle; diff --git a/src/common/core.c b/src/common/core.c index 63123dfea..9a131d042 100644 --- a/src/common/core.c +++ b/src/common/core.c @@ -100,7 +100,8 @@ struct core_interface *core = &core_s; #ifndef POSIX #define compat_signal(signo, func) signal((signo), (func)) #else -sigfunc *compat_signal(int signo, sigfunc *func) { +sigfunc *compat_signal(int signo, sigfunc *func) +{ struct sigaction sact, oact; sact.sa_handler = func; @@ -121,7 +122,8 @@ sigfunc *compat_signal(int signo, sigfunc *func) { * CORE : Console events for Windows *--------------------------------------*/ #ifdef _WIN32 -static BOOL WINAPI console_handler(DWORD c_event) { +static BOOL WINAPI console_handler(DWORD c_event) +{ switch(c_event) { case CTRL_CLOSE_EVENT: case CTRL_LOGOFF_EVENT: @@ -137,7 +139,8 @@ static BOOL WINAPI console_handler(DWORD c_event) { return TRUE; } -static void cevents_init(void) { +static void cevents_init(void) +{ if (SetConsoleCtrlHandler(console_handler,TRUE)==FALSE) ShowWarning ("Unable to install the console handler!\n"); } @@ -146,7 +149,8 @@ static void cevents_init(void) { /*====================================== * CORE : Signal Sub Function *--------------------------------------*/ -static void sig_proc(int sn) { +static void sig_proc(int sn) +{ static int is_called = 0; switch (sn) { @@ -179,7 +183,8 @@ static void sig_proc(int sn) { } } -void signals_init (void) { +void signals_init (void) +{ compat_signal(SIGTERM, sig_proc); compat_signal(SIGINT, sig_proc); #ifndef _DEBUG // need unhandled exceptions to debug on Windows @@ -245,7 +250,8 @@ bool usercheck(void) return true; } -void core_defaults(void) { +void core_defaults(void) +{ nullpo_defaults(); #ifndef MINICORE hpm_defaults(); @@ -271,16 +277,20 @@ void core_defaults(void) { thread_defaults(); #endif } + /** * Returns the source (core or plugin name) for the given command-line argument */ -const char *cmdline_arg_source(struct CmdlineArgData *arg) { +const char *cmdline_arg_source(struct CmdlineArgData *arg) +{ #ifdef MINICORE return "core"; #else // !MINICORE + nullpo_retr(NULL, arg); return HPM->pid2name(arg->pluginID); #endif // MINICORE } + /** * Defines a command line argument. * @@ -292,9 +302,11 @@ const char *cmdline_arg_source(struct CmdlineArgData *arg) { * @param options options associated to the command-line argument. @see enum cmdline_options. * @return the success status. */ -bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, CmdlineExecFunc func, const char *help, unsigned int options) { +bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, CmdlineExecFunc func, const char *help, unsigned int options) +{ struct CmdlineArgData *data = NULL; + nullpo_retr(false, name); VECTOR_ENSURE(cmdline->args_data, 1, 1); VECTOR_PUSHZEROED(cmdline->args_data); data = &VECTOR_LAST(cmdline->args_data); @@ -310,6 +322,7 @@ bool cmdline_arg_add(unsigned int pluginID, const char *name, char shortname, Cm return true; } + /** * Help screen to be displayed by '--help'. */ @@ -333,6 +346,7 @@ static CMDLINEARG(help) } return false; } + /** * Info screen to be displayed by '--version'. */ @@ -343,6 +357,7 @@ static CMDLINEARG(version) ShowInfo("Open "CL_WHITE"readme.txt"CL_RESET" for more information.\n"); return false; } + /** * Checks if there is a value available for the current argument * @@ -360,6 +375,7 @@ bool cmdline_arg_next_value(const char *name, int current_arg, int argc) return true; } + /** * Executes the command line arguments handlers. * @@ -381,11 +397,15 @@ bool cmdline_arg_next_value(const char *name, int current_arg, int argc) int cmdline_exec(int argc, char **argv, unsigned int options) { int count = 0, i; + + nullpo_ret(argv); for (i = 1; i < argc; i++) { int j; struct CmdlineArgData *data = NULL; const char *arg = argv[i]; if (arg[0] != '-') { // All arguments must begin with '-' + if ((options&(CMDLINE_OPT_SILENT|CMDLINE_OPT_PREINIT)) != 0) + continue; ShowError("Invalid option '%s'.\n", argv[i]); exit(EXIT_FAILURE); } @@ -423,6 +443,7 @@ int cmdline_exec(int argc, char **argv, unsigned int options) } return count; } + /** * Defines the global command-line arguments. */ @@ -466,10 +487,12 @@ void cmdline_defaults(void) cmdline->arg_next_value = cmdline_arg_next_value; cmdline->arg_source = cmdline_arg_source; } + /*====================================== * CORE : MAINROUTINE *--------------------------------------*/ -int main (int argc, char **argv) { +int main (int argc, char **argv) +{ int retval = EXIT_SUCCESS; {// initialize program arguments char *p1 = SERVER_NAME = argv[0]; diff --git a/src/common/core.h b/src/common/core.h index 4aaa6cfac..a8726fcef 100644 --- a/src/common/core.h +++ b/src/common/core.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/db.c b/src/common/db.c index 0c7bc2ae0..91592fdac 100644 --- a/src/common/db.c +++ b/src/common/db.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -93,6 +93,7 @@ #include "common/ers.h" #include "common/memmgr.h" #include "common/mmo.h" +#include "common/nullpo.h" #include "common/showmsg.h" #include "common/strlib.h" @@ -2795,7 +2796,8 @@ void *db_data2ptr(struct DBData *data) * @public * @see #db_final(void) */ -void db_init(void) { +void db_init(void) +{ db_iterator_ers = ers_new(sizeof(struct DBIterator_impl),"db.c::db_iterator_ers",ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK); db_alloc_ers = ers_new(sizeof(struct DBMap_impl),"db.c::db_alloc_ers",ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK); ers_chunk_size(db_alloc_ers, 50); @@ -2907,7 +2909,7 @@ void db_final(void) } // Link DB System - jAthena -void linkdb_insert( struct linkdb_node** head, void *key, void* data) +void linkdb_insert(struct linkdb_node **head, void *key, void *data) { struct linkdb_node *node; if( head == NULL ) return ; @@ -2928,7 +2930,8 @@ void linkdb_insert( struct linkdb_node** head, void *key, void* data) node->data = data; } -void linkdb_vforeach( struct linkdb_node** head, LinkDBFunc func, va_list ap) { +void linkdb_vforeach(struct linkdb_node **head, LinkDBFunc func, va_list ap) +{ struct linkdb_node *node; if( head == NULL ) return; node = *head; @@ -2941,14 +2944,15 @@ void linkdb_vforeach( struct linkdb_node** head, LinkDBFunc func, va_list ap) { } } -void linkdb_foreach( struct linkdb_node** head, LinkDBFunc func, ...) { +void linkdb_foreach(struct linkdb_node **head, LinkDBFunc func, ...) +{ va_list ap; va_start(ap, func); linkdb_vforeach(head, func, ap); va_end(ap); } -void* linkdb_search( struct linkdb_node** head, void *key) +void* linkdb_search(struct linkdb_node **head, void *key) { int n = 0; struct linkdb_node *node; @@ -2973,7 +2977,7 @@ void* linkdb_search( struct linkdb_node** head, void *key) return NULL; } -void* linkdb_erase( struct linkdb_node** head, void *key) +void* linkdb_erase(struct linkdb_node **head, void *key) { struct linkdb_node *node; if( head == NULL ) return NULL; @@ -2995,7 +2999,7 @@ void* linkdb_erase( struct linkdb_node** head, void *key) return NULL; } -void linkdb_replace( struct linkdb_node** head, void *key, void *data ) +void linkdb_replace(struct linkdb_node **head, void *key, void *data) { int n = 0; struct linkdb_node *node; @@ -3022,7 +3026,7 @@ void linkdb_replace( struct linkdb_node** head, void *key, void *data ) linkdb_insert( head, key, data ); } -void linkdb_final( struct linkdb_node** head ) +void linkdb_final(struct linkdb_node **head) { struct linkdb_node *node, *node2; if( head == NULL ) return ; @@ -3034,7 +3038,9 @@ void linkdb_final( struct linkdb_node** head ) } *head = NULL; } -void db_defaults(void) { + +void db_defaults(void) +{ DB = &DB_s; DB->alloc = db_alloc; DB->custom_release = db_custom_release; @@ -3055,5 +3061,4 @@ void db_defaults(void) { DB->ui2key = db_ui2key; DB->i642key = db_i642key; DB->ui642key = db_ui642key; - } diff --git a/src/common/db.h b/src/common/db.h index 1c0955221..2918e5acb 100644 --- a/src/common/db.h +++ b/src/common/db.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -1114,7 +1114,11 @@ HPShared struct db_interface *DB; * @param _vec Vector. */ #define VECTOR_INIT(_vec) \ - memset(&(_vec), 0, sizeof(_vec)) + do { \ + VECTOR_DATA(_vec) = NULL; \ + VECTOR_CAPACITY(_vec) = 0; \ + VECTOR_LENGTH(_vec) = 0; \ + } while(false) /** * Returns the internal array of values. @@ -1220,12 +1224,11 @@ HPShared struct db_interface *DB; */ #define VECTOR_ENSURE(_vec, _n, _step) \ do { \ - int _empty_ = VECTOR_CAPACITY(_vec)-VECTOR_LENGTH(_vec); \ - if ((_n) > _empty_) { \ - while ((_n) > _empty_) \ - _empty_ += (_step); \ - VECTOR_RESIZE(_vec, _empty_+VECTOR_LENGTH(_vec)); \ - } \ + int _newcapacity_ = VECTOR_CAPACITY(_vec); \ + while ((_n) + VECTOR_LENGTH(_vec) > _newcapacity_) \ + _newcapacity_ += (_step); \ + if (_newcapacity_ > VECTOR_CAPACITY(_vec)) \ + VECTOR_RESIZE(_vec, _newcapacity_); \ } while(false) /** diff --git a/src/common/des.c b/src/common/des.c index c680610e9..73297ab70 100644 --- a/src/common/des.c +++ b/src/common/des.c @@ -23,6 +23,7 @@ #include "des.h" #include "common/cbasetypes.h" +#include "common/nullpo.h" /** @file * Implementation of the des interface. @@ -54,6 +55,7 @@ static void des_IP(struct des_bit64 *src) struct des_bit64 tmp = {{0}}; int i; + nullpo_retv(src); for(i = 0; i < ARRAYLENGTH(ip_table); ++i) { uint8_t j = ip_table[i] - 1; if (src->b[(j >> 3) & 7] & mask[j & 7]) @@ -81,6 +83,7 @@ static void des_FP(struct des_bit64 *src) struct des_bit64 tmp = {{0}}; int i; + nullpo_retv(src); for (i = 0; i < ARRAYLENGTH(fp_table); ++i) { uint8_t j = fp_table[i] - 1; if (src->b[(j >> 3) & 7] & mask[j & 7]) @@ -119,6 +122,7 @@ static void des_E(struct des_bit64 *src) tmp.b[i / 6 + 0] |= mask[i % 6]; } #endif + nullpo_retv(src); // optimized tmp.b[0] = ((src->b[7]<<5) | (src->b[4]>>3)) & 0x3f; // ..0 vutsr tmp.b[1] = ((src->b[4]<<1) | (src->b[5]>>7)) & 0x3f; // ..srqpo n @@ -150,6 +154,7 @@ static void des_TP(struct des_bit64 *src) struct des_bit64 tmp = {{0}}; int i; + nullpo_retv(src); for (i = 0; i < ARRAYLENGTH(tp_table); ++i) { uint8_t j = tp_table[i] - 1; if (src->b[(j >> 3) + 0] & mask[j & 7]) @@ -194,6 +199,7 @@ static void des_SBOX(struct des_bit64 *src) struct des_bit64 tmp = {{0}}; int i; + nullpo_retv(src); for (i = 0; i < ARRAYLENGTH(s_table); ++i) { tmp.b[i] = (s_table[i][src->b[i*2+0]] & 0xf0) | (s_table[i][src->b[i*2+1]] & 0x0f); @@ -214,6 +220,7 @@ static void des_RoundFunction(struct des_bit64 *src) des_SBOX(&tmp); des_TP(&tmp); + nullpo_retv(src); src->b[0] ^= tmp.b[4]; src->b[1] ^= tmp.b[5]; src->b[2] ^= tmp.b[6]; diff --git a/src/common/ers.c b/src/common/ers.c index 8970fefc2..f2256cf30 100644 --- a/src/common/ers.c +++ b/src/common/ers.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -149,7 +149,8 @@ static struct ers_instance_t *InstanceList = NULL; /** * @param Options the options from the instance seeking a cache, we use it to give it a cache with matching configuration **/ -static ers_cache_t *ers_find_cache(unsigned int size, enum ERSOptions Options) { +static ers_cache_t *ers_find_cache(unsigned int size, enum ERSOptions Options) +{ ers_cache_t *cache; for (cache = CacheList; cache; cache = cache->Next) @@ -187,6 +188,7 @@ static void ers_free_cache(ers_cache_t *cache, bool remove) { unsigned int i; + nullpo_retv(cache); for (i = 0; i < cache->Used; i++) aFree(cache->Blocks[i]); @@ -307,7 +309,8 @@ static void ers_obj_destroy(ERS *self) aFree(instance); } -void ers_cache_size(ERS *self, unsigned int new_size) { +void ers_cache_size(ERS *self, unsigned int new_size) +{ struct ers_instance_t *instance = (struct ers_instance_t *)self; nullpo_retv(instance); @@ -319,10 +322,11 @@ void ers_cache_size(ERS *self, unsigned int new_size) { instance->Cache->ChunkSize = new_size; } - ERS *ers_new(uint32 size, char *name, enum ERSOptions options) { struct ers_instance_t *instance; + + nullpo_retr(NULL, name); CREATE(instance,struct ers_instance_t, 1); size += sizeof(struct ers_list); @@ -359,7 +363,8 @@ ERS *ers_new(uint32 size, char *name, enum ERSOptions options) return &instance->VTable; } -void ers_report(void) { +void ers_report(void) +{ ers_cache_t *cache; unsigned int cache_c = 0, blocks_u = 0, blocks_a = 0, memory_b = 0, memory_t = 0; #ifdef DEBUG @@ -403,7 +408,8 @@ void ers_report(void) { /** * Call on shutdown to clear remaining entries **/ -void ers_final(void) { +void ers_final(void) +{ struct ers_instance_t *instance = InstanceList, *next; while( instance ) { diff --git a/src/common/ers.h b/src/common/ers.h index 1689345dc..5f9516ad6 100644 --- a/src/common/ers.h +++ b/src/common/ers.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/grfio.c b/src/common/grfio.c index 0a9708f17..fba3dda86 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -86,11 +86,13 @@ struct grfio_interface *grfio; // little endian char array to uint conversion static unsigned int getlong(unsigned char *p) { + nullpo_ret(p); return (p[0] << 0 | p[1] << 8 | p[2] << 16 | p[3] << 24); } static void NibbleSwap(unsigned char *src, int len) { + nullpo_retv(src); while (len > 0) { *src = (*src >> 4) | (*src << 4); ++src; @@ -135,6 +137,7 @@ static void grf_shuffle_enc(struct des_bit64 *src) { struct des_bit64 out; + nullpo_retv(src); out.b[0] = src->b[3]; out.b[1] = src->b[4]; out.b[2] = src->b[5]; @@ -152,6 +155,7 @@ static void grf_shuffle_dec(struct des_bit64 *src) { struct des_bit64 out; + nullpo_retv(src); out.b[0] = src->b[3]; out.b[1] = src->b[4]; out.b[2] = src->b[6]; @@ -175,6 +179,7 @@ static void grf_decode_header(unsigned char *buf, size_t len) struct des_bit64 *p = (struct des_bit64 *)buf; size_t nblocks = len / sizeof(struct des_bit64); size_t i; + nullpo_retv(buf); // first 20 blocks are all des-encrypted for (i = 0; i < 20 && i < nblocks; ++i) @@ -197,6 +202,7 @@ static void grf_decode_full(unsigned char *buf, size_t len, int cycle) int dcycle, scycle; size_t i, j; + nullpo_retv(buf); // first 20 blocks are all des-encrypted for (i = 0; i < 20 && i < nblocks; ++i) des->decrypt_block(&p[i]); @@ -314,6 +320,7 @@ static void hashinit(void) static int grf_filehash(const char *fname) { uint32 hash = 0; + nullpo_ret(fname); while (*fname != '\0') { hash = (hash<<1) + (hash>>7)*9 + TOLOWER(*fname); fname++; @@ -396,7 +403,9 @@ static struct grf_filelist *grfio_filelist_add(struct grf_filelist *entry) */ static struct grf_filelist *grfio_filelist_modify(struct grf_filelist *entry) { - struct grf_filelist *fentry = grfio_filelist_find(entry->fn); + struct grf_filelist *fentry; + nullpo_retr(NULL, entry); + fentry = grfio_filelist_find(entry->fn); if (fentry != NULL) { int tmp = fentry->next; memcpy(fentry, entry, sizeof(struct grf_filelist)); @@ -434,6 +443,7 @@ static void grfio_localpath_create(char *buffer, size_t size, const char *filena int i; size_t len; + nullpo_retv(buffer); len = strlen(data_dir); if (data_dir[0] == '\0' || data_dir[len-1] == '/' || data_dir[len-1] == '\\') @@ -553,6 +563,7 @@ void *grfio_reads(const char *fname, int *size) static char *grfio_decode_filename(unsigned char *buf, int len) { int i; + nullpo_retr(NULL, buf); for (i = 0; i < len; i += 8) { NibbleSwap(&buf[i],8); des->decrypt(&buf[i],8); @@ -568,7 +579,9 @@ static char *grfio_decode_filename(unsigned char *buf, int len) */ static bool grfio_is_full_encrypt(const char *fname) { - const char *ext = strrchr(fname, '.'); + const char *ext; + nullpo_retr(false, fname); + ext = strrchr(fname, '.'); if (ext != NULL) { static const char *extensions[] = { ".gnd", ".gat", ".act", ".str" }; int i; @@ -594,8 +607,10 @@ static int grfio_entryread(const char *grfname, int gentry) unsigned char grf_header[0x2e] = { 0 }; int entry,entrys,ofs,grf_version; unsigned char *grf_filelist; + FILE *fp; - FILE *fp = fopen(grfname, "rb"); + nullpo_retr(1, grfname); + fp = fopen(grfname, "rb"); if (fp == NULL) { ShowWarning("GRF data file not found: '%s'\n", grfname); return 1; // 1:not found error @@ -764,6 +779,7 @@ static bool grfio_parse_restable_row(const char *row) char local[256]; struct grf_filelist *entry = NULL; + nullpo_retr(false, row); if (sscanf(row, "%255[^#\r\n]#%255[^#\r\n]#", w1, w2) != 2) return false; @@ -854,6 +870,7 @@ static void grfio_resourcecheck(void) */ static int grfio_add(const char *fname) { + nullpo_retr(1, fname); if (gentry_entrys >= gentry_maxentry) { #define GENTRY_ADDS 4 // The number increment of gentry_table entries gentry_maxentry += GENTRY_ADDS; @@ -899,6 +916,7 @@ void grfio_init(const char *fname) FILE *data_conf; int grf_num = 0; + nullpo_retv(fname); hashinit(); // hash table initialization data_conf = fopen(fname, "r"); diff --git a/src/common/mapindex.c b/src/common/mapindex.c index 5b0f6169b..e16eb4216 100644 --- a/src/common/mapindex.c +++ b/src/common/mapindex.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -25,6 +25,7 @@ #include "common/cbasetypes.h" #include "common/db.h" #include "common/mmo.h" +#include "common/nullpo.h" #include "common/showmsg.h" #include "common/strlib.h" @@ -37,11 +38,14 @@ struct mapindex_interface *mapindex; /// Retrieves the map name from 'string' (removing .gat extension if present). /// Result gets placed either into 'buf' or in a static local buffer. -const char* mapindex_getmapname(const char* string, char* output) { +const char* mapindex_getmapname(const char* string, char* output) +{ static char buf[MAP_NAME_LENGTH]; char* dest = (output != NULL) ? output : buf; - size_t len = strnlen(string, MAP_NAME_LENGTH_EXT); + size_t len; + nullpo_retr(buf, string); + len = strnlen(string, MAP_NAME_LENGTH_EXT); if (len == MAP_NAME_LENGTH_EXT) { ShowWarning("(mapindex_normalize_name) Map name '%*s' is too long!\n", 2*MAP_NAME_LENGTH_EXT, string); len--; @@ -58,12 +62,15 @@ const char* mapindex_getmapname(const char* string, char* output) { /// Retrieves the map name from 'string' (adding .gat extension if not already present). /// Result gets placed either into 'buf' or in a static local buffer. -const char* mapindex_getmapname_ext(const char* string, char* output) { +const char* mapindex_getmapname_ext(const char* string, char* output) +{ static char buf[MAP_NAME_LENGTH_EXT]; char* dest = (output != NULL) ? output : buf; size_t len; + nullpo_retr(buf, string); + safestrncpy(buf,string, sizeof(buf)); sscanf(string, "%*[^#]%*[#]%15s", buf); @@ -87,7 +94,8 @@ const char* mapindex_getmapname_ext(const char* string, char* output) { /// Adds a map to the specified index /// Returns 1 if successful, 0 otherwise -int mapindex_addmap(int index, const char* name) { +int mapindex_addmap(int index, const char* name) +{ char map_name[MAP_NAME_LENGTH]; if (index == -1){ @@ -128,7 +136,8 @@ int mapindex_addmap(int index, const char* name) { return index; } -unsigned short mapindex_name2id(const char* name) { +unsigned short mapindex_name2id(const char* name) +{ int i; char map_name[MAP_NAME_LENGTH]; @@ -141,7 +150,8 @@ unsigned short mapindex_name2id(const char* name) { return 0; } -const char *mapindex_id2name_sub(uint16 id, const char *file, int line, const char *func) { +const char *mapindex_id2name_sub(uint16 id, const char *file, int line, const char *func) +{ if (id >= MAX_MAPINDEX || !mapindex_exists(id)) { ShowDebug("mapindex_id2name: Requested name for non-existant map index [%d] in cache. %s:%s:%d\n", id,file,func,line); return mapindex->list[0].name; // dummy empty string so that the callee doesn't crash @@ -149,7 +159,8 @@ const char *mapindex_id2name_sub(uint16 id, const char *file, int line, const ch return mapindex->list[id].name; } -int mapindex_init(void) { +int mapindex_init(void) +{ FILE *fp; char line[1024]; int last_index = -1; @@ -196,16 +207,20 @@ bool mapindex_check_default(void) return true; } -void mapindex_removemap(int index){ +void mapindex_removemap(int index) +{ + Assert_retv(index < MAX_MAPINDEX); strdb_remove(mapindex->db, mapindex->list[index].name); mapindex->list[index].name[0] = '\0'; } -void mapindex_final(void) { +void mapindex_final(void) +{ db_destroy(mapindex->db); } -void mapindex_defaults(void) { +void mapindex_defaults(void) +{ mapindex = &mapindex_s; /* TODO: place it in inter-server.conf? */ diff --git a/src/common/mapindex.h b/src/common/mapindex.h index 0ebbeb04b..91f59aeaf 100644 --- a/src/common/mapindex.h +++ b/src/common/mapindex.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/md5calc.c b/src/common/md5calc.c index bd6b48f10..d2fc32371 100644 --- a/src/common/md5calc.c +++ b/src/common/md5calc.c @@ -168,16 +168,15 @@ static void md5_Round_Calculate(const unsigned char *block, } /// @copydoc md5_interface::binary() -static void md5_string2binary(const char *string, unsigned char *output) +static void md5_buf2binary(const uint8 *buf, const int buf_size, uint8 *output) { //var /*8bit*/ unsigned char padding_message[64]; //Extended message 512bit 64byte - const unsigned char *pstring; // The position of string in the present scanning notes is held. + const uint8 *pbuf; // The position of string in the present scanning notes is held. /*32bit*/ - unsigned int string_byte_len, //The byte chief of string is held. - string_bit_len, //The bit length of string is held. + unsigned int buf_bit_len, //The bit length of string is held. copy_len, //The number of bytes which is used by 1-3 and which remained msg_digest[4]; //Message digest 128bit 4byte unsigned int *A = &msg_digest[0], //The message digest in accordance with RFC (reference) @@ -195,16 +194,15 @@ static void md5_string2binary(const char *string, unsigned char *output) //Step 1.Append Padding Bits (extension of a mark bit) //1-1 - string_byte_len = (unsigned int)strlen(string); //The byte chief of a character sequence is acquired. - pstring = (const unsigned char *)string; // The position of the present character sequence is set. + pbuf = buf; // The position of the present character sequence is set. //1-2 Repeat calculation until length becomes less than 64 bytes. - for (i=string_byte_len; 64<=i; i-=64,pstring+=64) - md5_Round_Calculate(pstring, A,B,C,D); + for (i=buf_size; 64<=i; i-=64,pbuf+=64) + md5_Round_Calculate(pbuf, A,B,C,D); //1-3 - copy_len = string_byte_len % 64; //The number of bytes which remained is computed. - strncpy((char *)padding_message, (const char *)pstring, copy_len); // A message is copied to an extended bit sequence. + copy_len = buf_size % 64; //The number of bytes which remained is computed. + strncpy((char *)padding_message, (const char *)pbuf, copy_len); // A message is copied to an extended bit sequence. memset(padding_message+copy_len, 0, 64 - copy_len); //It buries by 0 until it becomes extended bit length. padding_message[copy_len] |= 0x80; //The next of a message is 1. @@ -216,12 +214,12 @@ static void md5_string2binary(const char *string, unsigned char *output) } //Step 2.Append Length (the information on length is added) - string_bit_len = string_byte_len * 8; //From the byte chief to bit length (32 bytes of low rank) - memcpy(&padding_message[56], &string_bit_len, 4); //32 bytes of low rank is set. + buf_bit_len = buf_size * 8; //From the byte chief to bit length (32 bytes of low rank) + memcpy(&padding_message[56], &buf_bit_len, 4); //32 bytes of low rank is set. //When bit length cannot be expressed in 32 bytes of low rank, it is a beam raising to a higher rank. - if (UINT_MAX / 8 < string_byte_len) { - unsigned int high = (string_byte_len - UINT_MAX / 8) * 8; + if (UINT_MAX / 8 < (unsigned int)buf_size) { + unsigned int high = (buf_size - UINT_MAX / 8) * 8; memcpy(&padding_message[60], &high, 4); } else { memset(&padding_message[60], 0, 4); //In this case, it is good for a higher rank at 0. @@ -237,12 +235,12 @@ static void md5_string2binary(const char *string, unsigned char *output) /// @copydoc md5_interface::string() void md5_string(const char *string, char *output) { - unsigned char digest[16]; + uint8 digest[16]; nullpo_retv(string); nullpo_retv(output); - md5->binary(string,digest); + md5->binary((const uint8 *)string, (int)strlen(string), digest); snprintf(output, 33, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", digest[ 0], digest[ 1], digest[ 2], digest[ 3], digest[ 4], digest[ 5], digest[ 6], digest[ 7], @@ -267,7 +265,7 @@ void md5_salt(int len, char *output) void md5_defaults(void) { md5 = &md5_s; - md5->binary = md5_string2binary; + md5->binary = md5_buf2binary; md5->string = md5_string; md5->salt = md5_salt; } diff --git a/src/common/md5calc.h b/src/common/md5calc.h index b4d4995f9..f55ebe312 100644 --- a/src/common/md5calc.h +++ b/src/common/md5calc.h @@ -46,7 +46,7 @@ struct md5_interface { * @param[in] string The source string. * @param[out] output Output buffer (at least 16 bytes available). */ - void (*binary) (const char *string, unsigned char *output); + void (*binary) (const uint8 *buf, const int buf_size, uint8 *output); /** * Generates a random salt. diff --git a/src/common/memmgr.c b/src/common/memmgr.c index dfea24465..b80b4d4e9 100644 --- a/src/common/memmgr.c +++ b/src/common/memmgr.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/memmgr.h b/src/common/memmgr.h index 680947466..6381c5bfa 100644 --- a/src/common/memmgr.h +++ b/src/common/memmgr.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -101,8 +101,10 @@ struct malloc_interface { void malloc_defaults(void); void memmgr_report(int extra); -#endif // HERCULES_CORE HPShared struct malloc_interface *iMalloc; +#else +#define iMalloc HPMi->memmgr +#endif // HERCULES_CORE #endif /* COMMON_MEMMGR_H */ diff --git a/src/common/mmo.h b/src/common/mmo.h index 77f706f0d..9c29b8a0e 100644 --- a/src/common/mmo.h +++ b/src/common/mmo.h @@ -131,7 +131,7 @@ #define MAX_FAME 1000000000 #define MAX_CART 100 #ifndef MAX_SKILL -#define MAX_SKILL 1478 +#define MAX_SKILL 1510 #endif #ifndef MAX_SKILL_ID #define MAX_SKILL_ID 10015 // [Ind/Hercules] max used skill ID @@ -259,12 +259,12 @@ // The following system marks a different job ID system used by the map server, // which makes a lot more sense than the normal one. [Skotlex] // These marks the "level" of the job. -#define JOBL_2_1 0x100 //256 -#define JOBL_2_2 0x200 //512 -#define JOBL_2 0x300 -#define JOBL_UPPER 0x1000 //4096 -#define JOBL_BABY 0x2000 //8192 -#define JOBL_THIRD 0x4000 //16384 +#define JOBL_2_1 0x0100 +#define JOBL_2_2 0x0200 +#define JOBL_2 0x0300 // JOBL_2_1 | JOBL_2_2 +#define JOBL_UPPER 0x1000 +#define JOBL_BABY 0x2000 +#define JOBL_THIRD 0x4000 #define SCRIPT_VARNAME_LENGTH 32 ///< Maximum length of a script variable @@ -566,7 +566,7 @@ struct mmo_charstatus { int zeny; int bank_vault; - short class_; + int16 class; int status_point, skill_point; int hp,max_hp,sp,max_sp; unsigned int option; @@ -670,7 +670,7 @@ struct party_member { int account_id; int char_id; char name[NAME_LENGTH]; - unsigned short class_; + int16 class; unsigned short map; unsigned short lv; unsigned leader : 1, @@ -689,7 +689,9 @@ struct party { struct map_session_data; struct guild_member { int account_id, char_id; - short hair,hair_color,gender,class_,lv; + short hair,hair_color,gender; + int16 class; + short lv; uint64 exp; int exp_payper; short online,position; @@ -777,6 +779,7 @@ struct fame_list { }; enum fame_list_type { + RANKTYPE_UNKNOWN = -1, RANKTYPE_BLACKSMITH = 0, RANKTYPE_ALCHEMIST = 1, RANKTYPE_TAEKWON = 2, @@ -993,6 +996,8 @@ enum { JOB_OBORO, JOB_REBELLION = 4215, + JOB_SUMMONER = 4218, + #ifndef JOB_MAX JOB_MAX, #endif diff --git a/src/common/mutex.c b/src/common/mutex.c index bdc2fb4dc..464a54161 100644 --- a/src/common/mutex.c +++ b/src/common/mutex.c @@ -24,6 +24,7 @@ #include "common/cbasetypes.h" // for WIN32 #include "common/memmgr.h" +#include "common/nullpo.h" #include "common/showmsg.h" #include "common/timer.h" @@ -84,6 +85,7 @@ struct mutex_data *mutex_create(void) /// @copydoc mutex_interface::destroy() void mutex_destroy(struct mutex_data *m) { + nullpo_retv(m); #ifdef WIN32 DeleteCriticalSection(&m->hMutex); #else @@ -96,6 +98,7 @@ void mutex_destroy(struct mutex_data *m) /// @copydoc mutex_interface::lock() void mutex_lock(struct mutex_data *m) { + nullpo_retv(m); #ifdef WIN32 EnterCriticalSection(&m->hMutex); #else @@ -106,6 +109,7 @@ void mutex_lock(struct mutex_data *m) /// @copydoc mutex_interface::trylock() bool mutex_trylock(struct mutex_data *m) { + nullpo_retr(false, m); #ifdef WIN32 if (TryEnterCriticalSection(&m->hMutex) != FALSE) return true; @@ -119,6 +123,7 @@ bool mutex_trylock(struct mutex_data *m) /// @copydoc mutex_interface::unlock() void mutex_unlock(struct mutex_data *m) { + nullpo_retv(m); #ifdef WIN32 LeaveCriticalSection(&m->hMutex); #else @@ -152,6 +157,7 @@ struct cond_data *cond_create(void) /// @copydoc mutex_interface::cond_destroy() void cond_destroy(struct cond_data *c) { + nullpo_retv(c); #ifdef WIN32 CloseHandle(c->events[EVENT_COND_SIGNAL]); CloseHandle(c->events[EVENT_COND_BROADCAST]); @@ -171,6 +177,7 @@ void cond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) int result; bool is_last = false; + nullpo_retv(c); EnterCriticalSection(&c->waiters_lock); c->nWaiters++; LeaveCriticalSection(&c->waiters_lock); @@ -201,6 +208,7 @@ void cond_wait(struct cond_data *c, struct mutex_data *m, sysint timeout_ticks) mutex->lock(m); #else + nullpo_retv(m); if (timeout_ticks < 0) { pthread_cond_wait(&c->hCond, &m->hMutex); } else { @@ -221,6 +229,7 @@ void cond_signal(struct cond_data *c) #ifdef WIN32 # if 0 bool has_waiters = false; + nullpo_retv(c); EnterCriticalSection(&c->waiters_lock); if(c->nWaiters > 0) has_waiters = true; @@ -230,6 +239,7 @@ void cond_signal(struct cond_data *c) # endif // 0 SetEvent(c->events[EVENT_COND_SIGNAL]); #else + nullpo_retv(c); pthread_cond_signal(&c->hCond); #endif } @@ -240,6 +250,7 @@ void cond_broadcast(struct cond_data *c) #ifdef WIN32 # if 0 bool has_waiters = false; + nullpo_retv(c); EnterCriticalSection(&c->waiters_lock); if(c->nWaiters > 0) has_waiters = true; @@ -249,6 +260,7 @@ void cond_broadcast(struct cond_data *c) # endif // 0 SetEvent(c->events[EVENT_COND_BROADCAST]); #else + nullpo_retv(c); pthread_cond_broadcast(&c->hCond); #endif } diff --git a/src/common/nullpo.c b/src/common/nullpo.c index 5b1be14ea..6525793bf 100644 --- a/src/common/nullpo.c +++ b/src/common/nullpo.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/nullpo.h b/src/common/nullpo.h index 098e669f3..28d058dc0 100644 --- a/src/common/nullpo.h +++ b/src/common/nullpo.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/showmsg.c b/src/common/showmsg.c index d8864684d..23679e762 100644 --- a/src/common/showmsg.c +++ b/src/common/showmsg.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/showmsg.h b/src/common/showmsg.h index 303c8dd28..eee6b467b 100644 --- a/src/common/showmsg.h +++ b/src/common/showmsg.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/socket.c b/src/common/socket.c index ea7bfab40..d4b8bb43f 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -520,7 +520,8 @@ void flush_fifos(void) /*====================================== * CORE : Connection functions *--------------------------------------*/ -int connect_client(int listen_fd) { +int connect_client(int listen_fd) +{ int fd; struct sockaddr_in client_address; socklen_t len; @@ -647,7 +648,8 @@ int make_listen_bind(uint32 ip, uint16 port) return fd; } -int make_connection(uint32 ip, uint16 port, struct hSockOpt *opt) { +int make_connection(uint32 ip, uint16 port, struct hSockOpt *opt) +{ struct sockaddr_in remote_address = { 0 }; int fd; int result; @@ -817,9 +819,12 @@ int rfifoskip(int fd, size_t len) int wfifoset(int fd, size_t len) { size_t newreserve; - struct socket_data* s = sockt->session[fd]; + struct socket_data* s; - if (!sockt->session_is_valid(fd) || s->wdata == NULL) + if (!sockt->session_is_valid(fd)) + return 0; + s = sockt->session[fd]; + if (s == NULL || s->wdata == NULL) return 0; // we have written len bytes to the buffer already before calling WFIFOSET @@ -1018,10 +1023,6 @@ int do_sockets(int next) } } -#ifdef __clang_analyzer__ - // Let Clang's static analyzer know this never happens (it thinks it might because of a NULL check in session_is_valid) - if (!sockt->session[i]) continue; -#endif // __clang_analyzer__ sockt->session[i]->func_parse(i); if(!sockt->session[i]) @@ -1199,7 +1200,8 @@ static int connect_check_(uint32 ip) /// Timer function. /// Deletes old connection history records. -static int connect_check_clear(int tid, int64 tick, int id, intptr_t data) { +static int connect_check_clear(int tid, int64 tick, int id, intptr_t data) +{ int clear = 0; int list = 0; struct connect_history *hist = NULL; @@ -1235,6 +1237,9 @@ int access_ipmask(const char *str, struct access_control *acc) uint32 ip; uint32 mask; + nullpo_ret(str); + nullpo_ret(acc); + if( strcmp(str,"all") == 0 ) { ip = 0; mask = 0; @@ -1736,9 +1741,11 @@ bool session_is_active(int fd) } // Resolves hostname into a numeric ip. -uint32 host2ip(const char* hostname) +uint32 host2ip(const char *hostname) { - struct hostent* h = gethostbyname(hostname); + struct hostent* h; + nullpo_ret(hostname); + h = gethostbyname(hostname); return (h != NULL) ? ntohl(*(uint32*)h->h_addr) : 0; } @@ -1771,7 +1778,8 @@ uint16 ntows(uint16 netshort) } /* [Ind/Hercules] - socket_datasync */ -void socket_datasync(int fd, bool send) { +void socket_datasync(int fd, bool send) +{ struct { unsigned int length;/* short is not enough for some */ } data_list[] = { @@ -2055,7 +2063,8 @@ void socket_net_config_read(const char *filename) return; } -void socket_defaults(void) { +void socket_defaults(void) +{ sockt = &sockt_s; sockt->fd_max = 0; diff --git a/src/common/socket.h b/src/common/socket.h index 947ea8d3e..e3a309f20 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/sql.c b/src/common/sql.c index 8da105872..c80edbce4 100644 --- a/src/common/sql.c +++ b/src/common/sql.c @@ -144,6 +144,7 @@ int Sql_GetColumnNames(struct Sql *self, const char *table, char *out_buf, size_ size_t len; size_t off = 0; + nullpo_retr(SQL_ERROR, out_buf); if( self == NULL || SQL_ERROR == SQL->Query(self, "EXPLAIN `%s`", table) ) return SQL_ERROR; @@ -377,7 +378,8 @@ void Sql_ShowDebug_(struct Sql *self, const char *debug_file, const unsigned lon } /// Frees a Sql handle returned by Sql_Malloc. -void Sql_Free(struct Sql *self) { +void Sql_Free(struct Sql *self) +{ if( self ) { SQL->FreeResult(self); @@ -414,6 +416,7 @@ static enum enum_field_types Sql_P_SizeToMysqlIntType(int sz) /// @private static int Sql_P_BindSqlDataType(MYSQL_BIND* bind, enum SqlDataType buffer_type, void* buffer, size_t buffer_len, unsigned long* out_length, int8* out_is_null) { + nullpo_retr(SQL_ERROR, bind); memset(bind, 0, sizeof(MYSQL_BIND)); switch( buffer_type ) { @@ -494,7 +497,8 @@ static int Sql_P_BindSqlDataType(MYSQL_BIND* bind, enum SqlDataType buffer_type, /// Prints debug information about a field (type and length). /// /// @private -static void Sql_P_ShowDebugMysqlFieldInfo(const char* prefix, enum enum_field_types type, int is_unsigned, unsigned long length, const char* length_postfix) { +static void Sql_P_ShowDebugMysqlFieldInfo(const char* prefix, enum enum_field_types type, int is_unsigned, unsigned long length, const char* length_postfix) +{ const char *sign = (is_unsigned ? "UNSIGNED " : ""); const char *type_string = NULL; switch (type) { @@ -535,6 +539,7 @@ static void SqlStmt_P_ShowDebugTruncatedColumn(struct SqlStmt *self, size_t i) MYSQL_FIELD* field; MYSQL_BIND* column; + nullpo_retv(self); meta = mysql_stmt_result_metadata(self->stmt); field = mysql_fetch_field_direct(meta, (unsigned int)i); ShowSQL("DB error - data of field '%s' was truncated.\n", field->name); @@ -874,8 +879,10 @@ void SqlStmt_Free(struct SqlStmt *self) aFree(self); } } + /* receives mysql error codes during runtime (not on first-time-connects) */ -void hercules_mysql_error_handler(unsigned int ecode) { +void hercules_mysql_error_handler(unsigned int ecode) +{ switch( ecode ) { case 2003:/* Can't connect to MySQL (this error only happens here when failing to reconnect) */ if( mysql_reconnect_type == 1 ) { @@ -1041,10 +1048,13 @@ void Sql_HerculesUpdateSkip(struct Sql *self, const char *filename) return; } -void Sql_Init(void) { +void Sql_Init(void) +{ Sql_inter_server_read("conf/common/inter-server.conf", false); // FIXME: Hardcoded path } -void sql_defaults(void) { + +void sql_defaults(void) +{ SQL = &sql_s; SQL->Connect = Sql_Connect; diff --git a/src/common/strlib.c b/src/common/strlib.c index b67adb63c..75ce2a272 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -629,6 +629,7 @@ int sv_parse(const char* str, int len, int startoff, char delim, int* out_pos, i svstate.delim = delim; svstate.done = false; svstate.start = 0; + svstate.end = 0; // parse count = 0; diff --git a/src/common/sysinfo.c b/src/common/sysinfo.c index aeb8d8e71..3c7e25a0c 100644 --- a/src/common/sysinfo.c +++ b/src/common/sysinfo.c @@ -31,6 +31,7 @@ #include "common/cbasetypes.h" #include "common/core.h" #include "common/memmgr.h" +#include "common/nullpo.h" #include "common/strlib.h" #include <stdio.h> // fopen @@ -237,11 +238,13 @@ enum windows_ver_suite { * @retval true if a revision was correctly detected. * @retval false if no revision was detected. out is set to NULL in this case. */ -bool sysinfo_svn_get_revision(char **out) { +bool sysinfo_svn_get_revision(char **out) +{ // Only include SVN support if detected it, or we're on MSVC #if !defined(SYSINFO_VCSTYPE) || SYSINFO_VCSTYPE == VCSTYPE_SVN || SYSINFO_VCSTYPE == VCSTYPE_UNKNOWN FILE *fp; + nullpo_ret(out); // subversion 1.7 uses a sqlite3 database // FIXME this is hackish at best... // - ignores database file structure @@ -291,6 +294,8 @@ bool sysinfo_svn_get_revision(char **out) { if (*out != NULL) return true; } +#else + nullpo_ret(out); #endif if (*out != NULL) aFree(*out); @@ -305,11 +310,13 @@ bool sysinfo_svn_get_revision(char **out) { * @retval true if a revision was correctly detected. * @retval false if no revision was detected. out is set to NULL in this case. */ -bool sysinfo_git_get_revision(char **out) { +bool sysinfo_git_get_revision(char **out) +{ // Only include Git support if we detected it, or we're on MSVC #if !defined(SYSINFO_VCSTYPE) || SYSINFO_VCSTYPE == VCSTYPE_GIT || SYSINFO_VCSTYPE == VCSTYPE_UNKNOWN char ref[128], filepath[128], line[128]; + nullpo_ret(out); strcpy(ref, "HEAD"); while (*ref) { @@ -334,6 +341,7 @@ bool sysinfo_git_get_revision(char **out) { if (*out != NULL) return true; #else + nullpo_ret(out); if (*out != NULL) aFree(*out); *out = NULL; @@ -351,7 +359,8 @@ typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD); * * Once retrieved, the version string is stored into sysinfo->p->osversion. */ -void sysinfo_osversion_retrieve(void) { +void sysinfo_osversion_retrieve(void) +{ OSVERSIONINFOEX osvi; StringBuf buf; ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); @@ -602,7 +611,8 @@ typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO); * System info is not stored anywhere after retrieval * @see http://msdn.microsoft.com/en-us/library/windows/desktop/ms724958(v=vs.85).aspx **/ -void sysinfo_systeminfo_retrieve( LPSYSTEM_INFO info ) { +void sysinfo_systeminfo_retrieve(LPSYSTEM_INFO info) +{ PGNSI pGNSI; // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise. @@ -619,7 +629,8 @@ void sysinfo_systeminfo_retrieve( LPSYSTEM_INFO info ) { * Returns number of bytes in a memory page * Only needed when compiling with MSVC **/ -long sysinfo_getpagesize( void ) { +long sysinfo_getpagesize(void) +{ SYSTEM_INFO si; ZeroMemory(&si, sizeof(SYSTEM_INFO)); @@ -633,7 +644,8 @@ long sysinfo_getpagesize( void ) { * Once retrieved, the name is stored into sysinfo->p->cpu and the * number of cores in sysinfo->p->cpucores. */ -void sysinfo_cpu_retrieve(void) { +void sysinfo_cpu_retrieve(void) +{ StringBuf buf; SYSTEM_INFO si; ZeroMemory(&si, sizeof(SYSTEM_INFO)); @@ -669,7 +681,8 @@ void sysinfo_cpu_retrieve(void) { * * Once retrieved, the name is stored into sysinfo->p->arch. */ -void sysinfo_arch_retrieve(void) { +void sysinfo_arch_retrieve(void) +{ SYSTEM_INFO si; ZeroMemory(&si, sizeof(SYSTEM_INFO)); @@ -697,7 +710,8 @@ void sysinfo_arch_retrieve(void) { * * Once retrieved, the value is stored in sysinfo->p->vcsrevision_src. */ -void sysinfo_vcsrevision_src_retrieve(void) { +void sysinfo_vcsrevision_src_retrieve(void) +{ if (sysinfo->p->vcsrevision_src != NULL) { aFree(sysinfo->p->vcsrevision_src); sysinfo->p->vcsrevision_src = NULL; @@ -721,7 +735,8 @@ void sysinfo_vcsrevision_src_retrieve(void) { * * Once retrieved, the value is stored in sysinfo->p->vcstype_name. */ -void sysinfo_vcstype_name_retrieve(void) { +void sysinfo_vcstype_name_retrieve(void) +{ if (sysinfo->p->vcstype_name != NULL) { aFree(sysinfo->p->vcstype_name); sysinfo->p->vcstype_name = NULL; @@ -750,7 +765,8 @@ void sysinfo_vcstype_name_retrieve(void) { * * Output example: "Linux", "Darwin", "Windows", etc. */ -const char *sysinfo_platform(void) { +const char *sysinfo_platform(void) +{ return sysinfo->p->platform; } @@ -768,7 +784,8 @@ const char *sysinfo_platform(void) { * Output example: "Windows 2008 Small Business Server", "OS X 10.8 Mountain Lion", * "Gentoo Base System Release 2.2", "Debian GNU/Linux 6.0.6 (squeeze)", etc. */ -const char *sysinfo_osversion(void) { +const char *sysinfo_osversion(void) +{ return sysinfo->p->osversion; } @@ -787,7 +804,8 @@ const char *sysinfo_osversion(void) { * "Intel(R) Xeon(R) CPU E5-1650 0 @ 3.20GHz", "Intel Core i7", * "x86 CPU, Family 6, Model 54, Stepping 1", etc. */ -const char *sysinfo_cpu(void) { +const char *sysinfo_cpu(void) +{ return sysinfo->p->cpu; } @@ -800,7 +818,8 @@ const char *sysinfo_cpu(void) { * * @return the number of CPU cores. */ -int sysinfo_cpucores(void) { +int sysinfo_cpucores(void) +{ return sysinfo->p->cpucores; } @@ -817,7 +836,8 @@ int sysinfo_cpucores(void) { * * Output example: "x86", "x86_64", "IA-64", "ARM", etc. */ -const char *sysinfo_arch(void) { +const char *sysinfo_arch(void) +{ return sysinfo->p->arch; } @@ -827,7 +847,8 @@ const char *sysinfo_arch(void) { * @retval true if this is a 64 bit build. * @retval false if this isn't a 64 bit build (i.e. it is a 32 bit build). */ -bool sysinfo_is64bit(void) { +bool sysinfo_is64bit(void) +{ #ifdef _LP64 return true; #else @@ -845,7 +866,8 @@ bool sysinfo_is64bit(void) { * Output example: "Microsoft Visual C++ 2012 (v170050727)", * "Clang v5.0.0", "MinGW32 v3.20", "GCC v4.7.3", etc. */ -const char *sysinfo_compiler(void) { +const char *sysinfo_compiler(void) +{ return sysinfo->p->compiler; } @@ -860,7 +882,8 @@ const char *sysinfo_compiler(void) { * * Output example: "-ggdb -O2 -flto -pipe -ffast-math ..." */ -const char *sysinfo_cflags(void) { +const char *sysinfo_cflags(void) +{ return sysinfo->p->cflags; } @@ -875,7 +898,8 @@ const char *sysinfo_cflags(void) { * * @see VCSTYPE_NONE, VCSTYPE_GIT, VCSTYPE_SVN, VCSTYPE_UNKNOWN */ -int sysinfo_vcstypeid(void) { +int sysinfo_vcstypeid(void) +{ return sysinfo->p->vcstype; } @@ -892,7 +916,8 @@ int sysinfo_vcstypeid(void) { * * Output example: "Git", "SVN", "Exported" */ -const char *sysinfo_vcstype(void) { +const char *sysinfo_vcstype(void) +{ return sysinfo->p->vcstype_name; } @@ -910,7 +935,8 @@ const char *sysinfo_vcstype(void) { * * Output example: Git: "9128feccf3bddda94a7f8a170305565416815b40", SVN: "17546" */ -const char *sysinfo_vcsrevision_src(void) { +const char *sysinfo_vcsrevision_src(void) +{ return sysinfo->p->vcsrevision_src; } @@ -926,7 +952,8 @@ const char *sysinfo_vcsrevision_src(void) { * * Output example: Git: "9128feccf3bddda94a7f8a170305565416815b40", SVN: "17546" */ -const char *sysinfo_vcsrevision_scripts(void) { +const char *sysinfo_vcsrevision_scripts(void) +{ return sysinfo->p->vcsrevision_scripts; } @@ -934,7 +961,8 @@ const char *sysinfo_vcsrevision_scripts(void) { * Reloads the run-time (scripts) VCS revision information. To be used during * script reloads to refresh the cached version. */ -void sysinfo_vcsrevision_reload(void) { +void sysinfo_vcsrevision_reload(void) +{ if (sysinfo->p->vcsrevision_scripts != NULL) { aFree(sysinfo->p->vcsrevision_scripts); sysinfo->p->vcsrevision_scripts = NULL; @@ -956,7 +984,8 @@ void sysinfo_vcsrevision_reload(void) { * @retval false if the current process is running as regular user, or * in any case under Windows. */ -bool sysinfo_is_superuser(void) { +bool sysinfo_is_superuser(void) +{ #ifndef _WIN32 if (geteuid() == 0) return true; @@ -967,7 +996,8 @@ bool sysinfo_is_superuser(void) { /** * Interface runtime initialization. */ -void sysinfo_init(void) { +void sysinfo_init(void) +{ sysinfo->p->compiler = SYSINFO_COMPILER; #ifdef WIN32 sysinfo->p->platform = "Windows"; @@ -993,7 +1023,8 @@ void sysinfo_init(void) { /** * Interface shutdown cleanup. */ -void sysinfo_final(void) { +void sysinfo_final(void) +{ #ifdef WIN32 // Only need to be free'd in win32, they're #defined elsewhere if (sysinfo->p->osversion) @@ -1035,7 +1066,8 @@ static const char *sysinfo_time(void) /** * Interface default values initialization. */ -void sysinfo_defaults(void) { +void sysinfo_defaults(void) +{ sysinfo = &sysinfo_s; memset(&sysinfo_p, '\0', sizeof(sysinfo_p)); sysinfo->p = &sysinfo_p; diff --git a/src/common/timer.c b/src/common/timer.c index 0b28f6a06..4f2b86a32 100644 --- a/src/common/timer.c +++ b/src/common/timer.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -258,10 +258,6 @@ int64 timer_gettick(void) { /// Adds a timer to the timer_heap static void push_timer_heap(int tid) { BHEAP_ENSURE(timer_heap, 1, 256); -#ifdef __clang_analyzer__ // Clang's static analyzer warns that BHEAP_ENSURE might set BHEAP_DATA(timer_heap) to NULL. -#include "assert.h" - assert(BHEAP_DATA(timer_heap) != NULL); -#endif // __clang_analyzer__ BHEAP_PUSH(timer_heap, tid, DIFFTICK_MINTOPCMP, swap); } diff --git a/src/common/timer.h b/src/common/timer.h index 2161f5e31..88c891dff 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/utils.c b/src/common/utils.c index 73df3aae1..bcfc153e3 100644 --- a/src/common/utils.c +++ b/src/common/utils.c @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify @@ -54,6 +54,9 @@ void WriteDump(FILE* fp, const void* buffer, size_t length) size_t i; char hex[48+1], ascii[16+1]; + nullpo_retv(fp); + nullpo_retv(buffer); + fprintf(fp, "--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n"); ascii[16] = 0; @@ -78,10 +81,12 @@ void WriteDump(FILE* fp, const void* buffer, size_t length) } /// Dumps given buffer on the console. -void ShowDump(const void *buffer, size_t length) { +void ShowDump(const void *buffer, size_t length) +{ size_t i; char hex[48+1], ascii[16+1]; + nullpo_retv(buffer); ShowDebug("--- 00-01-02-03-04-05-06-07-08-09-0A-0B-0C-0D-0E-0F 0123456789ABCDEF\n"); ascii[16] = 0; @@ -108,6 +113,7 @@ static char* checkpath(char *path, const char *srcpath) { // just make sure the char*path is not const char *p = path; + if (NULL == path || NULL == srcpath) return path; while(*srcpath) { @@ -400,7 +406,9 @@ int apply_percentrate(int value, int rate, int maxrate) //----------------------------------------------------- const char* timestamp2string(char* str, size_t size, time_t timestamp, const char* format) { - size_t len = strftime(str, size, format, localtime(×tamp)); + size_t len; + nullpo_retr(NULL, str); + len = strftime(str, size, format, localtime(×tamp)); memset(str + len, '\0', size - len); return str; } @@ -413,6 +421,7 @@ bool HCache_check(const char *file) char s_path[255], dT[1]; time_t rtime; + nullpo_retr(false, file); if (!(first = fopen(file,"rb"))) return false; @@ -456,10 +465,14 @@ bool HCache_check(const char *file) return true; } -FILE *HCache_open(const char *file, const char *opt) { +FILE *HCache_open(const char *file, const char *opt) +{ FILE *first; char s_path[255]; + nullpo_retr(NULL, file); + nullpo_retr(NULL, opt); + if( file[0] == '.' && file[1] == '/' ) file += 2; else if( file[0] == '.' ) @@ -498,15 +511,19 @@ void HCache_init(void) } /* transit to fread, shields vs warn_unused_result */ -size_t hread(void * ptr, size_t size, size_t count, FILE * stream) { +size_t hread(void *ptr, size_t size, size_t count, FILE *stream) +{ return fread(ptr, size, count, stream); } + /* transit to fwrite, shields vs warn_unused_result */ -size_t hwrite(const void * ptr, size_t size, size_t count, FILE * stream) { +size_t hwrite(const void *ptr, size_t size, size_t count, FILE *stream) +{ return fwrite(ptr, size, count, stream); } -void HCache_defaults(void) { +void HCache_defaults(void) +{ HCache = &HCache_s; HCache->init = HCache_init; diff --git a/src/common/utils.h b/src/common/utils.h index c5f64124f..9d3c323ef 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify diff --git a/src/common/winapi.h b/src/common/winapi.h index 724f052a0..b410e00cd 100644 --- a/src/common/winapi.h +++ b/src/common/winapi.h @@ -2,7 +2,7 @@ * This file is part of Hercules. * http://herc.ws - http://github.com/HerculesWS/Hercules * - * Copyright (C) 2012-2015 Hercules Dev Team + * Copyright (C) 2012-2016 Hercules Dev Team * Copyright (C) Athena Dev Teams * * Hercules is free software: you can redistribute it and/or modify |