Bug 1308418 - Avoid double-constructing owned_critical_section via copy constructor. r=jya

This uplifts the upstream change 22557d466eceb6ff6ba70ae30d2dcd87648cde0b from
https://github.com/kinetiknz/cubeb as a standalone patch suitable for uplift.
This commit is contained in:
Matthew Gregan 2016-10-19 14:42:03 +13:00
Родитель cd84eadb22
Коммит b123dc8ba5
6 изменённых файлов: 148 добавлений и 10 удалений

Просмотреть файл

@ -0,0 +1,123 @@
commit 22557d466eceb6ff6ba70ae30d2dcd87648cde0b
Author: Cervantes Yu <cyu@mozilla.com>
Date: Tue Oct 11 10:44:35 2016 +0800
Avoid double-constructing owned_critical_section via copy constructor.
diff --git a/src/cubeb_audiounit.cpp b/src/cubeb_audiounit.cpp
index c8a135f..d2f144b 100644
--- a/src/cubeb_audiounit.cpp
+++ b/src/cubeb_audiounit.cpp
@@ -423,14 +423,13 @@ audiounit_init(cubeb ** context, char const * context_name)
*context = NULL;
- ctx = new cubeb;
+ ctx = (cubeb *)calloc(1, sizeof(cubeb));
assert(ctx);
- PodZero(ctx, 1);
+ // Placement new to call the ctors of cubeb members.
+ new (ctx) cubeb();
ctx->ops = &audiounit_ops;
- ctx->mutex = owned_critical_section();
-
ctx->active_streams = 0;
ctx->limit_streams = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7;
@@ -833,7 +832,8 @@ audiounit_destroy(cubeb * ctx)
audiounit_remove_device_listener(ctx);
}
- delete ctx;
+ ctx->~cubeb();
+ free(ctx);
}
static void audiounit_stream_destroy(cubeb_stream * stm);
@@ -1117,9 +1117,10 @@ audiounit_stream_init(cubeb * context,
}
}
- stm = new cubeb_stream;
+ stm = (cubeb_stream *) calloc(1, sizeof(cubeb_stream));
assert(stm);
- PodZero(stm, 1);
+ // Placement new to call the ctors of cubeb_stream members.
+ new (stm) cubeb_stream();
/* These could be different in the future if we have both
* full-duplex stream and different devices for input vs output. */
@@ -1130,7 +1131,6 @@ audiounit_stream_init(cubeb * context,
stm->state_callback = state_callback;
stm->user_ptr = user_ptr;
stm->device_changed_callback = NULL;
- stm->mutex = owned_critical_section();
/* Init data members where necessary */
stm->hw_latency_frames = UINT64_MAX;
@@ -1443,7 +1443,8 @@ audiounit_stream_destroy(cubeb_stream * stm)
stm->context->active_streams -= 1;
}
- delete stm;
+ stm->~cubeb_stream();
+ free(stm);
}
static int
diff --git a/src/cubeb_utils_unix.h b/src/cubeb_utils_unix.h
index 72041f5..72476ac 100644
--- a/src/cubeb_utils_unix.h
+++ b/src/cubeb_utils_unix.h
@@ -80,6 +80,10 @@ public:
private:
pthread_mutex_t mutex;
+
+ // Disallow copy and assignment because pthread_mutex_t cannot be copied.
+ owned_critical_section(const owned_critical_section&);
+ owned_critical_section& operator=(const owned_critical_section&);
};
#endif /* CUBEB_UTILS_UNIX */
diff --git a/src/cubeb_utils_win.h b/src/cubeb_utils_win.h
index 90e81c1..322ad24 100644
--- a/src/cubeb_utils_win.h
+++ b/src/cubeb_utils_win.h
@@ -62,6 +62,10 @@ private:
#ifdef DEBUG
DWORD owner;
#endif
+
+ // Disallow copy and assignment because CRICICAL_SECTION cannot be copied.
+ owned_critical_section(const owned_critical_section&);
+ owned_critical_section& operator=(const owned_critical_section&);
};
#endif /* CUBEB_UTILS_WIN */
diff --git a/src/cubeb_wasapi.cpp b/src/cubeb_wasapi.cpp
index b2f28d2..d960dfc 100644
--- a/src/cubeb_wasapi.cpp
+++ b/src/cubeb_wasapi.cpp
@@ -1637,7 +1637,8 @@ wasapi_stream_init(cubeb * context, cubeb_stream ** stream,
stm->latency = latency_frames;
stm->volume = 1.0;
- stm->stream_reset_lock = owned_critical_section();
+ // Placement new to call ctor.
+ new (&stm->stream_reset_lock) owned_critical_section();
stm->reconfigure_event = CreateEvent(NULL, 0, 0, NULL);
if (!stm->reconfigure_event) {
@@ -1734,6 +1735,9 @@ void wasapi_stream_destroy(cubeb_stream * stm)
close_wasapi_stream(stm);
}
+ // Need to call dtor to free the resource in owned_critical_section.
+ stm->stream_reset_lock.~owned_critical_section();
+
free(stm);
}

