Skip to contentSkip to conversation

scan-build fixes

PATCH Applied Alex Henrie 3 participants 24 messages Dec 26, 2020 – Dec 28, 2020

lore
Share & tools
lore
#0 lore raw

cover scan-build fixes

These defects were identified by scan-build
<https://clang-analyzer.llvm.org/scan-build.html>.

I hope that drawing attention to the problems helps even if you decide to fix some of them in different ways than I am proposing.

Happy holidays!

-Alex

Alex Henrie (9):
conf: fix use after free in _snd_config_load_with_include
ucm: fix bad frees in get_list0 and get_list20
rawmidi: fix memory leak in snd_rawmidi_virtual_open
confmisc: fix memory leak in snd_func_concat
timer: fix sizeof operator mismatch in snd_timer_query_hw_open
pcm: remove dead assignments from
snd_pcm_rate_(commit_area|grab_next_period)
pcm_multi: remove dead assignment from _snd_pcm_multi_open
pcm: fix undefined bit shift in bad_pcm_state
conf: remove dead code from get_hexachar

 include/pcm.h              |  4 +++-
 src/conf.c                 | 13 ++++---------
 src/confmisc.c             |  2 +-
 src/pcm/pcm.c              |  2 ++
 src/pcm/pcm_local.h        |  2 +-
 src/pcm/pcm_multi.c        |  1 -
 src/pcm/pcm_rate.c         |  2 --
 src/rawmidi/rawmidi_virt.c |  3 ++-
 src/timer/timer_query_hw.c |  2 +-
 src/ucm/main.c             |  4 ++--
 10 files changed, 16 insertions(+), 19 deletions(-)
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#1 lore raw

PATCH 1/9 conf: fix use after free in _snd_config_load_with_include

---
 src/conf.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
src/conf.c +3 -1
index 7df2b4e7..44d1bfde 100644
@@ -1970,7 +1970,9 @@ int _snd_config_load_with_include(snd_config_t *config, snd_input_t *in,
 		SNDERR("%s:%d:%d:%s", fd->name ? fd->name : "_toplevel_", fd->line, fd->column, str);
 		goto _end;
 	}
-	if (get_char(&input) != LOCAL_UNEXPECTED_EOF) {
+	err = get_char(&input);
+	fd = input.current;
+	if (err != LOCAL_UNEXPECTED_EOF) {
 		SNDERR("%s:%d:%d:Unexpected }", fd->name ? fd->name : "", fd->line, fd->column);
 		err = -EINVAL;
 		goto _end;
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#3 lore raw

PATCH 2/9 ucm: fix bad frees in get_list0 and get_list20

In reply to Alex Henrie
---
 src/ucm/main.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/ucm/main.c +2 -2
index 3871d5aa..754b967e 100644
@@ -666,7 +666,7 @@ static int get_list0(struct list_head *list,
 	}
 	return cnt;
       __fail:
-        snd_use_case_free_list((const char **)res, cnt);
+        snd_use_case_free_list(*result, cnt);
         return -ENOMEM;
 }
 
@@ -724,7 +724,7 @@ static int get_list20(struct list_head *list,
 	}
 	return cnt;
       __fail:
-        snd_use_case_free_list((const char **)res, cnt);
+        snd_use_case_free_list(*result, cnt);
         return -ENOMEM;
 }
 
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#5 lore raw

PATCH 3/9 rawmidi: fix memory leak in snd_rawmidi_virtual_open

In reply to Alex Henrie
---
 src/rawmidi/rawmidi_virt.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
