forked from uxjulia/CrossInk
-
Notifications
You must be signed in to change notification settings - Fork 2
Fix silently dropped TRMNL image_url: decode \uXXXX escapes, widen token buffer #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mcmphd
wants to merge
5
commits into
yazdipour:development
Choose a base branch
from
mcmphd:fix/trmnl-json-token-buffer-overflow
base: development
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
32fc82a
feat(opds): download opds files in specific dir
yazdipour 154b3bb
feat(TRMNL): implement TRMNL Lockscreen
yazdipour 05bbe27
fix(TRMNL): widen JSON token buffer to stop silent image_url drops
mcmphd a51e76d
fix(TRMNL): decode \uXXXX escapes instead of passing them through raw
mcmphd 32bb350
fix: address sourcery review -- reset unicode state on error, fix bro…
mcmphd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #include "TrmnlDisplayJsonParser.h" | ||
|
|
||
| #include <cstdlib> | ||
| #include <cstring> | ||
|
|
||
| namespace { | ||
| void safeCopy(char* dst, const size_t dstSize, const char* src, const size_t srcLen) { | ||
| const size_t n = srcLen < dstSize - 1 ? srcLen : dstSize - 1; | ||
| memcpy(dst, src, n); | ||
| dst[n] = '\0'; | ||
| } | ||
| } // namespace | ||
|
|
||
| TrmnlDisplayJsonParser::TrmnlDisplayJsonParser() | ||
| : parser(JsonCallbacks{this, sOnKey, sOnString, sOnNumber, sOnBool, sOnNull, sOnObjectStart, sOnObjectEnd, | ||
| sOnArrayStart, sOnArrayEnd}) { | ||
| reset(); | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::reset() { | ||
| parser.reset(); | ||
| lastKey = LastKey::NONE; | ||
| depth = 0; | ||
| imageUrl[0] = '\0'; | ||
| filename[0] = '\0'; | ||
| refreshRateSeconds = 0; | ||
| imageUrlFound = false; | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::feed(const char* data, const size_t len) { parser.feed(data, len); } | ||
| bool TrmnlDisplayJsonParser::foundImageUrl() const { return imageUrlFound; } | ||
| bool TrmnlDisplayJsonParser::hasError() const { return parser.hasError(); } | ||
| const char* TrmnlDisplayJsonParser::getImageUrl() const { return imageUrl; } | ||
| const char* TrmnlDisplayJsonParser::getFilename() const { return filename; } | ||
| uint32_t TrmnlDisplayJsonParser::getRefreshRateSeconds() const { return refreshRateSeconds; } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnKey(void* ctx, const char* key, const size_t len) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth != 1) { | ||
| self->lastKey = LastKey::NONE; | ||
| return; | ||
| } | ||
| if (len == 9 && memcmp(key, "image_url", 9) == 0) { | ||
| self->lastKey = LastKey::IMAGE_URL; | ||
| } else if ((len == 8 && memcmp(key, "filename", 8) == 0) || (len == 10 && memcmp(key, "image_name", 10) == 0)) { | ||
| self->lastKey = LastKey::FILENAME; | ||
| } else if (len == 12 && memcmp(key, "refresh_rate", 12) == 0) { | ||
| self->lastKey = LastKey::REFRESH_RATE; | ||
| } else { | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnString(void* ctx, const char* value, const size_t len) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth == 1 && self->lastKey == LastKey::IMAGE_URL) { | ||
| safeCopy(self->imageUrl, sizeof(self->imageUrl), value, len); | ||
| self->imageUrlFound = self->imageUrl[0] != '\0'; | ||
| } else if (self->depth == 1 && self->lastKey == LastKey::FILENAME) { | ||
| safeCopy(self->filename, sizeof(self->filename), value, len); | ||
| } | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnNumber(void* ctx, const char* value, const size_t /*len*/) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth == 1 && self->lastKey == LastKey::REFRESH_RATE) { | ||
| self->refreshRateSeconds = static_cast<uint32_t>(strtoul(value, nullptr, 10)); | ||
| } | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
|
|
||
| void TrmnlDisplayJsonParser::sOnBool(void* ctx, bool /*value*/) { | ||
| static_cast<TrmnlDisplayJsonParser*>(ctx)->lastKey = LastKey::NONE; | ||
| } | ||
| void TrmnlDisplayJsonParser::sOnNull(void* ctx) { static_cast<TrmnlDisplayJsonParser*>(ctx)->lastKey = LastKey::NONE; } | ||
| void TrmnlDisplayJsonParser::sOnObjectStart(void* ctx) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| self->depth++; | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
| void TrmnlDisplayJsonParser::sOnObjectEnd(void* ctx) { | ||
| auto* self = static_cast<TrmnlDisplayJsonParser*>(ctx); | ||
| if (self->depth > 0) self->depth--; | ||
| self->lastKey = LastKey::NONE; | ||
| } | ||
| void TrmnlDisplayJsonParser::sOnArrayStart(void* ctx) { sOnObjectStart(ctx); } | ||
| void TrmnlDisplayJsonParser::sOnArrayEnd(void* ctx) { sOnObjectEnd(ctx); } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
|
|
||
| #include "StreamingJsonParser.h" | ||
|
|
||
| class TrmnlDisplayJsonParser { | ||
| public: | ||
| TrmnlDisplayJsonParser(); | ||
| void reset(); | ||
| void feed(const char* data, size_t len); | ||
|
|
||
| bool foundImageUrl() const; | ||
| bool hasError() const; | ||
| const char* getImageUrl() const; | ||
| const char* getFilename() const; | ||
| uint32_t getRefreshRateSeconds() const; | ||
|
|
||
| private: | ||
| enum class LastKey : uint8_t { NONE, IMAGE_URL, FILENAME, REFRESH_RATE }; | ||
|
|
||
| static void sOnKey(void* ctx, const char* key, size_t len); | ||
| static void sOnString(void* ctx, const char* value, size_t len); | ||
| static void sOnNumber(void* ctx, const char* value, size_t len); | ||
| static void sOnBool(void* ctx, bool value); | ||
| static void sOnNull(void* ctx); | ||
| static void sOnObjectStart(void* ctx); | ||
| static void sOnObjectEnd(void* ctx); | ||
| static void sOnArrayStart(void* ctx); | ||
| static void sOnArrayEnd(void* ctx); | ||
|
|
||
| StreamingJsonParser parser; | ||
| LastKey lastKey = LastKey::NONE; | ||
| uint16_t depth = 0; | ||
| // Matches StreamingJsonParser::TOKEN_BUF_SIZE -- see its comment for why this needs | ||
| // real headroom beyond a "typical" image_url length. | ||
| char imageUrl[1024] = ""; | ||
| char filename[128] = ""; | ||
| uint32_t refreshRateSeconds = 0; | ||
| bool imageUrlFound = false; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.