In the Windows getopt_internal function the condition freeing
the memory allocated by _dupenv_s is correct only for the first
call to the function.

the next callers will try to free the buffer even though the _dupenv_s
call is skipped if the POSIXLY_CORRECT env isn't found (undefined behavior).

Fixed by releasing the buffer in the scope of the same if statement calling 
_dupenv_s

Fixes: 5e373e456e6acdc ("eal/windows: add getopt implementation")
Cc: sta...@dpdk.org

Signed-off-by: Tal Shnaiderman <tal...@nvidia.com>
---
 lib/librte_eal/windows/getopt.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/lib/librte_eal/windows/getopt.c b/lib/librte_eal/windows/getopt.c
index a08f7c109b..20da225a68 100644
--- a/lib/librte_eal/windows/getopt.c
+++ b/lib/librte_eal/windows/getopt.c
@@ -253,16 +253,17 @@ getopt_internal(int nargc, char **nargv, const char 
*options,
         * Disable GNU extensions if POSIXLY_CORRECT is set or options
         * string begins with a '+'.
         */
-       if (posixly_correct == -1)
+       if (posixly_correct == -1) {
                posixly_correct = _dupenv_s(&buf, &len, "POSIXLY_CORRECT");
+               if (!posixly_correct)
+                       free(buf);
+       }
        if (!posixly_correct || *options == '+')
                flags &= ~FLAG_PERMUTE;
        else if (*options == '-')
                flags |= FLAG_ALLARGS;
        if (*options == '+' || *options == '-')
                options++;
-       if (!posixly_correct)
-               free(buf);
        /*
         * reset if requested
         */
-- 
2.16.1.windows.4

Reply via email to