sink, source: Do not dereference freed memory when freeing the next events
[platform/upstream/pulseaudio.git] / src / pulsecore / source.c
index aabe7ad..b553a2d 100644 (file)
@@ -15,9 +15,7 @@
   General Public License for more details.
 
   You should have received a copy of the GNU Lesser General Public License
-  along with PulseAudio; if not, write to the Free Software
-  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
-  USA.
+  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
 ***/
 
 #ifdef HAVE_CONFIG_H
@@ -117,7 +115,7 @@ void pa_source_new_data_set_muted(pa_source_new_data *data, bool mute) {
     pa_assert(data);
 
     data->muted_is_set = true;
-    data->muted = !!mute;
+    data->muted = mute;
 }
 
 void pa_source_new_data_set_port(pa_source_new_data *data, const char *port) {
@@ -533,7 +531,7 @@ void pa_source_put(pa_source *s) {
     pa_assert_ctl_context();
 
     pa_assert(s->state == PA_SOURCE_INIT);
-    pa_assert(!(s->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER) || s->output_from_master);
+    pa_assert(!(s->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER) || pa_source_is_filter(s));
 
     /* The following fields must be initialized properly when calling _put() */
     pa_assert(s->asyncmsgq);
@@ -564,8 +562,10 @@ void pa_source_put(pa_source *s) {
      * the source implementor to set this flag as needed.
      *
      * Note: This flag can also change over the life time of the source. */
-    if (!(s->flags & PA_SOURCE_HW_VOLUME_CTRL) && !(s->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER))
+    if (!(s->flags & PA_SOURCE_HW_VOLUME_CTRL) && !(s->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)) {
         pa_source_enable_decibel_volume(s, true);
+        s->soft_volume = s->reference_volume;
+    }
 
     /* If the source implementor support DB volumes by itself, we should always
      * try and enable flat volumes too */
@@ -596,7 +596,7 @@ void pa_source_put(pa_source *s) {
               || (s->base_volume == PA_VOLUME_NORM
                   && ((s->flags & PA_SOURCE_DECIBEL_VOLUME || (s->flags & PA_SOURCE_SHARE_VOLUME_WITH_MASTER)))));
     pa_assert(!(s->flags & PA_SOURCE_DECIBEL_VOLUME) || s->n_volume_steps == PA_VOLUME_NORM+1);
-    pa_assert(!(s->flags & PA_SOURCE_DYNAMIC_LATENCY) == (s->thread_info.fixed_latency != 0));
+    pa_assert(!(s->flags & PA_SOURCE_DYNAMIC_LATENCY) == !(s->thread_info.fixed_latency == 0));
 
     if (s->suspend_cause)
         pa_assert_se(source_set_state(s, PA_SOURCE_SUSPENDED) == 0);
@@ -610,7 +610,7 @@ void pa_source_put(pa_source *s) {
 /* Called from main context */
 void pa_source_unlink(pa_source *s) {
     bool linked;
-    pa_source_output *o, *j = NULL;
+    pa_source_output *o, PA_UNUSED *j = NULL;
 
     pa_assert(s);
     pa_assert_ctl_context();
@@ -662,6 +662,8 @@ static void source_free(pa_object *o) {
 
     pa_log_info("Freeing source %u \"%s\"", s->index, s->name);
 
+    pa_source_volume_change_flush(s);
+
     pa_idxset_free(s->outputs, NULL);
     pa_hashmap_free(s->thread_info.outputs);
 
@@ -1171,6 +1173,13 @@ pa_source *pa_source_get_master(pa_source *s) {
 }
 
 /* Called from main context */
+bool pa_source_is_filter(pa_source *s) {
+    pa_source_assert_ref(s);
+
+    return (s->output_from_master != NULL);
+}
+
+/* Called from main context */
 bool pa_source_is_passthrough(pa_source *s) {
 
     pa_source_assert_ref(s);
@@ -2645,6 +2654,7 @@ static void pa_source_volume_change_free(pa_source_volume_change *c) {
 void pa_source_volume_change_push(pa_source *s) {
     pa_source_volume_change *c = NULL;
     pa_source_volume_change *nc = NULL;
+    pa_source_volume_change *pc = NULL;
     uint32_t safety_margin = s->thread_info.volume_change_safety_margin;
 
     const char *direction = NULL;
@@ -2702,7 +2712,7 @@ void pa_source_volume_change_push(pa_source *s) {
     pa_log_debug("Volume going %s to %d at %llu", direction, pa_cvolume_avg(&nc->hw_volume), (long long unsigned) nc->at);
 
     /* We can ignore volume events that came earlier but should happen later than this. */
-    PA_LLIST_FOREACH(c, nc->next) {
+    PA_LLIST_FOREACH_SAFE(c, pc, nc->next) {
         pa_log_debug("Volume change to %d at %llu was dropped", pa_cvolume_avg(&c->hw_volume), (long long unsigned) c->at);
         pa_source_volume_change_free(c);
     }