src/rawmidi/rawmidi_virt.c +2 -1
index 2c4c27f5..884b8ff8 100644
@@ -315,7 +315,7 @@ int snd_rawmidi_virtual_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
 			     int merge, int mode)
 {
 	int err;
-	snd_rawmidi_t *rmidi;
+	snd_rawmidi_t *rmidi = NULL;
 	snd_rawmidi_virtual_t *virt = NULL;
 	struct pollfd pfd;
 
@@ -392,6 +392,7 @@ int snd_rawmidi_virtual_open(snd_rawmidi_t **inputp, snd_rawmidi_t **outputp,
 		free(*inputp);
 	if (outputp)
 		free(*outputp);
+	free(rmidi);
 	return err;
 }
 
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#7 lore raw

PATCH 4/9 confmisc: fix memory leak in snd_func_concat

In reply to Alex Henrie
---
 src/confmisc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/confmisc.c +1 -1
index eb8218c1..aece39c3 100644
@@ -440,8 +440,8 @@ int snd_func_concat(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
 	err = snd_config_get_id(src, &id);
 	if (err >= 0)
 		err = snd_config_imake_string(dst, id, res);
-	free(res);
       __error:
+	free(res);
 	return err;
 }
 #ifndef DOC_HIDDEN
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#8 lore raw

Re: [PATCH 4/9] confmisc: fix memory leak in snd_func_concat

Quoted 11 lines · Alex Henrie
---
 src/confmisc.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/confmisc.c +0 -0
index eb8218c1..aece39c3 100644

src/confmisc.c int snd_func_concat(snd_config_t **dst, snd_config_t *root, snd_config_t *src, · line 447

Quoted 9 lines
src/confmisc.c +1 -1
@@ -440,8 +440,8 @@ int snd_func_concat(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
 	err = snd_config_get_id(src, &id);
 	if (err >= 0)
 		err = snd_config_imake_string(dst, id, res);
-	free(res);
       __error:
+	free(res);
 	return err;
 }
 #ifndef DOC_HIDDEN

I guess this would lead to the double-free at the error path after realloc() that already freed res before the goto line. Care to fix it and resubmit this one?

thanks,

Takashi

#9 lore raw

Re: [PATCH 4/9] confmisc: fix memory leak in snd_func_concat

Quoted 4 lines · Takashi Iwai

I guess this would lead to the double-free at the error path after realloc() that already freed res before the goto line. Care to fix it and resubmit this one?

Thank you for catching that! I just sent a v2 of this patch.

-Alex

#10 lore raw

PATCH 5/9 timer: fix sizeof operator mismatch in snd_timer_query_hw_open

In reply to Alex Henrie
---
 src/timer/timer_query_hw.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
src/timer/timer_query_hw.c +1 -1
index dad228c8..d8bac6e7 100644
@@ -104,7 +104,7 @@ int snd_timer_query_hw_open(snd_timer_query_t **handle, const char *name, int mo
 		close(fd);
 		return -SND_ERROR_INCOMPATIBLE_VERSION;
 	}
-	tmr = (snd_timer_query_t *) calloc(1, sizeof(snd_timer_t));
+	tmr = (snd_timer_query_t *) calloc(1, sizeof(snd_timer_query_t));
 	if (tmr == NULL) {
 		close(fd);
 		return -ENOMEM;
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#12 lore raw

PATCH 6/9 pcm: remove dead assignments from snd_pcm_rate_(commit_area|grab_next_period)

In reply to Alex Henrie
---
 src/pcm/pcm_rate.c | 2 --
 1 file changed, 2 deletions(-)
src/pcm/pcm_rate.c +0 -2
index 5bf7dbb9..dc38e95e 100644
@@ -746,7 +746,6 @@ static int snd_pcm_rate_commit_area(snd_pcm_t *pcm, snd_pcm_rate_t *rate,
 		if (result < 0)
 			return result;
 	      __partial:
-		xfer = 0;
 		cont = slave_frames;
 		if (cont > slave_size)
 			cont = slave_size;
@@ -846,7 +845,6 @@ static int snd_pcm_rate_grab_next_period(snd_pcm_t *pcm, snd_pcm_uframes_t hw_of
 		if (result < 0)
 			return result;
 	      __partial:
-		xfer = 0;
 		cont = slave_frames;
 		if (cont > rate->gen.slave->period_size)
 			cont = rate->gen.slave->period_size;
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#14 lore raw

PATCH 7/9 pcm_multi: remove dead assignment from _snd_pcm_multi_open

In reply to Alex Henrie
---
 src/pcm/pcm_multi.c | 1 -
 1 file changed, 1 deletion(-)
src/pcm/pcm_multi.c +0 -1
index 53c414d5..5fa09b9b 100644
@@ -1323,7 +1323,6 @@ int _snd_pcm_multi_open(snd_pcm_t **pcmp, const char *name,
 		err = -ENOMEM;
 		goto _free;
 	}
-	idx = 0;
 	for (idx = 0; idx < channels_count; ++idx)
 		channels_sidx[idx] = -1;
 	idx = 0;
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#16 lore raw

PATCH 8/9 pcm: fix undefined bit shift in bad_pcm_state

In reply to Alex Henrie
---
 include/pcm.h       | 4 +++-
 src/pcm/pcm.c       | 2 ++
 src/pcm/pcm_local.h | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
include/pcm.h +3 -1
index e300b951..c6c5d8f8 100644
@@ -307,7 +307,9 @@ typedef enum _snd_pcm_state {
 	SND_PCM_STATE_SUSPENDED,
 	/** Hardware is disconnected */
 	SND_PCM_STATE_DISCONNECTED,
-	SND_PCM_STATE_LAST = SND_PCM_STATE_DISCONNECTED,
+	/** State cannot be queried */
+	SND_PCM_STATE_UNKNOWN,
+	SND_PCM_STATE_LAST = SND_PCM_STATE_UNKNOWN,
 	/** Private - used internally in the library - do not use*/
 	SND_PCM_STATE_PRIVATE1 = 1024
 } snd_pcm_state_t;
src/pcm/pcm.c +2 -0
index 24030b31..5fafc2a0 100644
@@ -680,6 +680,8 @@ static int pcm_state_to_error(snd_pcm_state_t state)
 		return -ESTRPIPE;
 	case SND_PCM_STATE_DISCONNECTED:
 		return -ENODEV;
+	case SND_PCM_STATE_UNKNOWN:
+		return -ENOSYS;
 	default:
 		return 0;
 	}
src/pcm/pcm_local.h +1 -1
index fe77e50d..04f89623 100644
@@ -444,7 +444,7 @@ static inline int __snd_pcm_start(snd_pcm_t *pcm)
 static inline snd_pcm_state_t __snd_pcm_state(snd_pcm_t *pcm)
 {
 	if (!pcm->fast_ops->state)
-		return -ENOSYS;
+		return SND_PCM_STATE_UNKNOWN;
 	return pcm->fast_ops->state(pcm->fast_op_arg);
 }
 
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#17 lore raw

Re: [PATCH 8/9] pcm: fix undefined bit shift in bad_pcm_state

Quoted 13 lines · Alex Henrie
---
 include/pcm.h       | 4 +++-
 src/pcm/pcm.c       | 2 ++
 src/pcm/pcm_local.h | 2 +-
 3 files changed, 6 insertions(+), 2 deletions(-)
include/pcm.h +0 -0
index e300b951..c6c5d8f8 100644

include/pcm.h typedef enum _snd_pcm_state { · line 315

Quoted 10 lines
include/pcm.h +3 -1
@@ -307,7 +307,9 @@ typedef enum _snd_pcm_state {
 	SND_PCM_STATE_SUSPENDED,
 	/** Hardware is disconnected */
 	SND_PCM_STATE_DISCONNECTED,
-	SND_PCM_STATE_LAST = SND_PCM_STATE_DISCONNECTED,
+	/** State cannot be queried */
+	SND_PCM_STATE_UNKNOWN,
+	SND_PCM_STATE_LAST = SND_PCM_STATE_UNKNOWN,
 	/** Private - used internally in the library - do not use*/
 	SND_PCM_STATE_PRIVATE1 = 1024
 } snd_pcm_state_t;

We can't add a random new state here. If any, such a thing has to be defined locally, but this would also break ABI. So, I don't think adding a new state is the right fix.

Quoted 18 lines
src/pcm/pcm.c +2 -0
index 24030b31..5fafc2a0 100644
@@ -680,6 +680,8 @@ static int pcm_state_to_error(snd_pcm_state_t state)
 		return -ESTRPIPE;
 	case SND_PCM_STATE_DISCONNECTED:
 		return -ENODEV;
+	case SND_PCM_STATE_UNKNOWN:
+		return -ENOSYS;
 	default:
 		return 0;
 	}
src/pcm/pcm_local.h +0 -0
index fe77e50d..04f89623 100644

src/pcm/pcm_local.h static inline int __snd_pcm_start(snd_pcm_t *pcm) · line 448

Quoted 6 lines
src/pcm/pcm_local.h +1 -1
@@ -444,7 +444,7 @@ static inline int __snd_pcm_start(snd_pcm_t *pcm)
 static inline snd_pcm_state_t __snd_pcm_state(snd_pcm_t *pcm)
 {
 	if (!pcm->fast_ops->state)
-		return -ENOSYS;
+		return SND_PCM_STATE_UNKNOWN;
 	return pcm->fast_ops->state(pcm->fast_op_arg);

We need either to handle a special error value in all places calling __snd_pcm_state() or to just return SND_PCM_STATE_XRUN or such instead here, IMO.

thanks,

Takashi

#18 lore raw

Re: [PATCH 8/9] pcm: fix undefined bit shift in bad_pcm_state

Dne 27. 12. 20 v 9:34 Takashi Iwai napsal(a):

Quoted 3 lines

We need either to handle a special error value in all places calling __snd_pcm_state() or to just return SND_PCM_STATE_XRUN or such instead here, IMO.

I think that SND_PCM_STATE_OPEN is more appropriate here. If the state callback is not defined, the state management is screwed anyway. The other functions will return an error (because they depend on the state management), so it's safe. I applied this change to repo.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#19 lore raw

Re: [PATCH 8/9] pcm: fix undefined bit shift in bad_pcm_state

Quoted 10 lines · Jaroslav Kysela

Dne 27. 12. 20 v 9:34 Takashi Iwai napsal(a):

Quoted 3 lines

We need either to handle a special error value in all places calling __snd_pcm_state() or to just return SND_PCM_STATE_XRUN or such instead here, IMO.

I think that SND_PCM_STATE_OPEN is more appropriate here. If the state callback is not defined, the state management is screwed anyway. The other functions will return an error (because they depend on the state management), so it's safe. I applied this change to repo.

Thank you for fixing this properly!

-Alex

#20 lore raw

PATCH 9/9 conf: remove dead code from get_hexachar

In reply to Alex Henrie
---
 src/conf.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
src/conf.c +1 -8
index 44d1bfde..970ad643 100644
@@ -877,16 +877,9 @@ static int get_nonwhite(input_t *input)
 
 static inline int get_hexachar(input_t *input)
 {
-	int c, num = 0;
-
+	int c;
 	c = get_char(input);
-	if (c >= '0' && c <= '9') num |= (c - '0') << 4;
-	else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 4;
-	else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 4;
 	c = get_char(input);
-	if (c >= '0' && c <= '9') num |= (c - '0') << 0;
-	else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 0;
-	else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 0;
 	return c;
 }
 
Signature · 1 line
2.29.2.368.ge46b91665e.dirty
#21 lore raw

Re: [PATCH 9/9] conf: remove dead code from get_hexachar

Quoted 16 lines · Alex Henrie
---
 src/conf.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
src/conf.c +0 -2
index 44d1bfde..970ad643 100644
@@ -877,16 +877,9 @@ static int get_nonwhite(input_t *input)
 
 static inline int get_hexachar(input_t *input)
 {
-	int c, num = 0;
-

src/conf.c static int get_nonwhite(input_t *input) · line 883

Quoted 10 lines
src/conf.c +1 -6
+	int c;
 	c = get_char(input);
-	if (c >= '0' && c <= '9') num |= (c - '0') << 4;
-	else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 4;
-	else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 4;
 	c = get_char(input);
-	if (c >= '0' && c <= '9') num |= (c - '0') << 0;
-	else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 0;
-	else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 0;
 	return c;

The current code is obviously wrong and the suggested fix goes even to a wronger direction :) The function should return num instead.

I wonder how this did't hit any problem, so far. Maybe 0x prefix was rarely used, fortunately.

thanks,

Takashi

#22 lore raw

Re: [PATCH 9/9] conf: remove dead code from get_hexachar

Dne 27. 12. 20 v 9:37 Takashi Iwai napsal(a):

Quoted 32 lines
Quoted 16 lines · Alex Henrie
---
 src/conf.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
src/conf.c +0 -2
index 44d1bfde..970ad643 100644
@@ -877,16 +877,9 @@ static int get_nonwhite(input_t *input)
 
 static inline int get_hexachar(input_t *input)
 {
-	int c, num = 0;
-

src/conf.c static int get_nonwhite(input_t *input) · line 883

Quoted 10 lines
src/conf.c +1 -6
+	int c;
 	c = get_char(input);
-	if (c >= '0' && c <= '9') num |= (c - '0') << 4;
-	else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 4;
-	else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 4;
 	c = get_char(input);
-	if (c >= '0' && c <= '9') num |= (c - '0') << 0;
-	else if (c >= 'a' && c <= 'f') num |= (c - 'a') << 0;
-	else if (c >= 'A' && c <= 'F') num |= (c - 'A') << 0;
 	return c;

The current code is obviously wrong and the suggested fix goes even to a wronger direction :) The function should return num instead.

Quoted 2 lines

I wonder how this did't hit any problem, so far. Maybe 0x prefix was rarely used, fortunately.

It's a bit recent code. I fixed the return value now. It's for \xFF not for 0xFF prefix. Thank you for your investigation, Alex.

Jaroslav

Signature · 2 lines
Jaroslav Kysela <[email protected]>
Linux Sound Maintainer; ALSA Project; Red Hat, Inc.
#23 lore raw

Re: [PATCH 9/9] conf: remove dead code from get_hexachar

Quoted 11 lines · Jaroslav Kysela

Dne 27. 12. 20 v 9:37 Takashi Iwai napsal(a):

Quoted 6 lines

The current code is obviously wrong and the suggested fix goes even to a wronger direction :) The function should return num instead.

I wonder how this did't hit any problem, so far. Maybe 0x prefix was rarely used, fortunately.

It's a bit recent code. I fixed the return value now. It's for \xFF not for 0xFF prefix. Thank you for your investigation, Alex.

Thank you for fixing this properly!

-Alex