Skip to content

Commit

Permalink
JSON crash fixes, atari json query no longer needs device prefix
Browse files Browse the repository at this point in the history
Apple test changes to track crashing JSON
Ensure json query starts with slash to stop crashes. do not fail atari json query not starting with device spec
Fix debug line endings
markjfisher committed Oct 20, 2023
1 parent d0e457c commit 3065543
Showing 5 changed files with 46 additions and 19 deletions.
2 changes: 1 addition & 1 deletion lib/device/iwm/network.cpp
Original file line number Diff line number Diff line change
@@ -389,7 +389,7 @@ void iwmNetwork::json_query(iwm_decoded_cmd_t cmd)
// addy |= ((cmd.g7byte6 & 0x7f) | ((cmd.grp7msb << 6) & 0x80)) << 8;
// addy |= ((cmd.g7byte7 & 0x7f) | ((cmd.grp7msb << 7) & 0x80)) << 16;

Debug_printf("Query set to: %s\n", string((char *)data_buffer, data_len).c_str());
Debug_printf("\r\nQuery set to: %s, data_len: %d\r\n", string((char *)data_buffer, data_len).c_str(), data_len);
json.setReadQuery(string((char *)data_buffer, data_len),cmdFrame.aux2);
}

38 changes: 23 additions & 15 deletions lib/device/sio/network.cpp
Original file line number Diff line number Diff line change
@@ -7,7 +7,10 @@
#include "network.h"

#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <memory>

#include "../../include/debug.h"
#include "../../include/pinmap.h"
@@ -1106,7 +1109,6 @@ void sioNetwork::sio_set_json_query()
{
uint8_t in[256];
const char *inp = NULL;
uint8_t *tmp;

memset(in, 0, sizeof(in));

@@ -1119,22 +1121,28 @@ void sioNetwork::sio_set_json_query()
in[i] = 0x00;
}

inp = strrchr((const char *)in, ':');

if (inp == NULL)
{
sio_error();
return;
std::string in_string(reinterpret_cast<char*>(in));
size_t last_colon_pos = in_string.rfind(':');

std::string inp_string;
if (last_colon_pos != std::string::npos) {
Debug_printf("sioNetwork::sio_set_json_query - skipped device spec. Application should be updated to remove it from query.");
inp_string = in_string.substr(last_colon_pos + 1);
} else {
inp_string = in_string;
}

inp++;
json->setReadQuery(string(inp), cmdFrame.aux2);
json_bytes_remaining = json->readValueLen();
tmp = (uint8_t *)malloc(json->readValueLen());
json->readValue(tmp,json_bytes_remaining);
*receiveBuffer += string((const char *)tmp,json_bytes_remaining);
free(tmp);
Debug_printf("Query set to %s\n",inp);
json->setReadQuery(inp_string, cmdFrame.aux2);
json_bytes_remaining = json->json_bytes_remaining;

std::vector<uint8_t> tmp(json_bytes_remaining);
json->readValue(tmp.data(), json_bytes_remaining);

// don't copy past first nul char in tmp
auto null_pos = std::find(tmp.begin(), tmp.end(), 0);
*receiveBuffer += std::string(tmp.begin(), null_pos);

Debug_printf("Query set to >%s<\r\n", inp_string.c_str());
sio_complete();
}

15 changes: 12 additions & 3 deletions lib/fnjson/fnjson.cpp
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ void FNJSON::setQueryParam(uint8_t qp)
*/
void FNJSON::setReadQuery(const string &queryString, uint8_t queryParam)
{
Debug_printf("FNJSON::setReadQuery queryString: %s, queryParam: %d\r\n", queryString.c_str(), queryParam);
_queryString = queryString;
_queryParam = queryParam;
_item = resolveQuery();
@@ -77,9 +78,9 @@ void FNJSON::setReadQuery(const string &queryString, uint8_t queryParam)
*/
cJSON *FNJSON::resolveQuery()
{
if (_queryString.empty())
return _json;

// Queries must start with a slash, else the JSON parsing crashes FujiNet
// An alternative fix would be to check if the returned value was equal to _json and do something with it, but this is simpler.
_queryString = prependSlash(_queryString);
return cJSONUtils_GetPointer(_json, _queryString.c_str());
}

@@ -149,6 +150,14 @@ string FNJSON::processString(string in)
*/
string FNJSON::getValue(cJSON *item)
{
if (item == NULL)
{
Debug_printf("\r\nFNJSON::getValue called with null item, returning empty string.\r\n");
return string("");
}

// char *asString = cJSON_PrintUnformatted(item);
// Debug_printf("FNJSON::getValue called with item: >%s<\r\n", asString);
stringstream ss;

if (cJSON_IsString(item))
7 changes: 7 additions & 0 deletions lib/utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -896,3 +896,10 @@ char *util_hexdump(const void *buf, size_t len) {
bool isApproximatelyInteger(double value, double tolerance) {
return std::abs(value - std::floor(value)) < tolerance;
}

std::string prependSlash(const std::string& str) {
if (str.empty() || str[0] != '/') {
return "/" + str;
}
return str;
}
3 changes: 3 additions & 0 deletions lib/utils/utils.h
Original file line number Diff line number Diff line change
@@ -95,4 +95,7 @@ char *util_hexdump(const void *buf, size_t len);
// check if a double is very close to an integer
bool isApproximatelyInteger(double value, double tolerance = 1e-6);

// ensure string starts with a "/"
std::string prependSlash(const std::string& str);

#endif // _FN_UTILS_H

0 comments on commit 3065543

Please sign in to comment.