Fix segfault when there's no config dir

Submitted by Paulo Zanoni on Nov. 28, 2011, 4:09 p.m.

Details

Message ID 1322496580-22867-1-git-send-email-przanoni@gmail.com
State New
Headers show

Not browsing as part of any series.

Commit Message

Paulo Zanoni Nov. 28, 2011, 4:09 p.m.
From: Paulo Zanoni <paulo.r.zanoni@intel.com>

Also, call AddConfigDirFiles only if we found a file, protecting
ourselves from future changes to the AddConfigDirFiles function.

Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
---
 hw/xfree86/parser/scan.c |    9 ++++++---
 1 files changed, 6 insertions(+), 3 deletions(-)

Patch hide | download patch | download mbox

diff --git a/hw/xfree86/parser/scan.c b/hw/xfree86/parser/scan.c
index 9099227..54fa474 100644
--- a/hw/xfree86/parser/scan.c
+++ b/hw/xfree86/parser/scan.c
@@ -852,13 +852,16 @@  OpenConfigDir(const char *path, const char *cmdline, const char *projroot,
 
 		/* match files named *.conf */
 		num = scandir(dirpath, &list, ConfigFilter, alphasort);
-		found = AddConfigDirFiles(dirpath, list, num);
+		if (num > 0)
+			found = AddConfigDirFiles(dirpath, list, num);
+		else
+			found = FALSE;
 		if (!found) {
 			free(dirpath);
 			dirpath = NULL;
 		}
-		while (num--)
-			free(list[num]);
+		while (num > 0)
+			free(list[--num]);
 		free(list);
 	}
 

Comments

Am 28.11.2011 17:09, schrieb przanoni@gmail.com:
> From: Paulo Zanoni <paulo.r.zanoni@intel.com>
> 
> Also, call AddConfigDirFiles only if we found a file, protecting
> ourselves from future changes to the AddConfigDirFiles function.
> 
> Signed-off-by: Paulo Zanoni <paulo.r.zanoni@intel.com>
> ---
>  hw/xfree86/parser/scan.c |    9 ++++++---
>  1 files changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/hw/xfree86/parser/scan.c b/hw/xfree86/parser/scan.c
> index 9099227..54fa474 100644
> --- a/hw/xfree86/parser/scan.c
> +++ b/hw/xfree86/parser/scan.c
> @@ -852,13 +852,16 @@ OpenConfigDir(const char *path, const char *cmdline, const char *projroot,
>  
>  		/* match files named *.conf */
>  		num = scandir(dirpath, &list, ConfigFilter, alphasort);
> -		found = AddConfigDirFiles(dirpath, list, num);
> +		if (num > 0)
> +			found = AddConfigDirFiles(dirpath, list, num);
> +		else
> +			found = FALSE;

looks ok, perhaps it is the better strategy to make AddConfigDirFiles() more robust by checking num ?

re,
 wh


>  		if (!found) {
>  			free(dirpath);
>  			dirpath = NULL;
>  		}
> -		while (num--)
> -			free(list[num]);
> +		while (num > 0)
> +			free(list[--num]);
>  		free(list);
>  	}
>