From 5bcbf27fe11f86c77beddeba66b369d31e1e81c9 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 3 Oct 2017 09:59:55 -0400 Subject: [PATCH] fix metadata DB insert without trailing pipe character fixes #374 Change-Id: Ibfe89b7804183ef04eba39d29e01b70160f7c264 --- lib/str.h | 13 +++++++++++++ recording-daemon/db.c | 8 +++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/lib/str.h b/lib/str.h index 5235509f8..1a02b26bc 100644 --- a/lib/str.h +++ b/lib/str.h @@ -72,6 +72,8 @@ INLINE int str_to_i(str *s, int def); INLINE uint str_to_ui(str *s, int def); /* extracts the first/next token into "new_token" and modifies "ori_and_remainer" in place */ INLINE int str_token(str *new_token, str *ori_and_remainder, int sep); +/* same as str_token but allows for a trailing non-empty token (e.g. "foo,bar" -> "foo", "bar" ) */ +INLINE int str_token_sep(str *new_token, str *ori_and_remainder, int sep); /* copy a string to a regular C string buffer, limiting the max size */ INLINE char *str_ncpy(char *dst, size_t bufsize, const str *src); @@ -320,6 +322,17 @@ INLINE int str_token(str *new_token, str *ori_and_remainder, int sep) { return 0; } +INLINE int str_token_sep(str *new_token, str *ori_and_remainder, int sep) { + str ori = *ori_and_remainder; + if (!str_token(new_token, ori_and_remainder, sep)) + return 0; + // separator not found, use remainder as final token if not empty + if (!ori.len) + return -1; + *new_token = ori; + return 0; +} + INLINE int str_uri_encode(char *out, const str *in) { return str_uri_encode_len(out, in->s, in->len); } diff --git a/recording-daemon/db.c b/recording-daemon/db.c index 076fe6e69..99a2598f9 100644 --- a/recording-daemon/db.c +++ b/recording-daemon/db.c @@ -219,11 +219,9 @@ static void db_do_call_metadata(metafile_t *mf) { str_init(&all_meta, mf->metadata); while (all_meta.len > 1) { str token; - if (str_token(&token, &all_meta, '|')) { - // separator not found, use remainder as token - token = all_meta; - all_meta.len = 0; - } + if (str_token_sep(&token, &all_meta, '|')) + break; + str key; if (str_token(&key, &token, ':')) { // key:value separator not found, skip