From e98e2a0b07363132707146f571d3411e3e1b8076 Mon Sep 17 00:00:00 2001 From: Denis Krjuchkov Date: Sun, 27 Jan 2013 13:26:17 +0600 Subject: Path::FromUTF8() returns nulled instance on error, add error handling where required --- src/Main.cxx | 23 ++++++++++++++++++--- src/Mapper.cxx | 45 ++++++++++++++++++++++++++++++++--------- src/Mapper.hxx | 3 ++- src/PlaylistSave.cxx | 3 ++- src/UpdateWalk.cxx | 4 ++-- src/db/SimpleDatabasePlugin.cxx | 6 ++++++ src/fs/Path.cxx | 3 --- src/fs/Path.hxx | 5 +---- 8 files changed, 69 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/Main.cxx b/src/Main.cxx index 41327916b..6cf9c3457 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -103,6 +103,11 @@ Partition *global_partition; static StateFile *state_file; +static inline GQuark main_quark() +{ + return g_quark_from_static_string ("main"); +} + static bool glue_daemonize_init(const struct options *options, GError **error_r) { @@ -144,7 +149,10 @@ glue_mapper_init(GError **error_r) if (music_dir == NULL) music_dir = g_strdup(g_get_user_special_dir(G_USER_DIRECTORY_MUSIC)); - mapper_init(music_dir, playlist_dir); + if (!mapper_init(music_dir, playlist_dir, &error)) { + g_propagate_error(error_r, error); + return false; + } g_free(music_dir); g_free(playlist_dir); @@ -236,9 +244,18 @@ glue_state_file_init(GError **error_r) return true; } - state_file = new StateFile(Path::FromUTF8(path), - *global_partition, *main_loop); + Path path_fs = Path::FromUTF8(path); + g_free(path); + + if (path_fs.IsNull()) { + g_set_error(error_r, main_quark(), 0, + "Failed to convert state file path to FS encoding"); + return false; + } + + state_file = new StateFile(std::move(path_fs), + *global_partition, *main_loop); state_file->Read(); return true; } diff --git a/src/Mapper.cxx b/src/Mapper.cxx index fa5af2dba..559df0d23 100644 --- a/src/Mapper.cxx +++ b/src/Mapper.cxx @@ -57,6 +57,12 @@ static size_t music_dir_fs_length; */ static Path playlist_dir_fs = Path::Null(); +static inline GQuark +mapper_quark() +{ + return g_quark_from_static_string ("mapper"); +} + /** * Duplicate a string, chop all trailing slashes. */ @@ -98,31 +104,52 @@ check_directory(const char *path_utf8, const Path &path_fs) g_warning("No permission to read directory: %s", path_utf8); } -static void -mapper_set_music_dir(const char *path_utf8) +static bool +mapper_set_music_dir(const char *path_utf8, GError **error_r) { + music_dir_fs = Path::FromUTF8(path_utf8); + if (music_dir_fs.IsNull()) { + g_set_error(error_r, mapper_quark(), 0, + "Failed to convert music path to FS encoding"); + return false; + } + + music_dir_fs_length = music_dir_fs.length(); + music_dir_utf8 = strdup_chop_slash(path_utf8); music_dir_utf8_length = strlen(music_dir_utf8); - music_dir_fs = Path::FromUTF8(path_utf8); check_directory(path_utf8, music_dir_fs); - music_dir_fs_length = music_dir_fs.length(); + + return true; } -static void -mapper_set_playlist_dir(const char *path_utf8) +static bool +mapper_set_playlist_dir(const char *path_utf8, GError **error_r) { playlist_dir_fs = Path::FromUTF8(path_utf8); + if (playlist_dir_fs.IsNull()) { + g_set_error(error_r, mapper_quark(), 0, + "Failed to convert playlist path to FS encoding"); + return false; + } + check_directory(path_utf8, playlist_dir_fs); + return true; } -void mapper_init(const char *_music_dir, const char *_playlist_dir) +bool mapper_init(const char *_music_dir, const char *_playlist_dir, + GError **error_r) { if (_music_dir != NULL) - mapper_set_music_dir(_music_dir); + if (!mapper_set_music_dir(_music_dir, error_r)) + return false; if (_playlist_dir != NULL) - mapper_set_playlist_dir(_playlist_dir); + if (!mapper_set_playlist_dir(_playlist_dir, error_r)) + return false; + + return true; } void mapper_finish(void) diff --git a/src/Mapper.hxx b/src/Mapper.hxx index c01db759a..af6c84cc8 100644 --- a/src/Mapper.hxx +++ b/src/Mapper.hxx @@ -33,7 +33,8 @@ class Path; struct Directory; struct song; -void mapper_init(const char *_music_dir, const char *_playlist_dir); +bool mapper_init(const char *_music_dir, const char *_playlist_dir, + GError **error_r); void mapper_finish(void); diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index d165911a4..49e475ee5 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -46,7 +46,8 @@ playlist_print_song(FILE *file, const struct song *song) const Path uri_fs = Path::FromUTF8(uri); g_free(uri); - fprintf(file, "%s\n", uri_fs.c_str()); + if (!uri_fs.IsNull()) + fprintf(file, "%s\n", uri_fs.c_str()); } } diff --git a/src/UpdateWalk.cxx b/src/UpdateWalk.cxx index 3e0dfe48d..322a8fce2 100644 --- a/src/UpdateWalk.cxx +++ b/src/UpdateWalk.cxx @@ -104,7 +104,7 @@ remove_excluded_from_directory(Directory *directory, directory_for_each_child_safe(child, n, directory) { const Path name_fs = Path::FromUTF8(child->GetName()); - if (exclude_list.Check(name_fs.c_str())) { + if (name_fs.IsNull() || exclude_list.Check(name_fs.c_str())) { delete_directory(child); modified = true; } @@ -115,7 +115,7 @@ remove_excluded_from_directory(Directory *directory, assert(song->parent == directory); const Path name_fs = Path::FromUTF8(song->uri); - if (exclude_list.Check(name_fs.c_str())) { + if (name_fs.IsNull() || exclude_list.Check(name_fs.c_str())) { delete_song(directory, song); modified = true; } diff --git a/src/db/SimpleDatabasePlugin.cxx b/src/db/SimpleDatabasePlugin.cxx index b7c60d9d8..d617b08dd 100644 --- a/src/db/SimpleDatabasePlugin.cxx +++ b/src/db/SimpleDatabasePlugin.cxx @@ -71,6 +71,12 @@ SimpleDatabase::Configure(const struct config_param *param, GError **error_r) path = Path::FromUTF8(_path); free(_path); + if (path.IsNull()) { + g_set_error(error_r, simple_db_quark(), 0, + "Failed to convert database path to FS encoding"); + return false; + } + return true; } diff --git a/src/fs/Path.cxx b/src/fs/Path.cxx index 7664e3ac0..cb808b36c 100644 --- a/src/fs/Path.cxx +++ b/src/fs/Path.cxx @@ -82,9 +82,6 @@ Path Path::FromUTF8(const char *path_utf8) p = g_convert(path_utf8, -1, fs_charset.c_str(), "utf-8", NULL, NULL, NULL); - if (p == NULL) - /* fall back to UTF-8 */ - p = g_strdup(path_utf8); return Path(Donate(), p); } diff --git a/src/fs/Path.hxx b/src/fs/Path.hxx index b9754b5c5..8635d12a9 100644 --- a/src/fs/Path.hxx +++ b/src/fs/Path.hxx @@ -144,10 +144,7 @@ public: /** * Convert a UTF-8 C string to a #Path instance. - * Returns a duplicate of the UTF-8 string on failure. - * - * TODO: return a "nulled" instance on error and add checks to - * all callers + * Returns return a "nulled" instance on error. */ gcc_pure static Path FromUTF8(const char *path_utf8); -- cgit v1.2.3