diff options
author | Michael Jennings <mej@kainx.org> | 2004-06-29 21:18:09 +0000 |
---|---|---|
committer | Michael Jennings <mej@kainx.org> | 2004-06-29 21:18:09 +0000 |
commit | 27e4de9f2ab2012548baa1e32363546ab8b1fa0d (patch) | |
tree | 3ec98a5c3c379eb56026d535d2a147baeb61d25f /src | |
parent | c262f4d46e5af5f4caf5a98d1706b11fe1e8d728 (diff) | |
download | libast-27e4de9f2ab2012548baa1e32363546ab8b1fa0d.tar.gz |
Tue Jun 29 17:14:13 2004 Michael Jennings (mej)
Some small fixes suggested by Charlie Gordon <gmane@chqrlie.org>.
Minor build tweaks...updated to new release number.
----------------------------------------------------------------------
SVN revision: 10642
Diffstat (limited to 'src')
-rw-r--r-- | src/conf.c | 17 | ||||
-rw-r--r-- | src/file.c | 3 | ||||
-rw-r--r-- | src/mem.c | 5 | ||||
-rw-r--r-- | src/strings.c | 42 |
4 files changed, 55 insertions, 12 deletions
@@ -502,9 +502,10 @@ spifconf_shell_expand(char *s) switch (*pbuff) { case '~': D_CONF(("Tilde detected.\n")); - if (!in_single && !in_double && getenv("HOME")) { - strncpy(newbuff + j, getenv("HOME"), max - j); - cnt1 = strlen(getenv("HOME")) - 1; + EnvVar = getenv("HOME"); + if (!in_single && !in_double && EnvVar && *EnvVar) { + spiftool_safe_strncpy(newbuff + j, getenv("HOME"), max - j); + cnt1 = strlen(EnvVar) - 1; cnt2 = max - j - 1; j += MIN(cnt1, cnt2); } else { @@ -588,8 +589,8 @@ spifconf_shell_expand(char *s) FREE(Command); if (Output) { if (*Output) { + spiftool_safe_strncpy(newbuff + j, Output, max - j); l = strlen(Output) - 1; - strncpy(newbuff + j, Output, max - j); cnt2 = max - j - 1; j += MIN(l, cnt2); } else { @@ -618,8 +619,8 @@ spifconf_shell_expand(char *s) FREE(Command); if (Output) { if (*Output) { + spiftool_safe_strncpy(newbuff + j, Output, max - j); l = strlen(Output) - 1; - strncpy(newbuff + j, Output, max - j); cnt2 = max - j - 1; j += MIN(l, cnt2); } else { @@ -656,8 +657,8 @@ spifconf_shell_expand(char *s) break; } EnvVar[k] = 0; - if ((tmp = getenv(EnvVar))) { - strncpy(newbuff, tmp, max - j); + if ((tmp = getenv(EnvVar)) && *tmp) { + spiftool_safe_strncpy(newbuff, tmp, max - j); cnt1 = strlen(tmp) - 1; cnt2 = max - j - 1; j += MIN(cnt1, cnt2); @@ -759,7 +760,7 @@ spifconf_find_file(const char *file, const char *dir, const char *pathlist) /* Don't try if it's too long */ if (n > 0 && n <= maxpathlen) { /* Compose the /path/file combo */ - strncpy(full_path, path, n); + memcpy(full_path, path, n); if (full_path[n - 1] != '/') { full_path[n++] = '/'; } @@ -84,8 +84,7 @@ spiftool_temp_file(char *ftemplate, size_t len) } if (len) { - strncpy(ftemplate, buff, len); - ftemplate[len - 1] = 0; + spiftool_safe_strncpy(ftemplate, buff, len); } return (fd); } @@ -156,7 +156,7 @@ memrec_add_var(memrec_t *memrec, const char *filename, unsigned long line, void D_MEM(("Storing as pointer #%lu at %10p (from %10p).\n", memrec->cnt, p, memrec->ptrs)); p->ptr = ptr; p->size = size; - strncpy(p->file, filename, LIBAST_FNAME_LEN); + spiftool_safe_strncpy(p->file, filename, LIBAST_FNAME_LEN); p->file[LIBAST_FNAME_LEN] = 0; p->line = line; } @@ -267,7 +267,7 @@ memrec_chg_var(memrec_t *memrec, const char *var, const char *filename, unsigned D_MEM(("Changing variable %s (%10p, %lu -> %10p, %lu)\n", var, oldp, p->size, newp, size)); p->ptr = newp; p->size = size; - strncpy(p->file, filename, LIBAST_FNAME_LEN); + spiftool_safe_strncpy(p->file, filename, LIBAST_FNAME_LEN); p->line = line; } @@ -581,6 +581,7 @@ spifmem_strdup(const char *var, const char *filename, unsigned long line, const register char *newstr; register size_t len; + ASSERT_RVAL(!SPIF_PTR_ISNULL(str), SPIF_NULL_TYPE_C(char *)); USE_VAR(var); D_MEM(("Variable %s (%10p) at %s:%lu\n", var, str, NONULL(filename), line)); diff --git a/src/strings.c b/src/strings.c index 6ae09fa..4081c06 100644 --- a/src/strings.c +++ b/src/strings.c @@ -53,6 +53,7 @@ memmem(const void *haystack, register size_t haystacklen, const void *needle, re REQUIRE_RVAL(haystack != SPIF_NULL_TYPE(ptr), SPIF_NULL_TYPE(ptr)); REQUIRE_RVAL(needlelen > 0, SPIF_NULL_TYPE(ptr)); REQUIRE_RVAL(haystacklen > 0, SPIF_NULL_TYPE(ptr)); + REQUIRE_RVAL(haystacklen > needlelen, SPIF_NULL_TYPE(ptr)); for (i = 0; i < len; i++) { if (!memcmp(hs + i, n, needlelen)) { return (hs + i); @@ -195,6 +196,47 @@ strsep(char **str, register char *sep) #endif /** + * Safer strncpy() with no NUL padding or wasted calculations. + */ +spif_bool_t +spiftool_safe_strncpy(spif_charptr_t dest, const spif_charptr_t src, spif_int32_t size) +{ + spif_char_t c; + spif_charptr_t s = src, pbuff = dest; + spif_charptr_t max_pbuff = dest + size - 1; + + ASSERT_RVAL(!SPIF_PTR_ISNULL(dest), FALSE); + REQUIRE_RVAL(!SPIF_PTR_ISNULL(src), FALSE); + REQUIRE_RVAL(size > 0, FALSE); + + for (; (c = *s) && (pbuff < max_pbuff); s++, pbuff++) { + *pbuff = c; + } + *pbuff = 0; + return ((c == 0) ? (TRUE) : (FALSE)); +} + +/** + * Variant of strncat() which uses the safe strncpy() replacement above. + */ +spif_bool_t +spiftool_safe_strncat(spif_charptr_t dest, const spif_charptr_t src, spif_int32_t size) +{ + spif_int32_t len; + + ASSERT_RVAL(!SPIF_PTR_ISNULL(dest), FALSE); + REQUIRE_RVAL(!SPIF_PTR_ISNULL(src), FALSE); + REQUIRE_RVAL(size > 0, FALSE); + + len = strnlen(dest, size); + if ((len < 0) || (len >= size)) { + return FALSE; + } else { + return spiftool_safe_strncpy(dest + len, size - len, src); + } +} + +/** * Returns a portion of a larger string. */ spif_charptr_t |