From f3a0167872b501c41cd3d3b522cf2455c5e60266 Mon Sep 17 00:00:00 2001 From: Simo Piiroinen Date: Mon, 7 Nov 2016 10:12:28 +0200 Subject: [PATCH] [modules] Clear context pointer after dropping reference Not explicitly initializing the pointer to null value makes the validity of the initial state subject to compiler/libc level implementation details. And not clearing the pointer after dropping the reference can lead to hard to debug issues if the potentially state pointer is used later on. Explicitly initialize the context to null value and set it back to null after dropping the reference. Signed-off-by: Simo Piiroinen --- src/usb_moded-modules.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/usb_moded-modules.c b/src/usb_moded-modules.c index cfb7784..c6afae2 100644 --- a/src/usb_moded-modules.c +++ b/src/usb_moded-modules.c @@ -2,10 +2,12 @@ @file usb_moded-modules.c Copyright (C) 2010 Nokia Corporation. All rights reserved. - Copyright (C) 2012-2015 Jolla. All rights reserved. + Copyright (C) 2012-2016 Jolla. All rights reserved. @author: Philippe De Swert @author: Philippe De Swert + @author: Slava Monich + @author: Simo Piiroinen This program is free software; you can redistribute it and/or modify it under the terms of the Lesser GNU General Public License @@ -43,7 +45,7 @@ /* kmod context - initialized at start in usb_moded_init by ctx_init() and cleaned up by ctx_cleanup() functions */ -struct kmod_ctx *ctx; +static struct kmod_ctx *ctx = 0; /* kmod module init */ void usb_moded_module_ctx_init(void) @@ -55,7 +57,8 @@ void usb_moded_module_ctx_init(void) /* kmod module cleanup */ void usb_moded_module_ctx_cleanup(void) { - kmod_unref(ctx); + if( ctx ) + kmod_unref(ctx), ctx = 0; } /** load module