Просмотреть файл

@ -437,14 +437,13 @@ audiounit_init(cubeb ** context, char const * context_name)
*context = NULL;
ctx = new cubeb;
ctx = (cubeb *)calloc(1, sizeof(cubeb));
assert(ctx);
PodZero(ctx, 1);
// Placement new to call the ctors of cubeb members.
new (ctx) cubeb();
ctx->ops = &audiounit_ops;
ctx->mutex = owned_critical_section();
ctx->active_streams = 0;
ctx->limit_streams = kCFCoreFoundationVersionNumber < kCFCoreFoundationVersionNumber10_7;
@ -847,7 +846,8 @@ audiounit_destroy(cubeb * ctx)
audiounit_remove_device_listener(ctx);
}
delete ctx;
ctx->~cubeb();
free(ctx);
}
static void audiounit_stream_destroy(cubeb_stream * stm);
@ -1131,9 +1131,10 @@ audiounit_stream_init(cubeb * context,
}
}
stm = new cubeb_stream;
stm = (cubeb_stream *) calloc(1, sizeof(cubeb_stream));
assert(stm);
PodZero(stm, 1);
// Placement new to call the ctors of cubeb_stream members.
new (stm) cubeb_stream();
/* These could be different in the future if we have both
* full-duplex stream and different devices for input vs output. */
@ -1144,7 +1145,6 @@ audiounit_stream_init(cubeb * context,
stm->state_callback = state_callback;
stm->user_ptr = user_ptr;
stm->device_changed_callback = NULL;
stm->mutex = owned_critical_section();
/* Init data members where necessary */
stm->hw_latency_frames = UINT64_MAX;
@ -1457,7 +1457,8 @@ audiounit_stream_destroy(cubeb_stream * stm)
stm->context->active_streams -= 1;
}
delete stm;
stm->~cubeb_stream();
free(stm);
}
static int

Просмотреть файл

@ -80,6 +80,10 @@ public:
private:
pthread_mutex_t mutex;
// Disallow copy and assignment because pthread_mutex_t cannot be copied.
owned_critical_section(const owned_critical_section&);
owned_critical_section& operator=(const owned_critical_section&);
};
#endif /* CUBEB_UTILS_UNIX */

Просмотреть файл

@ -62,6 +62,10 @@ private:
#ifdef DEBUG
DWORD owner;
#endif
// Disallow copy and assignment because CRICICAL_SECTION cannot be copied.
owned_critical_section(const owned_critical_section&);
owned_critical_section& operator=(const owned_critical_section&);
};
#endif /* CUBEB_UTILS_WIN */

Просмотреть файл

@ -1642,7 +1642,8 @@ wasapi_stream_init(cubeb * context, cubeb_stream ** stream,
stm->latency = latency_frames;
stm->volume = 1.0;
stm->stream_reset_lock = owned_critical_section();
// Placement new to call ctor.
new (&stm->stream_reset_lock) owned_critical_section();
stm->reconfigure_event = CreateEvent(NULL, 0, 0, NULL);
if (!stm->reconfigure_event) {
@ -1739,6 +1740,9 @@ void wasapi_stream_destroy(cubeb_stream * stm)
close_wasapi_stream(stm);
}
// Need to call dtor to free the resource in owned_critical_section.
stm->stream_reset_lock.~owned_critical_section();
free(stm);
}

Просмотреть файл

@ -61,3 +61,5 @@ echo "Applying a patch on top of $version"
patch -p1 < ./wasapi-drift.patch
echo "Applying another patch on top of $version"
patch -p1 < ./wasapi-stereo-mic.patch
echo "Applying another patch on top of $version"
patch -p1 < ./bug1308418-mutex-copy-ctor.patch