[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] [PATCH] uri parsing helpers



On Sun, Aug 8, 2010 at 2:29 PM, Mihai Draghicioiu
<mihai.draghicioiu@xxxxxxxxx> wrote:
> I suggest either allocating the uri struct in memory or not calling
> evhttp_uri_clear at the top of evhttp_uri_parse. When declaring the
> uri variable inside a function, it will be uninitialized, so in order
> to avoid a crash from evhttp_uri_clear, one has to memset the struct
> to zero. So I suggest setting all members to zero in evhttp_uri_parse,
> at the top, and if the user wants to reuse the uri variable, they can
> call evhttp_uri_clear later on.
>
> Example:
>
> void foo() {
>    evhttp_uri uri;
>    evhttp_uri_parse("http://foo";, &uri); // causes crash
> }
>
> void bar() {
>    evhttp_uri uri;
>    memset(&uri, 0, sizeof(uri));
>    evhttp_uri_parse("http://bar";, &uri); // no crash
> }
>
> My suggestion:
> evhttp_uri *uri = evhttp_uri_parse("http://baz";);
> // do stuff, then:
> evhttp_uri_free(uri);
>
> Also, the function could return int (0 for success, -1 for failure),
> because not all uris can be parsed properly. There are a few return
> cases in it, and those can return -1.

Mihai, thanks for your review!

I had doubts about memory allocation for uri or not. For my
application and in http_parse_uri_test there static zero's filling is
used:
+	struct evhttp_uri uri = { 0 };

Now I see, that my patch breaks existing conventions, like used for
event or bufferevent structures.

evhttp_uri_parse() from patch v2 [1] is using posix return value
convention. Should it use more strict parsing ? It returns -1 only in
cases:
 1) cannot duplicate memory for temporary buffer
 2) string doesn't contains "://" token - evhttp_uri_parse() cannot
parse URIs like "mailto:foo@bar";.

I will alter the patch: add evhttp_uri_free(), and suppose there is no
more need for evhttp_uri_clear().


[1] https://sourceforge.net/tracker/?func=detail&aid=3037660&group_id=50884&atid=461324

-- 
Pavel Plesov
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.