Merge lp:~thomas-voss/platform-api/fix-1338610 into lp:platform-api

Proposed by Thomas Voß
Status: Merged
Approved by: Charles Kerr
Approved revision: 247
Merged at revision: 250
Proposed branch: lp:~thomas-voss/platform-api/fix-1338610
Merge into: lp:platform-api
Diff against target: 375 lines (+159/-49)
4 files modified
src/ubuntu/application/common/application/location/controller.cpp (+84/-29)
src/ubuntu/application/common/application/location/instance.h (+9/-3)
src/ubuntu/application/common/application/location/service.cpp (+30/-5)
src/ubuntu/application/common/application/location/session.cpp (+36/-12)
To merge this branch: bzr merge lp:~thomas-voss/platform-api/fix-1338610
Reviewer Review Type Date Requested Status
Charles Kerr (community) Approve
PS Jenkins bot continuous-integration Approve
Florian Boucault Pending
Review via email: mp+228567@code.launchpad.net

Commit message

Check for null in functions accessing a session object.
  Make sure that exceptions cannot propagate for controller interactions.

Description of the change

Check for null in functions accessing a session object.
Make sure that exceptions cannot propagate for controller interactions.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Charles Kerr (charlesk) wrote :

I'd forgotten that there are alphabetical equivalents of the bitwise operators, so "if (not session)" sent me reaching for my C++ book... :)

