Fix bugs with skipstone_message_unpack()
This commit is contained in:
parent
272d9fe38c
commit
c676a32460
2 changed files with 9 additions and 6 deletions
|
@ -1,22 +1,25 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <inttypes.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
|
||||||
#include <skipstone/message.h>
|
#include <skipstone/message.h>
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
uint8_t buf[7] = "\x5KMFDM",
|
uint8_t buf[11] = "\x5KMFDM\x04\x03\x02\x01",
|
||||||
str[SKIPSTONE_MESSAGE_MAX_STRLEN];
|
str[SKIPSTONE_MESSAGE_MAX_STRLEN];
|
||||||
|
|
||||||
if (skipstone_message_unpack(buf, sizeof(buf), "z", str) < 0) {
|
uint32_t num;
|
||||||
|
|
||||||
|
if (skipstone_message_unpack(buf, sizeof(buf), "zL", str, &num) < 0) {
|
||||||
fprintf(stderr, "%s: %s: %s\n",
|
fprintf(stderr, "%s: %s: %s\n",
|
||||||
argv[0], "skipstone_message_unpack()", strerror(errno));
|
argv[0], "skipstone_message_unpack()", strerror(errno));
|
||||||
|
|
||||||
goto error_message_unpack;
|
goto error_message_unpack;
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("%s\n", str);
|
printf("%s: %" PRIu32 "\n", str, num);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
|
|
|
@ -137,7 +137,7 @@ ssize_t skipstone_message_unpack(void *message,
|
||||||
|
|
||||||
sz = ((uint8_t *)message)[offset++];
|
sz = ((uint8_t *)message)[offset++];
|
||||||
|
|
||||||
if (len < offset + sz + 1) {
|
if (len < offset + sz) {
|
||||||
goto done;
|
goto done;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,9 +145,9 @@ ssize_t skipstone_message_unpack(void *message,
|
||||||
|
|
||||||
memcpy(dest, (uint8_t *)message + offset, sz);
|
memcpy(dest, (uint8_t *)message + offset, sz);
|
||||||
|
|
||||||
offset += sz;
|
((uint8_t *)dest)[sz] = '\0';
|
||||||
|
|
||||||
((uint8_t *)message)[offset++] = '\0';
|
offset += sz;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue