diff options
author | Alexander Tkachev | 2016-07-22 20:02:36 +0600 |
---|---|---|
committer | Alexander Tkachev | 2016-08-24 16:07:55 +0600 |
commit | f3959e140106334b33cf74832fc5b20d27407d0a (patch) | |
tree | 420bfa32f00fa24723176bd85dbc4b25cbda45df /backends/networking/curl | |
parent | f95073f0084b3fae3e2cbf14fd98135c37eabbdf (diff) | |
download | scummvm-rg350-f3959e140106334b33cf74832fc5b20d27407d0a.tar.gz scummvm-rg350-f3959e140106334b33cf74832fc5b20d27407d0a.tar.bz2 scummvm-rg350-f3959e140106334b33cf74832fc5b20d27407d0a.zip |
CLOUD: Upload ListDirectory Requests
Lots of checks to avoid JSON-related segfaults added.
Diffstat (limited to 'backends/networking/curl')
-rw-r--r-- | backends/networking/curl/curljsonrequest.cpp | 61 | ||||
-rw-r--r-- | backends/networking/curl/curljsonrequest.h | 7 |
2 files changed, 63 insertions, 5 deletions
diff --git a/backends/networking/curl/curljsonrequest.cpp b/backends/networking/curl/curljsonrequest.cpp index 4d87aa2649..7db56cfdcc 100644 --- a/backends/networking/curl/curljsonrequest.cpp +++ b/backends/networking/curl/curljsonrequest.cpp @@ -42,7 +42,7 @@ CurlJsonRequest::~CurlJsonRequest() { char *CurlJsonRequest::getPreparedContents() { //write one more byte in the end - byte zero[1] = { 0 }; + byte zero[1] = {0}; _contentsStream.write(zero, 1); //replace all "bad" bytes with '.' character @@ -114,8 +114,12 @@ bool CurlJsonRequest::jsonIsObject(Common::JSONValue *item, const char *warningP return false; } -bool CurlJsonRequest::jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix) { +bool CurlJsonRequest::jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional) { if (!item.contains(key)) { + if (isOptional) { + return true; + } + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); return false; } @@ -127,8 +131,12 @@ bool CurlJsonRequest::jsonContainsString(Common::JSONObject &item, const char *k return false; } -bool CurlJsonRequest::jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix) { +bool CurlJsonRequest::jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional) { if (!item.contains(key)) { + if (isOptional) { + return true; + } + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); return false; } @@ -140,4 +148,51 @@ bool CurlJsonRequest::jsonContainsIntegerNumber(Common::JSONObject &item, const return false; } +bool CurlJsonRequest::jsonContainsArray(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional) { + if (!item.contains(key)) { + if (isOptional) { + return true; + } + + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); + return false; + } + + if (item.getVal(key)->isArray()) return true; + + warning("%s: passed item's \"%s\" attribute is not an array!", warningPrefix, key); + debug(9, "%s", item.getVal(key)->stringify(true).c_str()); + return false; +} + +bool CurlJsonRequest::jsonContainsStringOrIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional) { + if (!item.contains(key)) { + if (isOptional) { + return true; + } + + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); + return false; + } + + if (item.getVal(key)->isString() || item.getVal(key)->isIntegerNumber()) return true; + + warning("%s: passed item's \"%s\" attribute is neither a string or an integer!", warningPrefix, key); + debug(9, "%s", item.getVal(key)->stringify(true).c_str()); + return false; +} + +bool CurlJsonRequest::jsonContainsAttribute(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional) { + if (!item.contains(key)) { + if (isOptional) { + return true; + } + + warning("%s: passed item misses the \"%s\" attribute!", warningPrefix, key); + return false; + } + + return true; +} + } // End of namespace Networking diff --git a/backends/networking/curl/curljsonrequest.h b/backends/networking/curl/curljsonrequest.h index 1d1409efc8..cab75bf4bc 100644 --- a/backends/networking/curl/curljsonrequest.h +++ b/backends/networking/curl/curljsonrequest.h @@ -54,8 +54,11 @@ public: virtual void restart(); static bool jsonIsObject(Common::JSONValue *item, const char *warningPrefix); - static bool jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix); - static bool jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix); + static bool jsonContainsString(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false); + static bool jsonContainsIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false); + static bool jsonContainsArray(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false); + static bool jsonContainsStringOrIntegerNumber(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false); + static bool jsonContainsAttribute(Common::JSONObject &item, const char *key, const char *warningPrefix, bool isOptional = false); }; } // End of namespace Networking |