]> granicus.if.org Git - neomutt/commitdiff
Encode URIs created with nm_uri_from_query 692/head
authorWilliam Pettersson <william.pettersson@gmail.com>
Sun, 30 Jul 2017 04:59:38 +0000 (14:59 +1000)
committerRichard Russon <rich@flatcap.org>
Fri, 4 Aug 2017 11:31:00 +0000 (12:31 +0100)
Uses url_pct_encode to do the encoding. Note that url_pct_encode now
also encodes the ampersand, which shouldn't cause problems given that
it's only used for usernames and passwords otherwise.

Addresses issue #603

mutt_notmuch.c
url.c
url.h

index fca5bac8bed17e65cf6576b73a4b2a7bf2fe82bc..9533d7d882150aaab9023231adcaac70948294d1 100644 (file)
@@ -1841,16 +1841,25 @@ char *nm_uri_from_query(struct Context *ctx, char *buf, size_t bufsz)
   mutt_debug(2, "nm_uri_from_query (%s)\n", buf);
   struct NmCtxData *data = get_ctxdata(ctx);
   char uri[_POSIX_PATH_MAX + LONG_STRING + 32]; /* path to DB + query + URI "decoration" */
+  int added;
 
   if (data)
-    snprintf(uri, sizeof(uri), "notmuch://%s?query=%s", get_db_filename(data), buf);
+    added = snprintf(uri, sizeof(uri), "notmuch://%s?query=", get_db_filename(data));
   else if (NotmuchDefaultUri)
-    snprintf(uri, sizeof(uri), "%s?query=%s", NotmuchDefaultUri, buf);
+    added = snprintf(uri, sizeof(uri), "%s?query=", NotmuchDefaultUri);
   else if (Maildir)
-    snprintf(uri, sizeof(uri), "notmuch://%s?query=%s", Maildir, buf);
+    added = snprintf(uri, sizeof(uri), "notmuch://%s?query=", Maildir);
   else
     return NULL;
 
+  if (added >= sizeof(uri))
+  {
+    // snprintf output was truncated, so can't create URI
+    return NULL;
+  }
+
+  url_pct_encode(&uri[added], sizeof(uri) - added, buf);
+
   strncpy(buf, uri, bufsz);
   buf[bufsz - 1] = '\0';
 
diff --git a/url.c b/url.c
index eb610054429010c90e74d0b91d9743caf28ec6d2..5d827c63d79f323ca22f1e66c7cdea86cf944580 100644 (file)
--- a/url.c
+++ b/url.c
@@ -186,7 +186,7 @@ int url_parse_ciss(struct CissUrl *ciss, char *src)
   return ciss_parse_userhost(ciss, tmp);
 }
 
-static void url_pct_encode(char *dst, size_t l, const char *src)
+void url_pct_encode(char *dst, size_t l, const char *src)
 {
   static const char *alph = "0123456789ABCDEF";
 
@@ -194,7 +194,7 @@ static void url_pct_encode(char *dst, size_t l, const char *src)
   l--;
   while (src && *src && l)
   {
-    if (strchr("/:%", *src) && l > 3)
+    if (strchr("/:&%", *src) && l > 3)
     {
       *dst++ = '%';
       *dst++ = alph[(*src >> 4) & 0xf];
diff --git a/url.h b/url.h
index d7332a770a4897ee0a3583b0e7ece1eeb0913eac..1940d1ca2ea20421eaa32c897783701b087bd887 100644 (file)
--- a/url.h
+++ b/url.h
@@ -67,5 +67,6 @@ int url_parse_ciss(struct CissUrl *ciss, char *src);
 int url_ciss_tostring(struct CissUrl *ciss, char *dest, size_t len, int flags);
 int url_parse_mailto(struct Envelope *e, char **body, const char *src);
 int url_pct_decode(char *s);
+void url_pct_encode(char *dest, size_t len, const char *src);
 
 #endif /* _MUTT_URL_H */