Skip to content

ADD: Http Support#4

Open
surajmaurya-git wants to merge 10 commits into
mainfrom
development
Open

ADD: Http Support#4
surajmaurya-git wants to merge 10 commits into
mainfrom
development

Conversation

@surajmaurya-git
Copy link
Copy Markdown
Collaborator

No description provided.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds HTTP support to the Anedya SDK, allowing operations like device binding and data submission via REST API. It also refactors configuration to use Kconfig and applies consistent formatting. The review highlights critical issues including missing struct members for dynamic allocation, potential null pointer dereferences in OTA and transaction handling, and a buffer overflow risk in response parsing. Additionally, the feedback recommends avoiding large stack allocations and cleaning up redundant code declarations.

Comment thread include/anedya_client.h
char http_base_url[100]; // e.g. "device.ap-in-1.anedya.io"
#endif
uint8_t is_connected;
anedya_txn_store_t txn_store;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The anedya_client_t structure is missing the tx_buffer and rx_buffer members, which are required when ANEDYA_ENABLE_DYNAMIC_ALLOCATION is defined, as seen in the initialization code in src/anedya_client.c (lines 22 and 25). This will result in a compilation error.

Copy link
Copy Markdown
Collaborator Author

@surajmaurya-git surajmaurya-git May 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Comment thread src/anedya_client.c
Comment on lines +340 to +347
int str_len = payload_len;
memset(str, 0, ANEDYA_RX_BUFFER_SIZE);
memset(buffer, 0, ANEDYA_RX_BUFFER_SIZE);
memcpy(str, payload, payload_len);
memcpy(buffer, str, str_len);
// printf("Payload Received: %s\r\n", str);
str[str_len] = '\0';
buffer[str_len] = '\0';
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

security-critical critical

Critical buffer overflow and out-of-bounds write risk. The payload_len is not validated against ANEDYA_RX_BUFFER_SIZE before the memcpy operations. If payload_len is too large, it will overflow the str and buffer arrays. Additionally, writing a null terminator at str[str_len] and buffer[str_len] is out of bounds if str_len equals ANEDYA_RX_BUFFER_SIZE.

Suggested change
int str_len = payload_len;
memset(str, 0, ANEDYA_RX_BUFFER_SIZE);
memset(buffer, 0, ANEDYA_RX_BUFFER_SIZE);
memcpy(str, payload, payload_len);
memcpy(buffer, str, str_len);
// printf("Payload Received: %s\r\n", str);
str[str_len] = '\0';
buffer[str_len] = '\0';
if (payload_len >= ANEDYA_RX_BUFFER_SIZE) {
_anedya_interface_std_out("Error: Payload too large for RX buffer");
return;
}
int str_len = payload_len;
memset(str, 0, ANEDYA_RX_BUFFER_SIZE);
memset(buffer, 0, ANEDYA_RX_BUFFER_SIZE);
memcpy(str, payload, payload_len);
memcpy(buffer, str, str_len);
str[str_len] = '\0';
buffer[str_len] = '\0';

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

Comment thread src/anedya_ota.c
if (_anedya_uuid_parse(deployment_id, dst->deployment_id) != ANEDYA_OK)
return ANEDYA_ERR_PARSE_ERROR;

// assetId
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Potential null pointer dereference. The asset member of anedya_op_ongoing_asset_list_t is a pointer that is accessed here without ensuring it points to a valid anedya_asset_t structure. If the caller has not pre-allocated these structures, the application will crash.

Comment thread src/anedya_client.c
Comment on lines +356 to +359
if (!txn_id || JSON_TEXT != json_getType(txn_id)) {
_anedya_interface_std_out(
"Error, the reqId property is not found in the response.");
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The function continues execution even if the reqId property is missing or invalid. This will lead to a null pointer dereference when json_getValue(txn_id) is called on line 360. A return statement should be added after logging the error.

Suggested change
if (!txn_id || JSON_TEXT != json_getType(txn_id)) {
_anedya_interface_std_out(
"Error, the reqId property is not found in the response.");
}
if (!txn_id || JSON_TEXT != json_getType(txn_id)) {
_anedya_interface_std_out(
"Error, the reqId property is not found in the response.");
return;
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cannot add api missing it

Comment thread src/anedya_op_cmd_list.c
#endif /* ANEDYA_CONNECTION_METHOD_MQTT */

#ifdef ANEDYA_CONNECTION_METHOD_HTTP
char resp_buf[ANEDYA_RX_BUFFER_SIZE];
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Allocating a large buffer like resp_buf[ANEDYA_RX_BUFFER_SIZE] on the stack is dangerous in embedded systems and can lead to stack overflow. It is better to use a buffer from the client instance or allocate it dynamically.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it it later when optimize for the dynamzic allocation

Comment thread include/anedya_client.h Outdated
Comment on lines +88 to +90
#ifdef ANEDYA_CONNECTION_METHOD_HTTP
void _anedya_handle_http_txn_response(anedya_client_t *cl, char *payload, int payload_len, anedya_txn_t *txn);
#endif
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The declaration of _anedya_handle_http_txn_response is duplicated here. It is already declared on lines 81-83.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment on lines +192 to +196
anedya_err_t err = _anedya_txn_register(client, txn);
if (err != ANEDYA_OK) {
return err;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Redundant nested #ifdef blocks for ANEDYA_CONNECTION_METHOD_MQTT found here.

#ifdef ANEDYA_CONNECTION_METHOD_MQTT
  p = anedya_json_nstr(p, "reqId", slot_number, digitLen, &marker);
#endif

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api issue

…t payload size validation in response handler
Comment thread include/anedya_client.h
*
* In HTTP mode there is no long-lived connection to establish. This function
* simply validates the configuration, builds the base URL, and marks
* is_connected = 1 so that all operation functions can proceed.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_connected = 1 is misleading when there is no connection. Client programs can break when there is no internet but called client_connect by changing connection method to HTTP by mistake. As programs can simply call is_connected and see it is set to 1 while ther might be situation that device isn't even connected. In fact I suggest that we remove anedya_client_connect from HTTP altogether when HTTP mode is selcted, as there's literally nothing to connect.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or other option is throw ANEDYA_UNSUPPORTED_FUNCTION error. The only purpose is if we don't do this users will never know the failure until it happens, as same code which was written for MQTT for checking connection status will keep workign with HTTP mode until operations do not work. That will be a silent failure.

Comment thread include/anedya_client.h
*
* @param[in] client Pointer to the anedya_client_t to operate on.
* @retval ANEDYA_OK always.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same applies for disconnect, I suggest remove these methods from HTTP mode. This will force users to handle things as per MQTT or HTTP.

Comment thread src/anedya_client.c
#endif /* ANEDYA_CONNECTION_METHOD_MQTT */

#ifdef ANEDYA_CONNECTION_METHOD_HTTP
void _anedya_handle_http_txn_response(anedya_client_t *cl, char *payload,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible let's keep the txn_response handling function common between both MQTT and HTTP mode and rather just adjust the core difference that is changed inside that function as per HTTP or MQTT mode. It'll create future bugs when we add more operations into it as we need to maintain now both HTTP part and MQTT part. Keep the path same as long as possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants