After receiving some helpful feedback, I decided to make some changes to this question that I posted yesterday. The title and content have been edited for clarity based on the new information I've learned.
Recently, I've been utilizing C code to transmit JSON strings from C to a Firefox extension using the native-messaging API. This setup has been functioning smoothly for a few months now, as I continue to add more code to both programs.
The variable response
points to the JSON string generated in C. As shown, the string is prefixed with a unint32
value representing the number of bytes in the string.
Oddly enough, JSON strings with a byte length equal to a multiple of 256 plus 10 seem to be successfully transmitted from C to the extension but are not being processed by the extension. I have tested values like 266 and 522.
Typically, if an invalid string is transmitted, the background script would throw an error related to malformed JSON or exceeding size limits, resulting in the closure of the C program. However, no errors are triggered when dealing with these specific sizes of JSON strings; the C program remains open.
Furthermore, running the same JSON strings in the C executable command line results in the correct JSON output displayed on the screen/stdout. Interestingly, altering the length of the strings by adding or removing a byte allows the extension to receive and parse them, unlike the mentioned sizes.
To aid in identifying issues, I also write the JSON to a local disk file. Upon inspection, everything appears normal except for one observation. These specific strings contain a smiley face after the uint32 size, causing the remaining bytes and JSON data to move to the next line. In contrast, all other strings retain their original formatting with non-typeable characters on the same line.
Any insights on why the extension fails to pick up these strings?
Thank you.
int send_response( const char *response )
{
int rc;
FILE *fp_out;
fp_out = fopen( "test.txt", "w" );
// Write response ( JSON prefixed with its UTF-8 length ).
const uint32_t len = strlen( response );
if ( ( rc = fwrite( &len, sizeof len, 1, fp_out ) ) != 1 ||
( rc = fwrite( response, len, 1, fp_out ) ) != 1 )
{}
if ( ( rc = fwrite( &len, sizeof len, 1, stdout ) ) != 1 ||
( rc = fwrite( response, sizeof *response, len, stdout ) ) != len )
{
perror( "fwrite" );
return EXIT_FAILURE;
}
fclose( fp_out );
return 0;
} // close send_response