-
Notifications
You must be signed in to change notification settings - Fork 423
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
coap_pdu_t inconsistencies regarding data size #60
Comments
I agree that changing the length field to size_t would be a good idea (even if no overflow is possible because a CoAP message cannot exceed 64 KiB and the datatype short is at least 16 bits wide). Having unsigned int for the len argument of coap_add_data() is perfectly valid in my opinion (being a matter of taste, no real technical reason for or against). |
I see. I've suggested size_t, because it's the commonly used type to depict sizes/lengths/numbers of elements, etc. I've suggested to use it everywhere for two main reasons:
Another idea might be using unsigned short only, considering the size limit you've mentioned, but I'm not sure if it's worth the types-juggling in the user-facing APIs. Also, since the CoAP message cannot be larger than 64KB, it might also be a good idea to add that check inside the function's code. Unless "max_size" is supposed to handle that already, but I couldn't find the code, which would set it to this maximum and it seems you can use coap_pdu_clear() to set it to any arbitrary value. Please, let me know your thoughts. |
Note that the 64 KiB limitation only holds for CoAP over UDP. With TCP, a single message might be as large as 4295033115 B (1 + 4 + 1 + 8 + 2^32 + 65805), c.f. https://tools.ietf.org/html/draft-ietf-core-coap-tcp-tls-05#section-2. This rules out |
Great to hear it. In that case I'll try to prepare my suggestion of changes and do a pull request soon. |
Thanks. Do you intend to do more complex changes than, e.g., the following? diff --git a/include/coap/pdu.h b/include/coap/pdu.h
index 7ed482d..2ae6cab 100644
--- a/include/coap/pdu.h
+++ b/include/coap/pdu.h
@@ -231,7 +231,7 @@ typedef struct {
* depending on the memory management
* implementation. */
unsigned short max_delta; /**< highest option number */
- unsigned short length; /**< PDU length (including header, options, data) */
+ size_t length; /**< PDU length (including header, options, data) */
unsigned char *data; /**< payload */
#ifdef WITH_LWIP
@@ -373,7 +373,7 @@ unsigned char *coap_add_option_later(coap_pdu_t *pdu,
* only once per PDU, otherwise the result is undefined.
*/
int coap_add_data(coap_pdu_t *pdu,
- unsigned int len,
+ size_t len,
const unsigned char *data);
/**
diff --git a/src/pdu.c b/src/pdu.c
index ff73d6a..b4ed6a9 100644
--- a/src/pdu.c
+++ b/src/pdu.c
@@ -231,7 +231,7 @@ coap_add_option_later(coap_pdu_t *pdu, unsigned short type, unsigned int len) {
}
int
-coap_add_data(coap_pdu_t *pdu, unsigned int len, const unsigned char *data) {
+coap_add_data(coap_pdu_t *pdu, size_t len, const unsigned char *data) {
assert(pdu);
assert(pdu->data == NULL);
|
Well that and everything this change will touch. This includes coap_pdu_clear2() for instance. I was also thinking about changing other arguments/members/variables referring to size, which would include changes in:
What is your opinion about these changes? |
I would prefer to first touch only things that already exist in the develop branch. Hence this would be coap_option.length and coap_add_option() and possibly coap_add_option_later(). |
Sounds good, I'm ok with that. |
Great. Are you going to provide a PR? |
Yes. Would it be ok to do a PR first to dthaler's branch? It appears that's the one our project currently relies on and it seems that it's on its way to get merged to this repo at one point. |
Okay, please go ahead. |
Thank you. In that case would also be ok to include the changes to other APIs (present in that fork) I've mentioned earlier? |
Hey, I've tried to check all inconsistencies and it seems this would require deprecating many APIs and creating new ones with changed arguments in addition to very thorough source files changes. For now I've implemented this change where I mainly fix /W4 warnings but also make sure that all casts are successful and don't lead to data loss. |
@obgm: Please take a look at this change - it would be great to have these changes in the main repo. We can include them only in dthaler's fork, but that way these changes are only limited to that one repo and make future merging back with your original harder. |
Closing this for now as PR #113 took care of the inconsistencies regarding the size type. |
The coap_pdu_t struct currently has a size_t "max_size" field and an unsigned short "length" field, thus making it possible to use coap_add_data() and overflow "length". Also, coap_add_data() is using a unsigned int "len" argument to pass the amount of the new data.
It would be good to decide on one specific type and stick to it throughout the code.
The text was updated successfully, but these errors were encountered: