Skip to content

Commit

Permalink
Pass length of newline delimited JSON to parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
hoehermann committed Jul 9, 2023
1 parent 5629edd commit 1de71ce
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 8 deletions.
10 changes: 5 additions & 5 deletions src/comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ signald_read_cb(gpointer data, gint source, PurpleInputCondition cond)
if(sa->input_buffer_position[-1] == '\n') {
*sa->input_buffer_position = 0;
purple_debug_info(SIGNALD_PLUGIN_ID, "got newline delimited message: %s", sa->input_buffer);
signald_parse_input(sa, sa->input_buffer);
// reset buffer
*sa->input_buffer = 0;
signald_parse_input(sa, sa->input_buffer, sa->input_buffer_position - sa->input_buffer - 1);
// reset buffer write pointer
sa->input_buffer_position = sa->input_buffer;
}
if (sa->input_buffer_position - sa->input_buffer + 1 == SIGNALD_INPUT_BUFSIZE) {
purple_debug_error(SIGNALD_PLUGIN_ID, "message exceeded buffer size: %s\n", sa->input_buffer);
purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_NETWORK_ERROR, "message exceeded buffer size");
// reset buffer write pointer
// should not have any effect since the connection will be destroyed, but better safe than sorry
sa->input_buffer_position = sa->input_buffer;
// NOTE: incomplete message may be passed to handler during next call
return;
}
read = recv(sa->fd, sa->input_buffer_position, 1, MSG_DONTWAIT);
Expand Down
4 changes: 2 additions & 2 deletions src/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,10 @@ signald_handle_input(SignaldAccount *sa, JsonNode *root)
}

void
signald_parse_input(SignaldAccount *sa, const char * json)
signald_parse_input(SignaldAccount *sa, const char * json, gssize length)
{
JsonParser *parser = json_parser_new();
if (!json_parser_load_from_data(parser, json, -1, NULL)) {
if (!json_parser_load_from_data(parser, json, length, NULL)) {
purple_connection_error(sa->pc, PURPLE_CONNECTION_ERROR_OTHER_ERROR, "Error parsing input.");
return;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#include "structs.h"

void signald_parse_input(SignaldAccount *sa, const char * json);
void signald_parse_input(SignaldAccount *sa, const char * json, gssize length);
1 change: 1 addition & 0 deletions src/login.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ try_connect(SignaldAccount *sa, gchar *socket_path) {
* Tries multiple possible default socket location at once in background.
* In case the user has explicitly defined a socket location, only that one is considered.
*/
// TODO: find out how purple does connections in the gevent loop. use that instead of explicit sockets and threads.
void
signald_connect_socket(SignaldAccount *sa) {
purple_connection_set_state(sa->pc, PURPLE_CONNECTION_CONNECTING);
Expand Down

0 comments on commit 1de71ce

Please sign in to comment.