Anyway, the code looks straightforward and reasonable, nice to see e.what() logged when available.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/ubuntu/application/common/application/location/controller.cpp'
2--- src/ubuntu/application/common/application/location/controller.cpp 2014-06-23 13:09:01 +0000
3+++ src/ubuntu/application/common/application/location/controller.cpp 2014-07-28 20:16:38 +0000
4@@ -55,19 +55,30 @@
5
6 *out_flags = 0;
7
8- auto service = Instance::instance().get_service();
9-
10- if (service->is_online().get())
11- *out_flags |= UA_LOCATION_SERVICE_ENABLED;
12- else
13- *out_flags |= UA_LOCATION_SERVICE_DISABLED;
14-
15- if (service->does_satellite_based_positioning().get())
16- *out_flags |= UA_LOCATION_SERVICE_GPS_ENABLED;
17- else
18- *out_flags |= UA_LOCATION_SERVICE_GPS_DISABLED;
19-
20- return U_STATUS_SUCCESS;
21+ try
22+ {
23+ auto service = Instance::instance().get_service();
24+
25+ if (service->is_online().get())
26+ *out_flags |= UA_LOCATION_SERVICE_ENABLED;
27+ else
28+ *out_flags |= UA_LOCATION_SERVICE_DISABLED;
29+
30+ if (service->does_satellite_based_positioning().get())
31+ *out_flags |= UA_LOCATION_SERVICE_GPS_ENABLED;
32+ else
33+ *out_flags |= UA_LOCATION_SERVICE_GPS_DISABLED;
34+
35+ return U_STATUS_SUCCESS;
36+ } catch(const std::exception& e)
37+ {
38+ std::cerr << "ua_location_service_controller_query_status: error accessing instance: " << e.what() << std::endl;
39+ } catch(...)
40+ {
41+ std::cerr << "ua_location_service_controller_query_status: error accessing instance." << std::endl;
42+ }
43+
44+ return U_STATUS_ERROR;
45 }
46
47 UStatus
48@@ -76,10 +87,21 @@
49 {
50 (void) controller;
51
52- auto service = Instance::instance().get_service();
53- service->is_online().set(true);
54-
55- return U_STATUS_SUCCESS;
56+ try
57+ {
58+ auto service = Instance::instance().get_service();
59+ service->is_online().set(true);
60+
61+ return U_STATUS_SUCCESS;
62+ } catch(const std::exception& e)
63+ {
64+ std::cerr << "ua_location_service_controller_enable_service: error accessing instance: " << e.what() << std::endl;
65+ } catch(...)
66+ {
67+ std::cerr << "ua_location_service_controller_enable_service: error accessing instance." << std::endl;
68+ }
69+
70+ return U_STATUS_ERROR;
71 }
72
73 UStatus
74@@ -88,10 +110,21 @@
75 {
76 (void) controller;
77
78- auto service = Instance::instance().get_service();
79- service->is_online().set(false);
80-
81- return U_STATUS_SUCCESS;
82+ try
83+ {
84+ auto service = Instance::instance().get_service();
85+ service->is_online().set(false);
86+
87+ return U_STATUS_SUCCESS;
88+ } catch(const std::exception& e)
89+ {
90+ std::cerr << "ua_location_service_controller_disable_service: error accessing instance: " << e.what() << std::endl;
91+ } catch(...)
92+ {
93+ std::cerr << "ua_location_service_controller_disable_service: error accessing instance." << std::endl;
94+ }
95+
96+ return U_STATUS_ERROR;
97 }
98
99 UStatus
100@@ -100,10 +133,21 @@
101 {
102 (void) controller;
103
104- auto service = Instance::instance().get_service();
105- service->does_satellite_based_positioning().set(true);
106-
107- return U_STATUS_SUCCESS;
108+ try
109+ {
110+ auto service = Instance::instance().get_service();
111+ service->does_satellite_based_positioning().set(true);
112+
113+ return U_STATUS_SUCCESS;
114+ } catch(const std::exception& e)
115+ {
116+ std::cerr << "ua_location_service_controller_enable_gps: error accessing instance: " << e.what() << std::endl;
117+ } catch(...)
118+ {
119+ std::cerr << "ua_location_service_controller_enable_gps: error accessing instance." << std::endl;
120+ }
121+
122+ return U_STATUS_ERROR;
123 }
124
125 UStatus
126@@ -112,8 +156,19 @@
127 {
128 (void) controller;
129
130- auto service = Instance::instance().get_service();
131- service->does_satellite_based_positioning().set(false);
132-
133- return U_STATUS_SUCCESS;
134+ try
135+ {
136+ auto service = Instance::instance().get_service();
137+ service->does_satellite_based_positioning().set(false);
138+
139+ return U_STATUS_SUCCESS;
140+ } catch(const std::exception& e)
141+ {
142+ std::cerr << "ua_location_service_controller_disable_gps: error accessing instance: " << e.what() << std::endl;
143+ } catch(...)
144+ {
145+ std::cerr << "ua_location_service_controller_disable_gps: error accessing instance." << std::endl;
146+ }
147+
148+ return U_STATUS_ERROR;
149 }
150
151=== modified file 'src/ubuntu/application/common/application/location/instance.h'
152--- src/ubuntu/application/common/application/location/instance.h 2014-06-23 11:17:42 +0000
153+++ src/ubuntu/application/common/application/location/instance.h 2014-07-28 20:16:38 +0000
154@@ -56,10 +56,16 @@
155
156 ~Instance() noexcept
157 {
158- bus->stop();
159+ try
160+ {
161+ bus->stop();
162
163- if (worker.joinable())
164- worker.join();
165+ if (worker.joinable())
166+ worker.join();
167+ } catch(...)
168+ {
169+ // We silently ignore errors to fulfill our noexcept guarantee.
170+ }
171 }
172
173 core::dbus::Bus::Ptr bus;
174
175=== modified file 'src/ubuntu/application/common/application/location/service.cpp'
176--- src/ubuntu/application/common/application/location/service.cpp 2014-06-24 06:22:58 +0000
177+++ src/ubuntu/application/common/application/location/service.cpp 2014-07-28 20:16:38 +0000
178@@ -35,29 +35,54 @@
179 ua_location_service_create_session_for_low_accuracy(
180 UALocationServiceRequirementsFlags /*flags*/)
181 {
182- return new UbuntuApplicationLocationServiceSession{
183- Instance::instance().get_service()->create_session_for_criteria(cul::Criteria{})};
184+ // Creating the instance might fail for a number of reason and
185+ // we cannot allow exceptions to propagate to prevent applications
186+ // from aborting. For that, we catch all exceptions, provide some error
187+ // information to std::cerr and return a nullptr in case of errors.
188+ try
189+ {
190+ return new UbuntuApplicationLocationServiceSession
191+ {
192+ // Creating the instance might fail for a number of reasons.
193+
194+ Instance::instance().get_service()->create_session_for_criteria(cul::Criteria{})
195+ };
196+ } catch(const std::exception& e)
197+ {
198+ std::cerr << "ua_location_service_create_session_for_low_accuracy: Error creating instance: " << e.what() << std::endl;
199+ } catch(...)
200+ {
201+ std::cerr << "ua_location_service_create_session_for_low_accuracy: Error creating instance." << std::endl;
202+ }
203+
204+ return nullptr;
205 }
206
207 UALocationServiceSession*
208 ua_location_service_create_session_for_high_accuracy(
209 UALocationServiceRequirementsFlags /*flags*/)
210 {
211+ // Creating the instance might fail for a number of reason and
212+ // we cannot allow exceptions to propagate to prevent applications
213+ // from aborting. For that, we catch all exceptions, provide some error
214+ // information to std::cerr and return a nullptr in case of errors.
215 try
216 {
217 return new UbuntuApplicationLocationServiceSession
218 {
219+ // Creating the instance might fail for a number of reasons.
220+
221 Instance::instance().get_service()->create_session_for_criteria(cul::Criteria{})
222 };
223 } catch(const std::exception& e)
224 {
225- fprintf(stderr, "Error creating session for high accuracy: %s \n", e.what());
226+ std::cerr << "ua_location_service_create_session_for_high_accuracy: Error creating instance: " << e.what() << std::endl;
227 } catch(...)
228 {
229- fprintf(stderr, "Error creating session for high accuracy.\n");
230+ std::cerr << "ua_location_service_create_session_for_high_accuracy: Error creating instance." << std::endl;
231 }
232
233- return NULL;
234+ return nullptr;
235 }
236
237 UALocationServiceController*
238
239=== modified file 'src/ubuntu/application/common/application/location/session.cpp'
240--- src/ubuntu/application/common/application/location/session.cpp 2014-06-26 15:07:29 +0000
241+++ src/ubuntu/application/common/application/location/session.cpp 2014-07-28 20:16:38 +0000
242@@ -30,6 +30,9 @@
243 ua_location_service_session_ref(
244 UALocationServiceSession *session)
245 {
246+ if (not session)
247+ return;
248+
249 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
250 s->ref();
251 }
252@@ -38,6 +41,9 @@
253 ua_location_service_session_unref(
254 UALocationServiceSession *session)
255 {
256+ if (not session)
257+ return;
258+
259 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
260 s->unref();
261 }
262@@ -48,7 +54,11 @@
263 UALocationServiceSessionPositionUpdatesHandler handler,
264 void *context)
265 {
266+ if (not session)
267+ return;
268+
269 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
270+
271 try
272 {
273 std::lock_guard<std::mutex> lg(s->position_updates.guard);
274@@ -69,7 +79,11 @@
275 UALocationServiceSessionHeadingUpdatesHandler handler,
276 void *context)
277 {
278+ if (not session)
279+ return;
280+
281 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
282+
283 try
284 {
285 std::lock_guard<std::mutex> lg(s->heading_updates.guard);
286@@ -90,7 +104,11 @@
287 UALocationServiceSessionVelocityUpdatesHandler handler,
288 void *context)
289 {
290+ if (not session)
291+ return;
292+
293 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
294+
295 try
296 {
297 std::lock_guard<std::mutex> lg(s->velocity_updates.guard);
298@@ -109,9 +127,10 @@
299 ua_location_service_session_start_position_updates(
300 UALocationServiceSession *session)
301 {
302+ if (not session)
303+ return U_STATUS_ERROR;
304+
305 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
306- if (!s)
307- return U_STATUS_ERROR;
308
309 try
310 {
311@@ -129,9 +148,10 @@
312 ua_location_service_session_stop_position_updates(
313 UALocationServiceSession *session)
314 {
315+ if (not session)
316+ return;
317+
318 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
319- if (!s)
320- return;
321
322 try
323 {
324@@ -146,9 +166,10 @@
325 ua_location_service_session_start_heading_updates(
326 UALocationServiceSession *session)
327 {
328+ if (not session)
329+ return U_STATUS_ERROR;
330+
331 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
332- if (!s)
333- return U_STATUS_ERROR;
334
335 try
336 {
337@@ -166,9 +187,10 @@
338 ua_location_service_session_stop_heading_updates(
339 UALocationServiceSession *session)
340 {
341+ if (not session)
342+ return;
343+
344 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
345- if (!s)
346- return;
347
348 try
349 {
350@@ -183,9 +205,10 @@
351 ua_location_service_session_start_velocity_updates(
352 UALocationServiceSession *session)
353 {
354+ if (not session)
355+ return U_STATUS_ERROR;
356+
357 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
358- if (!s)
359- return U_STATUS_ERROR;
360
361 try
362 {
363@@ -203,9 +226,10 @@
364 ua_location_service_session_stop_velocity_updates(
365 UALocationServiceSession *session)
366 {
367+ if (not session)
368+ return;
369+
370 auto s = static_cast<UbuntuApplicationLocationServiceSession*>(session);
371- if (!s)
372- return;
373
374 try
375 {

Subscribers

People subscribed via source and target branches