]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mood.c
mood.c: Fix memory leak in change_current_mood().
[paraslash.git] / mood.c
diff --git a/mood.c b/mood.c
index c06f695c36f8d29664cc48e2e877a7d758702913..4e0a7e3ddca78ed0e274cd2afba792fe767e25c3 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -1,8 +1,4 @@
-/*
- * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 
 /** \file mood.c Paraslash's mood handling functions. */
 
@@ -422,10 +418,13 @@ static int load_mood(const struct osl_row *mood_row, struct mood **m,
 
        *m = NULL;
        ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
-       if (ret < 0)
+       if (ret < 0) {
+               if (errmsg)
+                       *errmsg = make_message(
+                               "could not read mood definition");
                return ret;
-       if (!*mood_name)
-               return -E_DUMMY_ROW;
+       }
+       assert(*mood_name);
        mlpd.m = alloc_new_mood(mood_name);
        ret = for_each_line(FELF_READ_ONLY, mood_def.data, mood_def.size,
                parse_mood_line, &mlpd);
@@ -874,13 +873,19 @@ int change_current_mood(const char *mood_name, char **errmsg)
        if (mood_name) {
                struct mood *m;
                struct osl_row *row;
-               struct osl_object obj = {
-                       .data = (char *)mood_name,
-                       .size = strlen(mood_name) + 1
-               };
+               struct osl_object obj;
+
+               if (!*mood_name) {
+                       *errmsg = make_message("empty mood name");
+                       return -ERRNO_TO_PARA_ERROR(EINVAL);
+               }
+               obj.data = (char *)mood_name;
+               obj.size = strlen(mood_name) + 1;
                ret = osl(osl_get_row(moods_table, BLOBCOL_NAME, &obj, &row));
                if (ret < 0) {
-                       PARA_NOTICE_LOG("no such mood: %s\n", mood_name);
+                       if (errmsg)
+                               *errmsg = make_message("no such mood: %s",
+                                       mood_name);
                        return ret;
                }
                ret = load_mood(row, &m, errmsg);
@@ -895,18 +900,27 @@ int change_current_mood(const char *mood_name, char **errmsg)
        aa.m = current_mood;
        PARA_NOTICE_LOG("computing statistics of admissible files\n");
        ret = audio_file_loop(&aa, add_if_admissible);
-       if (ret < 0)
-               return ret;
+       if (ret < 0) {
+               if (errmsg)
+                       *errmsg = make_message("audio file loop failed");
+               goto out;
+       }
        for (i = 0; i < statistics.num; i++) {
                struct admissible_file_info *a = aa.array + i;
                ret = add_to_score_table(a->aft_row, a->score);
-               if (ret < 0)
+               if (ret < 0) {
+                       if (errmsg)
+                               *errmsg = make_message(
+                                       "could not add row to score table");
                        goto out;
+               }
        }
        log_statistics();
        ret = statistics.num;
 out:
        free(aa.array);
+       if (ret < 0)
+               close_current_mood();
        return ret;
 }