diff options
author | William Wilgus <wilgus.william@gmail.com> | 2022-11-14 11:32:34 -0500 |
---|---|---|
committer | William Wilgus <me.theuser@yahoo.com> | 2022-11-14 23:56:16 -0500 |
commit | f6c719d7ec71cc7771c46d3daa390924a3871ba3 (patch) | |
tree | e6209f23565db01809f75067247e667963092ff6 /firmware | |
parent | b25a9d8f99b75570d18ea64602de7fe48da612d6 (diff) | |
download | rockbox-f6c719d7ec71cc7771c46d3daa390924a3871ba3.tar.gz rockbox-f6c719d7ec71cc7771c46d3daa390924a3871ba3.zip |
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
Diffstat (limited to 'firmware')
-rw-r--r-- | firmware/SOURCES | 1 | ||||
-rw-r--r-- | firmware/buflib.c | 4 | ||||
-rw-r--r-- | firmware/common/dircache.c | 8 | ||||
-rw-r--r-- | firmware/common/strlcpy.c | 64 | ||||
-rw-r--r-- | firmware/common/strmemccpy.c | 38 | ||||
-rw-r--r-- | firmware/drivers/lcd-bitmap-common.c | 2 | ||||
-rw-r--r-- | firmware/font.c | 2 | ||||
-rw-r--r-- | firmware/general.c | 4 | ||||
-rw-r--r-- | firmware/include/string-extra.h | 1 | ||||
-rw-r--r-- | firmware/include/strmemccpy.h | 32 |
10 files changed, 108 insertions, 48 deletions
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 | |||
240 | common/strnatcmp.c | 240 | common/strnatcmp.c |
241 | common/strlcat.c | 241 | common/strlcat.c |
242 | common/strlcpy.c | 242 | common/strlcpy.c |
243 | common/strmemccpy.c | ||
243 | common/structec.c | 244 | common/structec.c |
244 | common/timefuncs.c | 245 | common/timefuncs.c |
245 | common/unicode.c | 246 | 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 @@ | |||
30 | #include <stdio.h> /* for snprintf() */ | 30 | #include <stdio.h> /* for snprintf() */ |
31 | #include <stddef.h> /* for ptrdiff_t */ | 31 | #include <stddef.h> /* for ptrdiff_t */ |
32 | #include "buflib.h" | 32 | #include "buflib.h" |
33 | #include "string-extra.h" /* strlcpy() */ | 33 | #include "string-extra.h" /* strmemccpy() */ |
34 | #include "debug.h" | 34 | #include "debug.h" |
35 | #include "panic.h" | 35 | #include "panic.h" |
36 | #include "crc32.h" | 36 | #include "crc32.h" |
@@ -974,7 +974,7 @@ buflib_alloc_maximum(struct buflib_context* ctx, const char* name, size_t *size, | |||
974 | if (*size <= 0) /* OOM */ | 974 | if (*size <= 0) /* OOM */ |
975 | return -1; | 975 | return -1; |
976 | 976 | ||
977 | strlcpy(buf, name, sizeof(buf)); | 977 | strmemccpy(buf, name, sizeof(buf)); |
978 | 978 | ||
979 | return buflib_alloc_ex(ctx, *size, buf, ops); | 979 | return buflib_alloc_ex(ctx, *size, buf, ops); |
980 | } | 980 | } |
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) | |||
1707 | /* save current paths size */ | 1707 | /* save current paths size */ |
1708 | int pathpos = strlen(sab_path); | 1708 | int pathpos = strlen(sab_path); |
1709 | /* append entry */ | 1709 | /* append entry */ |
1710 | strlcpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos); | 1710 | strmemccpy(&sab_path[pathpos], "/", sizeof(sab_path) - pathpos); |
1711 | strlcpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1); | 1711 | strmemccpy(&sab_path[pathpos+1], entry->d_name, sizeof(sab_path) - pathpos - 1); |
1712 | 1712 | ||
1713 | int rc = sab_process_dir(ce->down); | 1713 | int rc = sab_process_dir(ce->down); |
1714 | /* restore path */ | 1714 | /* restore path */ |
@@ -1735,7 +1735,7 @@ static int sab_process_dir(struct dircache_entry *ce) | |||
1735 | static int sab_process_volume(IF_MV(int volume,) struct dircache_entry *ce) | 1735 | static int sab_process_volume(IF_MV(int volume,) struct dircache_entry *ce) |
1736 | { | 1736 | { |
1737 | memset(ce, 0, sizeof(struct dircache_entry)); | 1737 | memset(ce, 0, sizeof(struct dircache_entry)); |
1738 | strlcpy(sab_path, "/", sizeof sab_path); | 1738 | strmemccpy(sab_path, "/", sizeof sab_path); |
1739 | return sab_process_dir(ce); | 1739 | return sab_process_dir(ce); |
1740 | } | 1740 | } |
1741 | 1741 | ||
@@ -1755,7 +1755,7 @@ int dircache_readdir_r(struct dircache_dirscan *dir, struct DIRENT *result) | |||
1755 | 1755 | ||
1756 | dir->scanidx = ce - dircache_root; | 1756 | dir->scanidx = ce - dircache_root; |
1757 | 1757 | ||
1758 | strlcpy(result->d_name, ce->d_name, sizeof (result->d_name)); | 1758 | strmemccpy(result->d_name, ce->d_name, sizeof (result->d_name)); |
1759 | result->info = ce->dirinfo; | 1759 | result->info = ce->dirinfo; |
1760 | 1760 | ||
1761 | return 1; | 1761 | 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 @@ | |||
1 | /* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ | 1 | /*************************************************************************** |
2 | 2 | * __________ __ ___. | |
3 | /* | 3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ |
4 | * Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> | 4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / |
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
5 | * | 9 | * |
6 | * Permission to use, copy, modify, and distribute this software for any | 10 | * Copyright (C) 2022 William Wilgus |
7 | * purpose with or without fee is hereby granted, provided that the above | ||
8 | * copyright notice and this permission notice appear in all copies. | ||
9 | * | 11 | * |
10 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | 12 | * This program is free software; you can redistribute it and/or |
11 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | 13 | * modify it under the terms of the GNU General Public License |
12 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | 14 | * as published by the Free Software Foundation; either version 2 |
13 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 15 | * of the License, or (at your option) any later version. |
14 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 16 | * |
15 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
16 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 18 | * KIND, either express or implied. |
17 | */ | 19 | * |
20 | ****************************************************************************/ | ||
18 | 21 | ||
19 | #include <string.h> | 22 | #include <string.h> |
23 | #include "strmemccpy.h" | ||
20 | 24 | ||
21 | /* | 25 | /* |
22 | * Copy src to string dst of size siz. At most siz-1 characters | 26 | * Copy src to string dst of size siz. At most siz-1 characters |
23 | * will be copied. Always NUL terminates (unless siz == 0). | 27 | * will be copied. Always NUL terminates (unless siz == 0). |
24 | * Returns strlen(src); if retval >= siz, truncation occurred. | 28 | * Returns strlen(src); if retval >= siz, truncation occurred. |
25 | */ | 29 | */ |
26 | size_t | 30 | size_t strlcpy(char *dst, const char *src, size_t siz) |
27 | strlcpy(char *dst, const char *src, size_t siz) | ||
28 | { | 31 | { |
29 | char *d = dst; | 32 | /* Copy as many bytes as will fit */ |
30 | const char *s = src; | 33 | char *d = strmemccpy(dst, src, siz); |
31 | size_t n = siz; | 34 | if (d) |
32 | 35 | return (d - dst - 1); /* count does not include NUL */ | |
33 | /* Copy as many bytes as will fit */ | ||
34 | if (n != 0) { | ||
35 | while (--n != 0) { | ||
36 | if ((*d++ = *s++) == '\0') | ||
37 | break; | ||
38 | } | ||
39 | } | ||
40 | 36 | ||
41 | /* Not enough room in dst, add NUL and traverse rest of src */ | 37 | /* Not enough room in dst, add NUL and traverse rest of src */ |
42 | if (n == 0) { | 38 | return(siz + strlen(src+siz)); /* count does not include NUL */ |
43 | if (siz != 0) | ||
44 | *d = '\0'; /* NUL-terminate dst */ | ||
45 | while (*s++) | ||
46 | ; | ||
47 | } | ||
48 | |||
49 | return(s - src - 1); /* count does not include NUL */ | ||
50 | } | 39 | } |
51 | |||
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2022 William Wilgus | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | /* (firmware/common/strmemccpy.c) */ | ||
22 | #include "string-extra.h" | ||
23 | |||
24 | /* copies src to a buffer of len bytes stopping after | ||
25 | * len or the first NULL (\0) in src | ||
26 | * NULL terminates except when len = 0 | ||
27 | * If len was exceeded NULL is returned otherwise returns | ||
28 | * a pointer to the first byte following the NULL in dst. | ||
29 | */ | ||
30 | char * strmemccpy(char *dst, const char *src, size_t len) | ||
31 | { | ||
32 | char * ret = (char *)memccpy(dst, src, '\0', len); | ||
33 | if (ret == NULL && len > 0) | ||
34 | { | ||
35 | dst[len - 1] = '\0'; | ||
36 | } | ||
37 | return ret; | ||
38 | } | ||
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, | |||
699 | } | 699 | } |
700 | 700 | ||
701 | /* copy contents to the line buffer */ | 701 | /* copy contents to the line buffer */ |
702 | strlcpy(s->linebuffer, string, sizeof(s->linebuffer)); | 702 | strmemccpy(s->linebuffer, string, sizeof(s->linebuffer)); |
703 | /* scroll bidirectional or forward only depending on the string width */ | 703 | /* scroll bidirectional or forward only depending on the string width */ |
704 | if ( LCDFN(scroll_info).bidir_limit ) { | 704 | if ( LCDFN(scroll_info).bidir_limit ) { |
705 | s->bidir = w < (vp->width) * | 705 | 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) | |||
854 | static void font_path_to_glyph_path( const char *font_path, char *glyph_path) | 854 | static void font_path_to_glyph_path( const char *font_path, char *glyph_path) |
855 | { | 855 | { |
856 | /* take full file name, cut extension, and add .glyphcache */ | 856 | /* take full file name, cut extension, and add .glyphcache */ |
857 | strlcpy(glyph_path, font_path, MAX_PATH); | 857 | strmemccpy(glyph_path, font_path, MAX_PATH); |
858 | glyph_path[strlen(glyph_path)-4] = '\0'; | 858 | glyph_path[strlen(glyph_path)-4] = '\0'; |
859 | strcat(glyph_path, ".gc"); | 859 | strcat(glyph_path, ".gc"); |
860 | } | 860 | } |
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, | |||
107 | int suffixlen = strlen(suffix); | 107 | int suffixlen = strlen(suffix); |
108 | 108 | ||
109 | if (buffer != path) | 109 | if (buffer != path) |
110 | strlcpy(buffer, path, MAX_PATH); | 110 | strmemccpy(buffer, path, MAX_PATH); |
111 | 111 | ||
112 | pathlen = strlen(buffer); | 112 | pathlen = strlen(buffer); |
113 | 113 | ||
@@ -181,7 +181,7 @@ char *create_datetime_filename(char *buffer, const char *path, | |||
181 | last_tm = *tm; | 181 | last_tm = *tm; |
182 | 182 | ||
183 | if (buffer != path) | 183 | if (buffer != path) |
184 | strlcpy(buffer, path, MAX_PATH); | 184 | strmemccpy(buffer, path, MAX_PATH); |
185 | 185 | ||
186 | pathlen = strlen(buffer); | 186 | pathlen = strlen(buffer); |
187 | snprintf(buffer + pathlen, MAX_PATH - pathlen, | 187 | 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 @@ | |||
25 | #include "strlcat.h" | 25 | #include "strlcat.h" |
26 | #include "strcasecmp.h" | 26 | #include "strcasecmp.h" |
27 | #include "strcasestr.h" | 27 | #include "strcasestr.h" |
28 | #include "strmemccpy.h" | ||
28 | #include "strtok_r.h" | 29 | #include "strtok_r.h" |
29 | #include "memset16.h" | 30 | #include "memset16.h" |
30 | 31 | ||
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 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2022 William Wilgus | ||
11 | * | ||
12 | * This program is free software; you can redistribute it and/or | ||
13 | * modify it under the terms of the GNU General Public License | ||
14 | * as published by the Free Software Foundation; either version 2 | ||
15 | * of the License, or (at your option) any later version. | ||
16 | * | ||
17 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
18 | * KIND, either express or implied. | ||
19 | * | ||
20 | ****************************************************************************/ | ||
21 | |||
22 | |||
23 | #ifndef __STRMEMCCPY_H__ | ||
24 | #define __STRMEMCCPY_H__ | ||
25 | /* copies src to a buffer of len bytes stopping after | ||
26 | * len or the first NULL (\0) in src | ||
27 | * NULL terminates except when len = 0 | ||
28 | * If len was exceeded NULL is returned otherwise returns | ||
29 | * a pointer to the first byte following the NULL in dst. | ||
30 | */ | ||
31 | char * strmemccpy(char *dst, const char *src, size_t len); | ||
32 | #endif | ||