--- deliantra/server/common/utils.C 2008/05/04 14:12:37 1.77 +++ deliantra/server/common/utils.C 2009/10/11 01:35:53 1.87 @@ -34,6 +34,10 @@ #include #include +#include + +#include +#include #include @@ -50,7 +54,7 @@ state [3] = state [2] * 69069U; if (state [0] < 128U) state [0] += 128U; for (int i = 11; --i; ) - operator ()(); + next (); } uint32_t @@ -64,19 +68,24 @@ return state [0] ^ state [1] ^ state [2] ^ state [3]; } +template uint32_t -tausworthe_random_generator::get_range (uint32_t num) +random_number_generator::get_range (uint32_t num) { - return (next () * (uint64_t)num) >> 32U; + return (this->next () * (uint64_t)num) >> 32U; } // return a number within (min .. max) +template int -tausworthe_random_generator::get_range (int r_min, int r_max) +random_number_generator::get_range (int r_min, int r_max) { return r_min + get_range (max (r_max - r_min + 1, 0)); } +template struct random_number_generator; +template struct random_number_generator; + /* * The random functions here take luck into account when rolling random * dice or numbers. This function has less of an impact the larger the @@ -95,13 +104,9 @@ int random_roll (int r_min, int r_max, const object *op, int goodbad) { - int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ + r_max = max (r_min, r_max); - if (r_max < r_min) - { - LOG (llevError | logBacktrace, "Calling random_roll with min=%d max=%d\n", r_min, r_max); - return r_min; - } + int base = r_max - r_min > 1 ? 20 : 50; /* d2 and d3 are corner cases */ if (op->type == PLAYER) { @@ -121,17 +126,11 @@ * for exp loss calculations for players changing religions. */ sint64 -random_roll64 (sint64 min, sint64 max, const object *op, int goodbad) +random_roll64 (sint64 r_min, sint64 r_max, const object *op, int goodbad) { - sint64 omin = min; - sint64 diff = max - min + 1; - int base = diff > 2 ? 20 : 50; /* d2 and d3 are corner cases */ - - if (diff < 0) - { - LOG (llevError | logBacktrace, "Calling random_roll64 with min=%" PRId64 " max=%" PRId64 "\n", min, max); - return (min); /* avoids a float exception */ - } + sint64 omin = r_min; + sint64 range = max (0, r_max - r_min + 1); + int base = range > 2 ? 20 : 50; /* d2 and d3 are corner cases */ /* * Make a call to get two 32 bit unsigned random numbers, and just to @@ -140,24 +139,24 @@ sint64 ran = (sint64) rndm.next () ^ ((sint64) rndm.next () << 31); if (op->type != PLAYER) - return ((ran % diff) + min); + return ((ran % range) + r_min); int luck = op->stats.luck; - if (rndm (base) < MIN (10, abs (luck))) + if (rndm (base) < min (10, abs (luck))) { /* we have a winner */ ((luck > 0) ? (luck = 1) : (luck = -1)); - diff -= luck; - if (diff < 1) + range -= luck; + if (range < 1) return (omin); /*check again */ - ((goodbad) ? (min += luck) : (diff)); + ((goodbad) ? (r_min += luck) : (range)); - return (MAX (omin, MIN (max, (ran % diff) + min))); + return (max (omin, min (r_max, (ran % range) + r_min))); } - return ran % diff + min; + return ran % range + r_min; } /* @@ -209,9 +208,9 @@ /* convert materialname to materialtype_t */ materialtype_t * -name_to_material (const shstr &name) +name_to_material (const shstr_cmp name) { - for (materialtype_t *mt = materialt; mt && mt->next; mt = mt->next) + for (materialtype_t *mt = materialt; mt; mt = mt->next) if (name == mt->name) return mt; @@ -227,10 +226,10 @@ materialtype_t *mt; int j; - if (op->materialname == NULL) + if (!op->materialname) return; - if (change->materialname != NULL && strcmp (op->materialname, change->materialname)) + if (op->materialname != change->materialname) return; if (!op->is_armor ()) @@ -260,14 +259,16 @@ { materialtype_t *mt, *lmt; - if (op->materialname != NULL) + if (!op->materialname) return; - if (nmt == NULL) + if (nmt) + lmt = nmt; + else { - lmt = NULL; + lmt = 0; - for (mt = materialt; mt && mt->next; mt = mt->next) + for (mt = materialt; mt; mt = mt->next) if (op->materials & mt->material && rndm (1, 100) <= mt->chance && difficulty >= mt->difficulty && (op->magic >= mt->magic || mt->magic == 0)) { @@ -276,10 +277,8 @@ break; } } - else - lmt = nmt; - if (lmt != NULL) + if (lmt) { if (op->stats.dam && op->is_weapon ()) { @@ -451,6 +450,27 @@ return; } +/******************************************************************************/ + +/* Checks a player-provided string which will become the msg property of + * an object for dangerous input. + */ +bool +msg_is_safe (const char *msg) +{ + bool safe = true; + + /* Trying to cheat by getting data into the object */ + if (!strncmp (msg, "endmsg", strlen ("endmsg")) || strstr (msg, "\nendmsg")) + safe = false; + + /* Trying to make the object talk, and potentially access arbitrary code */ + if (object::msg_has_dialogue (msg)) + safe = false; + + return safe; +} + ///////////////////////////////////////////////////////////////////////////// void @@ -458,10 +478,39 @@ { if (!fork ()) { - signal (SIGABRT, SIG_DFL); + signal (SIGINT , SIG_IGN); + signal (SIGTERM, SIG_IGN); + signal (SIGABRT, SIG_IGN); + + signal (SIGSEGV, SIG_DFL); + signal (SIGBUS , SIG_DFL); + signal (SIGILL , SIG_DFL); + signal (SIGTRAP, SIG_DFL); + // try to put corefiles into a subdirectory, if existing, to allow // an administrator to reduce the I/O load. chdir ("cores"); + + // try to detach us from as many external dependencies as possible + // as coredumping can take time by closing all fd's. + { + struct rlimit lim; + + if (getrlimit (RLIMIT_NOFILE, &lim)) + lim.rlim_cur = 1024; + + for (int i = 0; i < lim.rlim_cur; ++i) + close (i); + } + + { + sigset_t empty; + sigemptyset (&empty); + sigprocmask (SIG_SETMASK, &empty, 0); + } + + // try to coredump with SIGTRAP + kill (getpid (), SIGTRAP); abort (); } @@ -534,28 +583,33 @@ /******************************************************************************/ -void assign (char *dst, const char *src, int maxlen) +int +assign (char *dst, const char *src, int maxsize) { if (!src) src = ""; int len = strlen (src); - if (len >= maxlen - 1) + if (len >= maxsize) { - if (maxlen <= 4) + if (maxsize <= 4) { - memset (dst, '.', maxlen - 1); - dst [maxlen - 1] = 0; + memset (dst, '.', maxsize - 2); + dst [maxsize - 1] = 0; } else { - memcpy (dst, src, maxlen - 4); - memcpy (dst + maxlen - 4, "...", 4); + memcpy (dst, src, maxsize - 4); + memcpy (dst + maxsize - 4, "...", 4); } + + len = maxsize; } else - memcpy (dst, src, len + 1); + memcpy (dst, src, ++len); + + return len; } const char *