diff options
author | Alexander Batischev <eual.jp@gmail.com> | 2022-06-18 15:45:31 +0300 |
---|---|---|
committer | Alexander Batischev <eual.jp@gmail.com> | 2022-06-22 22:18:56 +0300 |
commit | cda535be4a3581d5cab3863863aa96358226260f (patch) | |
tree | 2bbb5d90261afe43fc7c9770ef3b72ac9de3b9a8 | |
parent | 4db46a08c662c291997b2c90cd5593f8b2aab457 (diff) |
Check for NULL before calling json_object_get_string()
Fixes #2102.
Co-authored-by: Dennis van der Schagt <dennisschagt@gmail.com>
-rw-r--r-- | src/feedhqapi.cpp | 12 | ||||
-rw-r--r-- | src/freshrssapi.cpp | 24 | ||||
-rw-r--r-- | src/inoreaderapi.cpp | 16 | ||||
-rw-r--r-- | src/newsblurapi.cpp | 75 | ||||
-rw-r--r-- | src/ocnewsapi.cpp | 57 | ||||
-rw-r--r-- | src/oldreaderapi.cpp | 20 |
6 files changed, 150 insertions, 54 deletions
diff --git a/src/feedhqapi.cpp b/src/feedhqapi.cpp index 382cb42e..40591803 100644 --- a/src/feedhqapi.cpp +++ b/src/feedhqapi.cpp @@ -136,12 +136,20 @@ std::vector<TaggedFeedUrl> FeedHqApi::get_subscribed_urls() json_object* id_str{}; json_object_object_get_ex(sub, "id", &id_str); const char* id = json_object_get_string(id_str); + if (id == nullptr) { + LOG(Level::WARN, "Skipping a subscription without an id"); + continue; + } json_object* title_str{}; json_object_object_get_ex(sub, "title", &title_str); const char* title = json_object_get_string(title_str); - - tags.push_back(std::string("~") + title); + if (title != nullptr) { + tags.push_back(std::string("~") + title); + } else { + LOG(Level::WARN, "Subscription has no title, so let's call it \"%i\"", i); + tags.push_back(std::string("~") + std::to_string(i)); + } char* escaped_id = curl_easy_escape(handle.ptr(), id, 0); diff --git a/src/freshrssapi.cpp b/src/freshrssapi.cpp index 881ed184..b8f873c3 100644 --- a/src/freshrssapi.cpp +++ b/src/freshrssapi.cpp @@ -135,11 +135,23 @@ std::vector<TaggedFeedUrl> FreshRssApi::get_subscribed_urls() json_object* sub = json_object_array_get_idx(subscription_obj, i); + json_object* id_str{}; + json_object_object_get_ex(sub, "id", &id_str); + const char* id = json_object_get_string(id_str); + if (id == nullptr) { + LOG(Level::WARN, "Skipping a subscription without an id"); + continue; + } + json_object* title_str{}; json_object_object_get_ex(sub, "title", &title_str); const char* title = json_object_get_string(title_str); - - tags.push_back(std::string("~") + title); + if (title != nullptr) { + tags.push_back(std::string("~") + title); + } else { + LOG(Level::WARN, "Subscription has no title, so let's call it \"%i\"", i); + tags.push_back(std::string("~") + std::to_string(i)); + } json_object* cats_obj{}; json_object_object_get_ex(sub, "categories", &cats_obj); @@ -151,19 +163,19 @@ std::vector<TaggedFeedUrl> FreshRssApi::get_subscribed_urls() json_object* cat_name{}; json_object_object_get_ex(cat, "label", &cat_name); const char* category = json_object_get_string(cat_name); + if (category == nullptr) { + LOG(Level::WARN, "Skipping subscription's category whose name is a null value"); + continue; + } tags.push_back(category); } - json_object* id_str{}; - json_object_object_get_ex(sub, "id", &id_str); - const char* id = json_object_get_string(id_str); char* escaped_id = curl_easy_escape(handle.ptr(), id, 0); auto url = strprintf::fmt("%s%s%s", cfg->get_configvalue("freshrss-url"), FRESHRSS_FEED_PREFIX, escaped_id); urls.push_back(TaggedFeedUrl(url, tags)); - curl_free(escaped_id); } diff --git a/src/inoreaderapi.cpp b/src/inoreaderapi.cpp index e1e37720..1ccacd9b 100644 --- a/src/inoreaderapi.cpp +++ b/src/inoreaderapi.cpp @@ -133,12 +133,20 @@ std::vector<TaggedFeedUrl> InoreaderApi::get_subscribed_urls() json_object_object_get_ex(sub, "id", &node); const char* id = json_object_get_string(node); + if (id == nullptr) { + LOG(Level::WARN, "Skipping a subscription without an id"); + continue; + } char* id_uenc = curl_easy_escape(handle.ptr(), id, 0); json_object_object_get_ex(sub, "title", &node); const char* title = json_object_get_string(node); - - tags.push_back(std::string("~") + title); + if (title != nullptr) { + tags.push_back(std::string("~") + title); + } else { + LOG(Level::WARN, "Subscription has no title, so let's call it \"%i\"", i); + tags.push_back(std::string("~") + std::to_string(i)); + } json_object_object_get_ex(sub, "categories", &node); struct array_list* categories = json_object_get_array(node); @@ -152,6 +160,10 @@ std::vector<TaggedFeedUrl> InoreaderApi::get_subscribed_urls() json_object* label_node{}; json_object_object_get_ex(cat, "label", &label_node); const char* label = json_object_get_string(label_node); + if (label == nullptr) { + LOG(Level::WARN, "Skipping subscription's label whose name is a null value"); + continue; + } tags.push_back(std::string(label)); } diff --git a/src/newsblurapi.cpp b/src/newsblurapi.cpp index fafbf39b..ec129ab9 100644 --- a/src/newsblurapi.cpp +++ b/src/newsblurapi.cpp @@ -109,10 +109,21 @@ std::vector<TaggedFeedUrl> NewsBlurApi::get_subscribed_urls() json_object* feed_json = json_object_iter_peek_value(&it); json_object_object_get_ex(feed_json, "feed_title", &node); - current_feed.title = json_object_get_string(node); + const auto title = json_object_get_string(node); + if (title != nullptr) { + current_feed.title = title; + } else { + LOG(Level::WARN, "Subscription has no title, so let's call it \"%s\"", feed_id); + current_feed.title = feed_id; + } json_object_object_get_ex(feed_json, "feed_link", &node); if (node) { - current_feed.link = json_object_get_string(node); + const auto link = json_object_get_string(node); + if (link == nullptr) { + LOG(Level::WARN, "Skipping a subscription without a link"); + continue; + } + current_feed.link = link; known_feeds[feed_id] = current_feed; std::string std_feed_id(feed_id); @@ -152,6 +163,8 @@ std::map<std::string, std::vector<std::string>> NewsBlurApi::mk_feeds_to_tags( continue; } + // invariant: `tag_to_feed_ids` is a JSON object + json_object_object_foreach( tag_to_feed_ids, key, feeds_with_tag_obj) { std::string std_key(key); @@ -163,9 +176,12 @@ std::map<std::string, std::vector<std::string>> NewsBlurApi::mk_feeds_to_tags( json_object* feed_id_obj = json_object_array_get_idx( feeds_with_tag_obj, j); - std::string feed_id( - json_object_get_string(feed_id_obj)); - result[feed_id].push_back(std_key); + const auto id = json_object_get_string(feed_id_obj); + if (id == nullptr) { + LOG(Level::WARN, "Skipping subscription's tag whose name is a null value"); + continue; + } + result[std::string(id)].push_back(std_key); } } } @@ -183,7 +199,8 @@ bool request_successfull(json_object* payload) if (json_object_object_get_ex(payload, "result", &result) == FALSE) { return false; } else { - return !strcmp("ok", json_object_get_string(result)); + const auto result_str = json_object_get_string(result); + return result_str != nullptr && strcmp("ok", result_str) == 0; } } @@ -284,26 +301,32 @@ rsspp::Feed NewsBlurApi::fetch_feed(const std::string& id) json_object* node{}; - if (json_object_object_get_ex( - item_obj, "story_title", &node) == TRUE) { - item.title = json_object_get_string(node); + if (json_object_object_get_ex(item_obj, "story_title", &node) == TRUE) { + const auto title = json_object_get_string(node); + if (title != nullptr) { + item.title = title; + } } - if (json_object_object_get_ex( - item_obj, "story_authors", &node) == TRUE) { - item.author = json_object_get_string(node); + if (json_object_object_get_ex(item_obj, "story_authors", &node) == TRUE) { + const auto author = json_object_get_string(node); + if (author != nullptr) { + item.author = author; + } } - if (json_object_object_get_ex(item_obj, - "story_permalink", - &node) == TRUE) { - item.link = json_object_get_string(node); + if (json_object_object_get_ex(item_obj, "story_permalink", &node) == TRUE) { + const auto link = json_object_get_string(node); + if (link != nullptr) { + item.link = link; + } } - if (json_object_object_get_ex( - item_obj, "story_content", &node) == TRUE) { - item.content_encoded = - json_object_get_string(node); + if (json_object_object_get_ex(item_obj, "story_content", &node) == TRUE) { + const auto content_encoded = json_object_get_string(node); + if (content_encoded != nullptr) { + item.content_encoded = content_encoded; + } } const char* article_id{}; @@ -325,11 +348,13 @@ rsspp::Feed NewsBlurApi::fetch_feed(const std::string& id) } } - if (json_object_object_get_ex( - item_obj, "story_date", &node) == TRUE) { - const char* pub_date = - json_object_get_string(node); - item.pubDate_ts = parse_date(pub_date); + if (json_object_object_get_ex(item_obj, "story_date", &node) == TRUE) { + const char* pub_date = json_object_get_string(node); + if (pub_date != nullptr) { + item.pubDate_ts = parse_date(pub_date); + } else { + item.pubDate_ts = ::time(nullptr); + } item.pubDate = utils::mt_strf_localtime( "%a, %d %b %Y %H:%M:%S %z", diff --git a/src/ocnewsapi.cpp b/src/ocnewsapi.cpp index 59f7adac..3c43275b 100644 --- a/src/ocnewsapi.cpp +++ b/src/ocnewsapi.cpp @@ -82,7 +82,11 @@ std::vector<TaggedFeedUrl> OcNewsApi::get_subscribed_urls() long folder_id = json_object_get_int(node); json_object_object_get_ex(folder, "name", &node); - folders_map[folder_id] = json_object_get_string(node); + const auto name = json_object_get_string(node); + + if (name != nullptr) { + folders_map[folder_id] = name; + } } rsspp::Feed starred; @@ -111,10 +115,19 @@ std::vector<TaggedFeedUrl> OcNewsApi::get_subscribed_urls() long feed_id = json_object_get_int(node); json_object_object_get_ex(feed, "title", &node); - current_feed.title = json_object_get_string(node); + const auto title = json_object_get_string(node); + if (title != nullptr) { + current_feed.title = title; + } else { + LOG(Level::WARN, "Subscription has no title, so let's call it \"%i\"", i); + current_feed.title = std::string("~") + std::to_string(i); + } json_object_object_get_ex(feed, "url", &node); - current_feed.link = json_object_get_string(node); + const auto link = json_object_get_string(node); + if (link != nullptr) { + current_feed.link = link; + } while (known_feeds.find(current_feed.title) != known_feeds.end()) { @@ -246,18 +259,28 @@ rsspp::Feed OcNewsApi::fetch_feed(const std::string& feed_id) rsspp::Item item; json_object_object_get_ex(item_j, "title", &node); - item.title = json_object_get_string(node); + const auto title = json_object_get_string(node); + if (title != nullptr) { + item.title = title; + } json_object_object_get_ex(item_j, "url", &node); - if (node) { - item.link = json_object_get_string(node); + const auto link = json_object_get_string(node); + if (link != nullptr) { + item.link = link; } json_object_object_get_ex(item_j, "author", &node); - item.author = json_object_get_string(node); + const auto author = json_object_get_string(node); + if (author != nullptr) { + item.author = author; + } json_object_object_get_ex(item_j, "body", &node); - item.content_encoded = json_object_get_string(node); + const auto content_encoded = json_object_get_string(node); + if (content_encoded != nullptr) { + item.content_encoded = content_encoded; + } { json_object* type_obj; @@ -267,13 +290,14 @@ rsspp::Feed OcNewsApi::fetch_feed(const std::string& feed_id) json_object_object_get_ex( item_j, "enclosureLink", &node); - if (type_obj && node) { - const std::string type = - json_object_get_string(type_obj); - if (utils::is_valid_podcast_type(type)) { - item.enclosure_url = - json_object_get_string(node); - item.enclosure_type = std::move(type); + const auto type_ptr = json_object_get_string(type_obj); + const auto url_ptr = json_object_get_string(node); + + if (type_ptr != nullptr) { + const std::string type = type_ptr; + if (utils::is_valid_podcast_type(type) && url_ptr != nullptr) { + item.enclosure_url = url_ptr; + item.enclosure_type = type; } } } @@ -285,8 +309,9 @@ rsspp::Feed OcNewsApi::fetch_feed(const std::string& feed_id) long f_id = json_object_get_int(node); json_object_object_get_ex(item_j, "guid", &node); + const auto guid = json_object_get_string(node); item.guid = std::to_string(id) + ":" + std::to_string(f_id) + - "/" + json_object_get_string(node); + "/" + (guid ? std::string(guid) : std::to_string(i)); json_object_object_get_ex(item_j, "unread", &node); bool unread = json_object_get_boolean(node); diff --git a/src/oldreaderapi.cpp b/src/oldreaderapi.cpp index 3f262b79..b8488af8 100644 --- a/src/oldreaderapi.cpp +++ b/src/oldreaderapi.cpp @@ -136,9 +136,20 @@ std::vector<TaggedFeedUrl> OldReaderApi::get_subscribed_urls() json_object_object_get_ex(sub, "id", &node); const char* id = json_object_get_string(node); + if (id == nullptr) { + LOG(Level::WARN, "Skipping a subscription without an id"); + continue; + } json_object_object_get_ex(sub, "title", &node); - const char* title = json_object_get_string(node); + const char* title_ptr = json_object_get_string(node); + std::string title; + if (title_ptr != nullptr) { + title = title_ptr; + } else { + LOG(Level::WARN, "Subscription has no title, so let's call it \"%i\"", i); + title = std::to_string(i); + } // Ignore URLs where ID start with given prefix - those never // load, always returning 404 and annoying people @@ -161,8 +172,11 @@ std::vector<TaggedFeedUrl> OldReaderApi::get_subscribed_urls() json_object* label_node{}; json_object_object_get_ex( cat, "label", &label_node); - const char* label = - json_object_get_string(label_node); + const char* label = json_object_get_string(label_node); + if (label == nullptr) { + LOG(Level::WARN, "Skipping subscription's label whose name is a null value"); + continue; + } tags.push_back(std::string(label)); } |