[vlc-commits] httpd_RedirectNew: avoid strdup() with unhandled error

Rémi Denis-Courmont git at videolan.org
Sun Apr 27 16:02:34 CEST 2014


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Apr 27 16:59:19 2014 +0300| [b9b166207baef873d20de0487b336e77a6764cfd] | committer: Rémi Denis-Courmont

httpd_RedirectNew: avoid strdup() with unhandled error

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=b9b166207baef873d20de0487b336e77a6764cfd
---

 src/network/httpd.c |   15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/src/network/httpd.c b/src/network/httpd.c
index 23a4012..6fd0235 100644
--- a/src/network/httpd.c
+++ b/src/network/httpd.c
@@ -525,7 +525,7 @@ httpd_handler_sys_t *httpd_HandlerDelete(httpd_handler_t *handler)
 struct httpd_redirect_t
 {
     httpd_url_t *url;
-    char        *psz_dst;
+    char         dst[1];
 };
 
 static int httpd_RedirectCallBack(httpd_callback_sys_t *p_sys,
@@ -544,11 +544,11 @@ static int httpd_RedirectCallBack(httpd_callback_sys_t *p_sys,
     answer->i_type   = HTTPD_MSG_ANSWER;
     answer->i_status = 301;
 
-    answer->i_body = httpd_HtmlError (&p_body, 301, rdir->psz_dst);
+    answer->i_body = httpd_HtmlError (&p_body, 301, rdir->dst);
     answer->p_body = (unsigned char *)p_body;
 
     /* XXX check if it's ok or we need to set an absolute url */
-    httpd_MsgAdd(answer, "Location",  "%s", rdir->psz_dst);
+    httpd_MsgAdd(answer, "Location",  "%s", rdir->dst);
 
     httpd_MsgAdd(answer, "Content-Length", "%d", answer->i_body);
 
@@ -558,8 +558,10 @@ static int httpd_RedirectCallBack(httpd_callback_sys_t *p_sys,
 httpd_redirect_t *httpd_RedirectNew(httpd_host_t *host, const char *psz_url_dst,
                                      const char *psz_url_src)
 {
-    httpd_redirect_t *rdir = malloc(sizeof(*rdir));
-    if (!rdir)
+    size_t dstlen = strlen(psz_url_dst);
+
+    httpd_redirect_t *rdir = malloc(sizeof(*rdir) + dstlen);
+    if (unlikely(rdir == NULL))
         return NULL;
 
     rdir->url = httpd_UrlNew(host, psz_url_src, NULL, NULL);
@@ -567,7 +569,7 @@ httpd_redirect_t *httpd_RedirectNew(httpd_host_t *host, const char *psz_url_dst,
         free(rdir);
         return NULL;
     }
-    rdir->psz_dst = strdup(psz_url_dst);
+    memcpy(rdir->dst, psz_url_dst, dstlen + 1);
 
     /* Redirect apply for all HTTP request and RTSP DESCRIBE resquest */
     httpd_UrlCatch(rdir->url, HTTPD_MSG_HEAD, httpd_RedirectCallBack,
@@ -584,7 +586,6 @@ httpd_redirect_t *httpd_RedirectNew(httpd_host_t *host, const char *psz_url_dst,
 void httpd_RedirectDelete(httpd_redirect_t *rdir)
 {
     httpd_UrlDelete(rdir->url);
-    free(rdir->psz_dst);
     free(rdir);
 }
 



More information about the vlc-commits mailing list