diff options
author | Volker Lendecke <vl@samba.org> | 2017-08-02 17:34:25 +0200 |
---|---|---|
committer | Karolin Seeger <kseeger@samba.org> | 2018-05-24 12:50:13 +0200 |
commit | 18664edf162e3a660e67934c515220c696c73774 (patch) | |
tree | 9b90888baaa6143bceab72515c9f625ce905775e | |
parent | 5c5c38be03e9c7aa1b04338bcef2bcca52119011 (diff) | |
download | samba-18664edf162e3a660e67934c515220c696c73774.tar.gz |
lib: Allow parsing a strv from a non-talloc const buf
This will allow parsing a tdb record without having to talloc_memdup it
BUG: https://bugzilla.samba.org/show_bug.cgi?id=13369
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Ralph Boehme <slow@samba.org>
-rw-r--r-- | lib/util/strv.c | 26 | ||||
-rw-r--r-- | lib/util/strv.h | 2 |
2 files changed, 19 insertions, 9 deletions
diff --git a/lib/util/strv.c b/lib/util/strv.c index 328f561722b..83d84d92528 100644 --- a/lib/util/strv.c +++ b/lib/util/strv.c @@ -86,29 +86,37 @@ static bool strv_valid_entry(const char *strv, size_t strv_len, return true; } -char *strv_next(char *strv, const char *entry) +const char *strv_len_next(const char *strv, size_t strv_len, + const char *entry) { - size_t len = talloc_array_length(strv); size_t entry_len; - char *result; if (entry == NULL) { - if (strv_valid_entry(strv, len, strv, NULL)) { + if (strv_valid_entry(strv, strv_len, strv, NULL)) { return strv; } return NULL; } - if (!strv_valid_entry(strv, len, entry, &entry_len)) { + if (!strv_valid_entry(strv, strv_len, entry, &entry_len)) { return NULL; } - result = &strv[entry - strv]; /* avoid const problems with this stmt */ - result += entry_len + 1; - if (result >= (strv + len)) { + entry += entry_len+1; + + if (entry >= (strv + strv_len)) { return NULL; } - return result; + return entry; +} + +char *strv_next(char *strv, const char *entry) +{ + size_t len = talloc_array_length(strv); + const char *result; + + result = strv_len_next(strv, len, entry); + return discard_const_p(char, result); } size_t strv_count(char *strv) diff --git a/lib/util/strv.h b/lib/util/strv.h index 398e8ead171..89f04023e44 100644 --- a/lib/util/strv.h +++ b/lib/util/strv.h @@ -26,6 +26,8 @@ int strv_add(TALLOC_CTX *mem_ctx, char **strv, const char *string); int strv_addn(TALLOC_CTX *mem_ctx, char **strv, const char *src, size_t srclen); int strv_append(TALLOC_CTX *mem_ctx, char **strv, const char *src); char *strv_next(char *strv, const char *entry); +const char *strv_len_next(const char *strv, size_t strv_len, + const char *entry); char *strv_find(char *strv, const char *entry); size_t strv_count(char *strv); void strv_delete(char **strv, char *entry); |