bugfix: don't free buffer where DONTCOPY tag was not set

This commit is contained in:
云风
2013-08-16 16:03:57 +08:00
parent 9378a8d657
commit dd188fd528

View File

@@ -557,7 +557,7 @@ skynet_forward(struct skynet_context * context, uint32_t destination) {
static void static void
_filter_args(struct skynet_context * context, int type, int *session, void ** data, size_t * sz) { _filter_args(struct skynet_context * context, int type, int *session, void ** data, size_t * sz) {
int dontcopy = type & PTYPE_TAG_DONTCOPY; int needcopy = !(type & PTYPE_TAG_DONTCOPY);
int allocsession = type & PTYPE_TAG_ALLOCSESSION; int allocsession = type & PTYPE_TAG_ALLOCSESSION;
type &= 0xff; type &= 0xff;
@@ -566,15 +566,12 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da
*session = skynet_context_newsession(context); *session = skynet_context_newsession(context);
} }
char * msg; if (needcopy && *data) {
if (dontcopy || *data == NULL) { char * msg = malloc(*sz+1);
msg = *data;
} else {
msg = malloc(*sz+1);
memcpy(msg, *data, *sz); memcpy(msg, *data, *sz);
msg[*sz] = '\0'; msg[*sz] = '\0';
*data = msg;
} }
*data = msg;
assert((*sz & HANDLE_MASK) == *sz); assert((*sz & HANDLE_MASK) == *sz);
*sz |= type << HANDLE_REMOTE_SHIFT; *sz |= type << HANDLE_REMOTE_SHIFT;
@@ -622,7 +619,9 @@ skynet_sendname(struct skynet_context * context, const char * addr , int type, i
} else if (addr[0] == '.') { } else if (addr[0] == '.') {
des = skynet_handle_findname(addr + 1); des = skynet_handle_findname(addr + 1);
if (des == 0) { if (des == 0) {
free(data); if (type & PTYPE_TAG_DONTCOPY) {
free(data);
}
skynet_error(context, "Drop message to %s", addr); skynet_error(context, "Drop message to %s", addr);
return session; return session;
} }