str_itoa for platforms that do not have itoa.

remove more static string buffer warning code (bsdstring) from reports.
This commit is contained in:
Enno Rehling 2018-11-23 13:05:38 +01:00
parent 037113b35d
commit af72febebc
3 changed files with 17 additions and 15 deletions

View File

@ -2198,28 +2198,23 @@ static void eval_resources(struct opstack **stack, const void *userdata)
const struct locale *lang = f ? f->locale : default_locale; const struct locale *lang = f ? f->locale : default_locale;
const struct resource *res = (const struct resource *)opop(stack).v; const struct resource *res = (const struct resource *)opop(stack).v;
char buf[1024]; /* but we only use about half of this */ char buf[1024]; /* but we only use about half of this */
size_t size = sizeof(buf) - 1;
variant var; variant var;
sbstring sbs;
char *bufp = buf;
while (res != NULL && size > 4) { sbs_init(&sbs, buf, sizeof(buf));
while (res != NULL) {
const char *rname = const char *rname =
resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0); resourcename(res->type, (res->number != 1) ? NMF_PLURAL : 0);
int result = snprintf(bufp, size, "%d %s", res->number, LOC(lang, rname)); sbs_strcat(&sbs, str_itoa(res->number));
if (wrptr(&bufp, &size, result) != 0 || size < sizeof(buf) / 2) { sbs_strcat(&sbs, "");
WARN_STATIC_BUFFER(); sbs_strcat(&sbs, LOC(lang, rname));
break;
}
res = res->next; res = res->next;
if (res != NULL && size > 2) { if (res != NULL) {
strcat(bufp, ", "); sbs_strcat(&sbs, ", ");
bufp += 2;
size -= 2;
} }
} }
*bufp = 0; var.v = strcpy(balloc(sbs_length(&sbs)), buf);
var.v = strcpy(balloc((size_t)(bufp - buf + 1)), buf);
opush(stack, var); opush(stack, var);
} }

View File

@ -34,6 +34,12 @@ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include <string.h> #include <string.h>
#endif #endif
const char *str_itoa(int n) {
static char buf[12];
snprintf(buf, sizeof(buf), "%d", n);
return buf;
}
size_t str_strlcpy(char *dst, const char *src, size_t len) size_t str_strlcpy(char *dst, const char *src, size_t len)
{ {
#ifdef HAVE_BSDSTRING #ifdef HAVE_BSDSTRING

View File

@ -28,6 +28,7 @@ extern "C" {
void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value); void str_replace(char *buffer, size_t size, const char *tmpl, const char *var, const char *value);
int str_hash(const char *s); int str_hash(const char *s);
const char *str_itoa(int i);
size_t str_slprintf(char * dst, size_t size, const char * format, ...); size_t str_slprintf(char * dst, size_t size, const char * format, ...);
size_t str_strlcpy(char *dst, const char *src, size_t len); size_t str_strlcpy(char *dst, const char *src, size_t len);
size_t str_strlcat(char *dst, const char *src, size_t len); size_t str_strlcat(char *dst, const char *src, size_t len);