Skip to content

Commit e4fb3c2

Browse files
sort: improved check for empty playlist - fix code duplication, always check for at least 2 items, ensure mutex lock is used
1 parent ce70914 commit e4fb3c2

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/sort.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ plt_sort_random (playlist_t *playlist, int iter) {
253253
// version 1: title formatting v2
254254
static void
255255
plt_sort_internal (playlist_t *playlist, int iter, int id, ddb_tf_context_t *tf_context, const char *tf_bytecode, const char *format, int order, int version, int is_autosorting) {
256+
257+
pl_lock ();
258+
259+
if (playlist->head[iter] == NULL || playlist->head[iter]->next[iter] == NULL) {
260+
pl_unlock ();
261+
return;
262+
}
263+
256264
int ascending = order == DDB_SORT_DESCENDING ? 0 : 1;
257265

258266
if (tf_bytecode == NULL) {
@@ -261,6 +269,7 @@ plt_sort_internal (playlist_t *playlist, int iter, int id, ddb_tf_context_t *tf_
261269
plt_replace_meta (playlist, "autosort_mode", "random");
262270
}
263271
plt_sort_random (playlist, iter);
272+
pl_unlock ();
264273
return;
265274
}
266275
if (!is_autosorting)
@@ -272,25 +281,18 @@ plt_sort_internal (playlist_t *playlist, int iter, int id, ddb_tf_context_t *tf_
272281
}
273282
}
274283

275-
if (format == NULL || id == DB_COLUMN_FILENUMBER || !playlist->head[iter] || !playlist->head[iter]->next[iter]) {
284+
if (format == NULL || id == DB_COLUMN_FILENUMBER) {
285+
pl_unlock ();
276286
return;
277287
}
278288
}
279289

280-
pl_lock ();
281-
282-
if (playlist->count[iter] == 0) {
283-
pl_unlock ();
284-
return;
285-
}
286-
287290
struct timeval tm1;
288291
gettimeofday (&tm1, NULL);
289292
pl_sort_ascending = ascending;
290293
trace ("ascending: %d\n", ascending);
291294
pl_sort_id = id;
292295

293-
294296
char *free_tf_bytecode = NULL;
295297

296298
pl_sort_version = version;

0 commit comments

Comments
 (0)