Don't see the need for the extra malloc here, you only use memcmp anyway so why not use msg.data() directly and skip the malloc/free?