From c29eceaeeda5e0cc91f830fd49b96a3b6c1d5751 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Sun, 15 Oct 2017 16:14:20 +0200 Subject: [PATCH] make map_name() interpret empty strings as "no separator" empty strings were previously meaningless, and starting with 72c2d695a, failure to handle them lead to bogus results when the IMAP hierarchy separator is legitimately empty (when the server genuinely supports none and none is manually configured). non-null can be asserted more cleanly than null-or-non-empty, so change the api like that. incidentally, this also removes the need to work around gcc's bogus warning in -Os mode. problem found by "Casper Ti. Vector" --- src/config.c | 2 ++ src/main.c | 4 ++-- src/sync.c | 2 +- src/util.c | 27 ++++++++++----------------- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/config.c b/src/config.c index 20f09d9..8ddfe26 100644 --- a/src/config.c +++ b/src/config.c @@ -355,6 +355,8 @@ load_config( const char *where, int pseudo ) if (store) { if (!store->max_size) store->max_size = INT_MAX; + if (!store->flat_delim) + store->flat_delim = ""; *storeapp = store; storeapp = &store->next; *storeapp = 0; diff --git a/src/main.c b/src/main.c index 3ba4323..eadd81f 100644 --- a/src/main.c +++ b/src/main.c @@ -983,7 +983,7 @@ store_connected( int sts, void *aux ) flags |= LIST_INBOX; } else if (c == '/') { /* Flattened sub-folders of INBOX actually end up in Path. */ - if (mvars->ctx[t]->conf->flat_delim) + if (mvars->ctx[t]->conf->flat_delim[0]) flags |= LIST_PATH; else flags |= LIST_INBOX; @@ -1027,7 +1027,7 @@ store_listed( int sts, string_list_t *boxes, void *aux ) return; case DRV_OK: for (box = boxes; box; box = box->next) { - if (mvars->ctx[t]->conf->flat_delim) { + if (mvars->ctx[t]->conf->flat_delim[0]) { string_list_t *nbox; if (map_name( box->string, (char **)&nbox, offsetof(string_list_t, string), mvars->ctx[t]->conf->flat_delim, "/" ) < 0) { error( "Error: flattened mailbox name '%s' contains canonical hierarchy delimiter\n", box->string ); diff --git a/src/sync.c b/src/sync.c index 8f2b4a2..7397db2 100644 --- a/src/sync.c +++ b/src/sync.c @@ -1014,7 +1014,7 @@ sync_boxes( store_t *ctx[], const char *names[], int present[], channel_conf_t * svars->orig_name[t] = (!names[t] || (ctx[t]->conf->map_inbox && !strcmp( ctx[t]->conf->map_inbox, names[t] ))) ? "INBOX" : names[t]; - if (!ctx[t]->conf->flat_delim) { + if (!ctx[t]->conf->flat_delim[0]) { svars->box_name[t] = nfstrdup( svars->orig_name[t] ); } else if (map_name( svars->orig_name[t], &svars->box_name[t], 0, "/", ctx[t]->conf->flat_delim ) < 0) { error( "Error: canonical mailbox name '%s' contains flattened hierarchy delimiter\n", svars->orig_name[t] ); diff --git a/src/util.c b/src/util.c index 3b624bd..f4dce49 100644 --- a/src/util.c +++ b/src/util.c @@ -479,19 +479,20 @@ map_name( const char *arg, char **result, int reserve, const char *in, const cha char *p; int i, l, ll, num, inl, outl; + assert( arg ); l = strlen( arg ); - if (!in) { + assert( in ); + inl = strlen( in ); + if (!inl) { copy: *result = nfmalloc( reserve + l + 1 ); memcpy( *result + reserve, arg, l + 1 ); return 0; } - inl = strlen( in ); - if (out) { - outl = strlen( out ); - if (inl == outl && !memcmp( in, out, inl )) - goto copy; - } + assert( out ); + outl = strlen( out ); + if (inl == outl && !memcmp( in, out, inl )) + goto copy; for (num = 0, i = 0; i < l; ) { for (ll = 0; ll < inl; ll++) if (arg[i + ll] != in[ll]) @@ -500,7 +501,7 @@ map_name( const char *arg, char **result, int reserve, const char *in, const cha i += inl; continue; fout: - if (out) { + if (outl) { for (ll = 0; ll < outl; ll++) if (arg[i + ll] != out[ll]) goto fnexti; @@ -511,7 +512,7 @@ map_name( const char *arg, char **result, int reserve, const char *in, const cha } if (!num) goto copy; - if (!out) + if (!outl) return -2; *result = nfmalloc( reserve + l + num * (outl - inl) + 1 ); p = *result + reserve; @@ -519,15 +520,7 @@ map_name( const char *arg, char **result, int reserve, const char *in, const cha for (ll = 0; ll < inl; ll++) if (arg[i + ll] != in[ll]) goto rnexti; -#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__clang__) -# pragma GCC diagnostic push -/* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=42145 */ -# pragma GCC diagnostic ignored "-Wmaybe-uninitialized" -#endif memcpy( p, out, outl ); -#if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)) && !defined(__clang__) -# pragma GCC diagnostic pop -#endif p += outl; i += inl; continue;