diff mbox

[LEDE-DEV,mountd,2/4] fix crash if no uci config file present

Message ID 1465999824-20936-2-git-send-email-olivier.hardouin@gmail.com
State Accepted
Headers show

Commit Message

olivier.hardouin@gmail.com June 15, 2016, 2:10 p.m. UTC
fix also possible null dereferenced pointers 

Signed-off-by: Olivier Hardouin <olivier.hardouin@gmail.com>
---
 uci.c  | 6 ++++++
 ucix.c | 9 ++++++++-
 2 files changed, 14 insertions(+), 1 deletion(-)

Comments

Conor O'Gorman June 15, 2016, 4:07 p.m. UTC | #1
On 15/06/16 15:10, olivier.hardouin@gmail.com wrote:
> fix also possible null dereferenced pointers
>
> Signed-off-by: Olivier Hardouin <olivier.hardouin@gmail.com>
> ---
>   uci.c  | 6 ++++++
>   ucix.c | 9 ++++++++-
>   2 files changed, 14 insertions(+), 1 deletion(-)
>
The code design appears to be that uci_init() is called before any 
others. It initialises p.
I would argue for a runtime warning and fail rather than a silent return 
for these errors?
John Crispin June 15, 2016, 4:58 p.m. UTC | #2
On 15/06/2016 18:07, Conor O'Gorman wrote:
> On 15/06/16 15:10, olivier.hardouin@gmail.com wrote:
>> fix also possible null dereferenced pointers
>>
>> Signed-off-by: Olivier Hardouin <olivier.hardouin@gmail.com>
>> ---
>>   uci.c  | 6 ++++++
>>   ucix.c | 9 ++++++++-
>>   2 files changed, 14 insertions(+), 1 deletion(-)
>>
> The code design appears to be that uci_init() is called before any
> others. It initialises p.
> I would argue for a runtime warning and fail rather than a silent return
> for these errors?
> 

not worth the effort, i plan to rewrite this as part of the block tool
and nuke mountd.

	John
diff mbox

Patch

diff --git a/uci.c b/uci.c
index f5aad02..46c3922 100644
--- a/uci.c
+++ b/uci.c
@@ -54,6 +54,9 @@  char* uci_get_option(struct uci_context *ctx, char *section, char *option)
 	char *value = NULL;
 	struct uci_ptr ptr;
 
+	if (!p)
+		return NULL;
+
 	memset(&ptr, 0, sizeof(ptr));
 	ptr.package = p->e.name;
 	ptr.section = section;
@@ -101,6 +104,9 @@  void uci_for_each_section_type(char *type, void (*cb)(char*, void*), void *priv)
 {
 	struct uci_element *e;
 
+	if (!p)
+		return;
+
 	uci_foreach_element(&p->sections, e)
 		if (!strcmp(type, uci_to_section(e)->type))
 			cb(e->name, priv);
diff --git a/ucix.c b/ucix.c
index e2a6780..1e4d1e6 100644
--- a/ucix.c
+++ b/ucix.c
@@ -18,6 +18,8 @@  static inline int ucix_get_ptr(struct uci_context *ctx, const char *p, const cha
 struct uci_context* ucix_init(const char *config_file)
 {
 	struct uci_context *ctx = uci_alloc_context();
+	if(!ctx)
+		return NULL;
 	uci_add_delta_path(ctx, "/var/state");
 	if(uci_load(ctx, config_file, NULL) != UCI_OK)
 	{
@@ -30,6 +32,8 @@  struct uci_context* ucix_init(const char *config_file)
 struct uci_context* ucix_init_path(const char *path, const char *config_file)
 {
 	struct uci_context *ctx = uci_alloc_context();
+	if(!ctx)
+		return NULL;
 	if(path)
 	{
 		uci_set_savedir(ctx, path);
@@ -44,7 +48,10 @@  struct uci_context* ucix_init_path(const char *path, const char *config_file)
 
 void ucix_cleanup(struct uci_context *ctx)
 {
-	uci_free_context(ctx);
+	if(ctx)
+	{
+		uci_free_context(ctx);
+	}
 }
 
 int ucix_save(struct uci_context *ctx, const char *p)