diff mbox

lib: fwts_klog: handle the case where klog_old is empty list

Message ID 1455667300-21365-1-git-send-email-colin.king@canonical.com
State Accepted
Headers show

Commit Message

Colin Ian King Feb. 17, 2016, 12:01 a.m. UTC
From: Colin Ian King <colin.king@canonical.com>

scan-build detected that an empty klog_old list would lead to l_old_last
being null and hence we get a null pointer dereference when assigning
old to the data in the null list. Check for a empty list and ensure
l_new is initialized to NULL for this corner case.

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 src/lib/src/fwts_klog.c | 21 ++++++++++++---------
 1 file changed, 12 insertions(+), 9 deletions(-)

Comments

Alex Hung Feb. 17, 2016, 4:35 a.m. UTC | #1
On 2016-02-17 08:01 AM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> scan-build detected that an empty klog_old list would lead to l_old_last
> being null and hence we get a null pointer dereference when assigning
> old to the data in the null list. Check for a empty list and ensure
> l_new is initialized to NULL for this corner case.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_klog.c | 21 ++++++++++++---------
>   1 file changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
> index 48a0e45..b5f0965 100644
> --- a/src/lib/src/fwts_klog.c
> +++ b/src/lib/src/fwts_klog.c
> @@ -51,7 +51,7 @@ void fwts_klog_free(fwts_list *klog)
>    */
>   fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new)
>   {
> -	fwts_list_link *l_old, *l_new;
> +	fwts_list_link *l_old, *l_new = NULL;
>   	fwts_list *klog_diff;
>
>   	if (klog_new == NULL) {
> @@ -74,14 +74,17 @@ fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new)
>   		fwts_list_foreach(l_old, klog_old)
>   			l_old_last = l_old;
>
> -		/* And now look for that last line in the new log */
> -		old = fwts_list_data(char *, l_old_last);
> -		fwts_list_foreach(l_new, klog_new) {
> -			char *new = fwts_list_data(char *, l_new);
> -			if (!strcmp(new, old)) {
> -				/* Found last line that matches, bump to next */
> -				l_new = l_new->next;
> -				break;
> +		if (l_old_last) {
> +			/* And now look for that last line in the new log */
> +			old = fwts_list_data(char *, l_old_last);
> +			fwts_list_foreach(l_new, klog_new) {
> +				const char *new = fwts_list_data(char *, l_new);
> +
> +				if (!strcmp(new, old)) {
> +					/* Found last line that matches, bump to next */
> +					l_new = l_new->next;
> +					break;
> +				}
>   			}
>   		}
>   	}
>


Acked-by: Alex Hung <alex.hung@canonical.com>
Ivan Hu Feb. 18, 2016, 6:57 a.m. UTC | #2
On 2016年02月17日 08:01, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
>
> scan-build detected that an empty klog_old list would lead to l_old_last
> being null and hence we get a null pointer dereference when assigning
> old to the data in the null list. Check for a empty list and ensure
> l_new is initialized to NULL for this corner case.
>
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   src/lib/src/fwts_klog.c | 21 ++++++++++++---------
>   1 file changed, 12 insertions(+), 9 deletions(-)
>
> diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
> index 48a0e45..b5f0965 100644
> --- a/src/lib/src/fwts_klog.c
> +++ b/src/lib/src/fwts_klog.c
> @@ -51,7 +51,7 @@ void fwts_klog_free(fwts_list *klog)
>    */
>   fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new)
>   {
> -	fwts_list_link *l_old, *l_new;
> +	fwts_list_link *l_old, *l_new = NULL;
>   	fwts_list *klog_diff;
>
>   	if (klog_new == NULL) {
> @@ -74,14 +74,17 @@ fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new)
>   		fwts_list_foreach(l_old, klog_old)
>   			l_old_last = l_old;
>
> -		/* And now look for that last line in the new log */
> -		old = fwts_list_data(char *, l_old_last);
> -		fwts_list_foreach(l_new, klog_new) {
> -			char *new = fwts_list_data(char *, l_new);
> -			if (!strcmp(new, old)) {
> -				/* Found last line that matches, bump to next */
> -				l_new = l_new->next;
> -				break;
> +		if (l_old_last) {
> +			/* And now look for that last line in the new log */
> +			old = fwts_list_data(char *, l_old_last);
> +			fwts_list_foreach(l_new, klog_new) {
> +				const char *new = fwts_list_data(char *, l_new);
> +
> +				if (!strcmp(new, old)) {
> +					/* Found last line that matches, bump to next */
> +					l_new = l_new->next;
> +					break;
> +				}
>   			}
>   		}
>   	}
>

Acked-by: Ivan Hu <ivan.hu@canonical.com>
diff mbox

Patch

diff --git a/src/lib/src/fwts_klog.c b/src/lib/src/fwts_klog.c
index 48a0e45..b5f0965 100644
--- a/src/lib/src/fwts_klog.c
+++ b/src/lib/src/fwts_klog.c
@@ -51,7 +51,7 @@  void fwts_klog_free(fwts_list *klog)
  */
 fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new)
 {
-	fwts_list_link *l_old, *l_new;
+	fwts_list_link *l_old, *l_new = NULL;
 	fwts_list *klog_diff;
 
 	if (klog_new == NULL) {
@@ -74,14 +74,17 @@  fwts_list *fwts_klog_find_changes(fwts_list *klog_old, fwts_list *klog_new)
 		fwts_list_foreach(l_old, klog_old)
 			l_old_last = l_old;
 
-		/* And now look for that last line in the new log */
-		old = fwts_list_data(char *, l_old_last);
-		fwts_list_foreach(l_new, klog_new) {
-			char *new = fwts_list_data(char *, l_new);
-			if (!strcmp(new, old)) {
-				/* Found last line that matches, bump to next */
-				l_new = l_new->next;
-				break;
+		if (l_old_last) {
+			/* And now look for that last line in the new log */
+			old = fwts_list_data(char *, l_old_last);
+			fwts_list_foreach(l_new, klog_new) {
+				const char *new = fwts_list_data(char *, l_new);
+
+				if (!strcmp(new, old)) {
+					/* Found last line that matches, bump to next */
+					l_new = l_new->next;
+					break;
+				}
 			}
 		}
 	}