From f6c719d7ec71cc7771c46d3daa390924a3871ba3 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Mon, 14 Nov 2022 11:32:34 -0500 Subject: replace strlcpy with strmemccpy replace applicable calls to strlcpy with calls to strmemccpy which null terminates on truncation in theory the strmemccpy calls should be slightly faster since they don't traverse the rest of the source string on truncation but I seriously doubt there is too much of that going on in the code base Change-Id: Ia0251514e36a6242bbf3f03c5e0df123aba60ed2 --- firmware/SOURCES | 1 + firmware/buflib.c | 4 +-- firmware/common/dircache.c | 8 ++--- firmware/common/strlcpy.c | 64 +++++++++++++++--------------------- firmware/common/strmemccpy.c | 38 +++++++++++++++++++++ firmware/drivers/lcd-bitmap-common.c | 2 +- firmware/font.c | 2 +- firmware/general.c | 4 +-- firmware/include/string-extra.h | 1 + firmware/include/strmemccpy.h | 32 ++++++++++++++++++ 10 files changed, 108 insertions(+), 48 deletions(-) create mode 100644 firmware/common/strmemccpy.c create mode 100644 firmware/include/strmemccpy.h (limited to 'firmware') diff --git a/firmware/SOURCES b/firmware/SOURCES index 94a986c9f8..b13a5ac304 100644 --- a/firmware/SOURCES +++ b/firmware/SOURCES @@ -240,6 +240,7 @@ common/strcasestr.c common/strnatcmp.c common/strlcat.c common/strlcpy.c +common/strmemccpy.c common/structec.c common/timefuncs.c common/unicode.c diff --git a/firmware/buflib.c b/firmware/buflib.c index 3130bc960c..2ce9cc344c 100644 --- a/firmware/buflib.c +++ b/firmware/buflib.c @@ -30,7 +30,7 @@ #include /* for snprintf() */ #include /* for ptrdiff_t */ #include "buflib.h" -#include "string-extra.h" /* strlcpy() */ +#include "string-extra.h" /* strmemccpy() */ #include "debug.h" #include "panic.h" #include "crc32.h" @@ -974,7 +974,7 @@ buflib_alloc_maximum(struct buflib_context* ctx, const char* name, size_t *size, if (*size <= 0) /* OOM */ return -1; - strlcpy(buf, name, sizeof(buf)); + strmemccpy(buf, name, sizeof(buf)); return buflib_alloc_ex(ctx, *size, buf, ops); } diff --git a/firmware/common/dircache.c b/firmware/common/dircache.c index 1d6371a6b5..5902f8b3fd 100644 --- a/firmware/common/dircache.c +++ b/firmware/common/dircache.c @@ -1707,8 +1707,8 @@ static int sab_process_dir(struct dircache_entry *ce) /* save current paths size */ int pathpos = strlen(sab_path); /* append entry */ - strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos); - strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1); + strmemccpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos); + strmemccpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1); int rc = sab_process_dir(ce->down); /* restore path */ @@ -1735,7 +1735,7 @@ static int sab_process_dir(struct dircache_entry *ce) static int sab_process_volume(IF_MV(int volume,) struct dircache_entry *ce) { memset(ce, 0, sizeof(struct dircache_entry)); - strlcpy(sab_path, "/", sizeof sab_path); + strmemccpy(sab_path, "/", sizeof sab_path); return sab_process_dir(ce); } @@ -1755,7 +1755,7 @@ int dircache_readdir_r(struct dircache_dirscan *dir, struct DIRENT *result) dir->scanidx = ce - dircache_root; - strlcpy(result->d_name, ce->d_name, sizeof (result->d_name)); + strmemccpy(result->d_name, ce->d_name, sizeof (result->d_name)); result->info = ce->dirinfo; return 1; diff --git a/firmware/common/strlcpy.c b/firmware/common/strlcpy.c index e320649140..bfdb6482a4 100644 --- a/firmware/common/strlcpy.c +++ b/firmware/common/strlcpy.c @@ -1,51 +1,39 @@ -/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ - -/* - * Copyright (c) 1998 Todd C. Miller +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ * - * Permission to use, copy, modify, and distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. + * Copyright (C) 2022 William Wilgus * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR - * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN - * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF - * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - */ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ #include +#include "strmemccpy.h" /* * Copy src to string dst of size siz. At most siz-1 characters * will be copied. Always NUL terminates (unless siz == 0). * Returns strlen(src); if retval >= siz, truncation occurred. */ -size_t -strlcpy(char *dst, const char *src, size_t siz) +size_t strlcpy(char *dst, const char *src, size_t siz) { - char *d = dst; - const char *s = src; - size_t n = siz; - - /* Copy as many bytes as will fit */ - if (n != 0) { - while (--n != 0) { - if ((*d++ = *s++) == '\0') - break; - } - } + /* Copy as many bytes as will fit */ + char *d = strmemccpy(dst, src, siz); + if (d) + return (d - dst - 1); /* count does not include NUL */ - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - - return(s - src - 1); /* count does not include NUL */ + /* Not enough room in dst, add NUL and traverse rest of src */ + return(siz + strlen(src+siz)); /* count does not include NUL */ } - diff --git a/firmware/common/strmemccpy.c b/firmware/common/strmemccpy.c new file mode 100644 index 0000000000..830907f55e --- /dev/null +++ b/firmware/common/strmemccpy.c @@ -0,0 +1,38 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2022 William Wilgus + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ +/* (firmware/common/strmemccpy.c) */ +#include "string-extra.h" + +/* copies src to a buffer of len bytes stopping after + * len or the first NULL (\0) in src + * NULL terminates except when len = 0 + * If len was exceeded NULL is returned otherwise returns + * a pointer to the first byte following the NULL in dst. +*/ +char * strmemccpy(char *dst, const char *src, size_t len) +{ + char * ret = (char *)memccpy(dst, src, '\0', len); + if (ret == NULL && len > 0) + { + dst[len - 1] = '\0'; + } + return ret; +} diff --git a/firmware/drivers/lcd-bitmap-common.c b/firmware/drivers/lcd-bitmap-common.c index 389d30917b..975c494b5a 100644 --- a/firmware/drivers/lcd-bitmap-common.c +++ b/firmware/drivers/lcd-bitmap-common.c @@ -699,7 +699,7 @@ static bool LCDFN(puts_scroll_worker)(int x, int y, const unsigned char *string, } /* copy contents to the line buffer */ - strlcpy(s->linebuffer, string, sizeof(s->linebuffer)); + strmemccpy(s->linebuffer, string, sizeof(s->linebuffer)); /* scroll bidirectional or forward only depending on the string width */ if ( LCDFN(scroll_info).bidir_limit ) { s->bidir = w < (vp->width) * diff --git a/firmware/font.c b/firmware/font.c index 97a15221fc..d7473346da 100644 --- a/firmware/font.c +++ b/firmware/font.c @@ -854,7 +854,7 @@ const unsigned char* font_get_bits(struct font* pf, unsigned short char_code) static void font_path_to_glyph_path( const char *font_path, char *glyph_path) { /* take full file name, cut extension, and add .glyphcache */ - strlcpy(glyph_path, font_path, MAX_PATH); + strmemccpy(glyph_path, font_path, MAX_PATH); glyph_path[strlen(glyph_path)-4] = '\0'; strcat(glyph_path, ".gc"); } diff --git a/firmware/general.c b/firmware/general.c index 8508b34b88..d421d722a8 100644 --- a/firmware/general.c +++ b/firmware/general.c @@ -107,7 +107,7 @@ char *create_numbered_filename(char *buffer, const char *path, int suffixlen = strlen(suffix); if (buffer != path) - strlcpy(buffer, path, MAX_PATH); + strmemccpy(buffer, path, MAX_PATH); pathlen = strlen(buffer); @@ -181,7 +181,7 @@ char *create_datetime_filename(char *buffer, const char *path, last_tm = *tm; if (buffer != path) - strlcpy(buffer, path, MAX_PATH); + strmemccpy(buffer, path, MAX_PATH); pathlen = strlen(buffer); snprintf(buffer + pathlen, MAX_PATH - pathlen, diff --git a/firmware/include/string-extra.h b/firmware/include/string-extra.h index 549a018dfc..a9b34661a7 100644 --- a/firmware/include/string-extra.h +++ b/firmware/include/string-extra.h @@ -25,6 +25,7 @@ #include "strlcat.h" #include "strcasecmp.h" #include "strcasestr.h" +#include "strmemccpy.h" #include "strtok_r.h" #include "memset16.h" diff --git a/firmware/include/strmemccpy.h b/firmware/include/strmemccpy.h new file mode 100644 index 0000000000..c7004610dd --- /dev/null +++ b/firmware/include/strmemccpy.h @@ -0,0 +1,32 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2022 William Wilgus + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + + +#ifndef __STRMEMCCPY_H__ +#define __STRMEMCCPY_H__ +/* copies src to a buffer of len bytes stopping after + * len or the first NULL (\0) in src + * NULL terminates except when len = 0 + * If len was exceeded NULL is returned otherwise returns + * a pointer to the first byte following the NULL in dst. +*/ +char * strmemccpy(char *dst, const char *src, size_t len); +#endif -- cgit v1.2.3