Skip to content

Commit cadf707

Browse files
Store: actually throw if the macOS application group is too long GH#515
Also throw if empty. Also use same character limit in error message as noted in docs. Also actually check the C API call result.
1 parent 8f622c8 commit cadf707

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

objectbox/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## latest
22

3+
* macOS: throw an error if the `macosApplicationGroup` parameter when opening a `Store` is too long
4+
(or empty). [#515](https://github.com/objectbox/objectbox-dart/issues/515#issuecomment-3503022269)
5+
36
### Sync
47

58
* SyncClient: actually configure Sync filters when passing them to `Sync.client` and related helper

objectbox/lib/src/native/store.dart

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,21 +229,30 @@ class Store implements Finalizable {
229229
: _closesNativeStore = true,
230230
_absoluteDirectoryPath = _safeAbsoluteDirectoryPath(directory) {
231231
try {
232-
if (Platform.isMacOS && macosApplicationGroup != null) {
232+
if (macosApplicationGroup != null) {
233+
final isGroupEmpty = macosApplicationGroup.isEmpty;
234+
// Docs don't explicitly require a trailing slash, so add one if missing
233235
if (!macosApplicationGroup.endsWith('/')) {
234236
macosApplicationGroup += '/';
235237
}
236-
if (macosApplicationGroup.length > 20) {
237-
ArgumentError.value(macosApplicationGroup, 'macosApplicationGroup',
238-
'Must be at most 20 characters long');
238+
// The database library would check for length, but refers to 'prefix'
239+
// instead of the parameter name used here. So duplicate the checks.
240+
if (isGroupEmpty || macosApplicationGroup.length > 20) {
241+
throw ArgumentError.value(
242+
macosApplicationGroup,
243+
'macosApplicationGroup',
244+
'Must be at least 1 and at most 19 characters long');
239245
}
240246
// This is required to enable additional interprocess communication
241247
// (IPC) in sandboxed apps (https://developer.apple.com/documentation/xcode/configuring-app-groups),
242248
// like POSIX semaphores, used by mutexes in the ObjectBox database
243249
// library. macOS requires that semaphore names are prefixed with an
244250
// application group ID.
245251
// See the constructor docs for more details.
246-
withNativeString(macosApplicationGroup, C.posix_sem_prefix_set);
252+
// Note: calling this on all platforms is fine, it will be a no-op if
253+
// not supported.
254+
checkObx(
255+
withNativeString(macosApplicationGroup, C.posix_sem_prefix_set));
247256
}
248257
_checkStoreDirectoryNotOpen();
249258
final model = Model(modelDefinition.model);

objectbox_test/test/store_test.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,23 @@ void main() {
290290
Directory('store').deleteSync(recursive: true);
291291
});
292292

293+
test('store macOS application group checks', () {
294+
// Note: the "store options" test tests a valid value
295+
expect(
296+
() => Store(getObjectBoxModel(),
297+
directory: 'store',
298+
macosApplicationGroup:
299+
'this-application-group-identifier-is-way-too-long'),
300+
throwsA(isArgumentError.having((e) => e.message, 'message',
301+
contains('Must be at least 1 and at most 19 characters long'))));
302+
303+
expect(
304+
() => Store(getObjectBoxModel(),
305+
directory: 'store', macosApplicationGroup: ''),
306+
throwsA(isArgumentError.having((e) => e.message, 'message',
307+
contains('Must be at least 1 and at most 19 characters long'))));
308+
});
309+
293310
test('store dbFileSize', () {
294311
final testEnv = TestEnv("db-file-size");
295312
expect(Store.dbFileSize(testEnv.dbDirPath), isPositive);

0 commit comments

Comments
 (0)