Skip to content

Commit

Permalink
fix MSG_STRING_E length issue (#282)
Browse files Browse the repository at this point in the history
* fix MSG_STRING_E length issue

only need copy _n+1 bytes including the tail '\0', the memcpy should be performed when p+_n+1 equals e

* add unit test for MSG_STRING_E
  • Loading branch information
yutongqing authored Feb 11, 2025
1 parent 4cb7477 commit b298087
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion libsofia-sip-ua/msg/sofia-sip/msg_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ SOFIAPUBFUN issize_t msg_parse_next_field(su_home_t *home, msg_header_t *prev,

/** Encode a string. @HI */
#define MSG_STRING_E(p, e, s) do { \
size_t _n = strlen(s); if (p + _n+1 < e) memcpy(p, s, _n+1); p+= _n; } while(0)
size_t _n = strlen(s); if (p + _n+1 <= e) memcpy(p, s, _n+1); p+= _n; } while(0)

/** Duplicate string. @HI */
#define MSG_STRING_DUP(p, d, s) \
Expand Down
10 changes: 10 additions & 0 deletions libsofia-sip-ua/msg/test_msg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1174,6 +1174,16 @@ int test_copy(void)
msg_destroy(msg1);
msg_destroy(msg);

{
char dst[8];
const char *src = "1234567";

char *start = &dst;
char *end = start+sizeof(dst);
MSG_STRING_E(start, end, src);
TEST_S(dst, src);
}

END();
}

Expand Down

0 comments on commit b298087

Please sign in to comment.