[vlc-devel] [PATCH 09/12] network: io: Remove VLA usages

Romain Vimont rom1v at videolabs.io
Thu Dec 10 14:18:57 CET 2020


On Thu, Dec 10, 2020 at 01:32:20PM +0100, Thomas Guillem wrote:
> 
> 
> On Thu, Dec 10, 2020, at 12:10, Pierre Ynard via vlc-devel wrote:
> > > Using VLA for this particular part of the code (http_auth) is very
> > > convenient since the size max is bounded, so it can't overflow.
> > > 
> > > Having said that, we are very close to a full support of MSVC. If
> > > we need to fix some well-written code to remove VLA, I'm OK for it.
> > > I'm convinced by Steve's arguments. Supporting MSVC can bring more
> > > developers in VLC.
> > 
> > Can we try using some #ifdef and/or converting VLAs to FLAs, rather than
> > to heap allocation, where the size is reasonably bounded?
> 
> He is a suggestion:
> 
> enum auth_param {
>     AUTH_PARAM_REALM,
>     ...
> };
> 
> #define AUTH_PARAM_LENMAX 10
> 
> static const struct auth_param {
>     enum auth_param,
>     char str[AUTH_PARAM_LENMAX],
> } = {
>      AUTH_PARAM_REALM, "realm",
>      ...
> }
> 
> The max size is known.
> Then add some bsearch to get the str from the id.

For this specific case (patch 7/12), the argument is always a string
literal, so it could be concatenated using a macro:

-------- 8< -----------------------------------------------------------
diff --git a/src/network/http_auth.c b/src/network/http_auth.c
index 599fda6286..cb926fd4d6 100644
--- a/src/network/http_auth.c
+++ b/src/network/http_auth.c
@@ -42,10 +42,9 @@
 /*****************************************************************************
  * "RFC 2617: Basic and Digest Access Authentication" header parsing
  *****************************************************************************/
-static char *AuthGetParam( const char *psz_header, const char *psz_param )
+static char *AuthGetParam( const char *psz_header, const char *psz_what )
+#define AuthGetParam(A, B) AuthGetParam(A, B "=\"")
 {
-    char psz_what[strlen(psz_param)+3];
-    sprintf( psz_what, "%s=\"", psz_param );
     psz_header = strstr( psz_header, psz_what );
     if ( psz_header )
     {
-------- 8< -----------------------------------------------------------

(It's a minimal example, it should be improved for readability.)

Regards


More information about the vlc-devel mailing list