Skip to content

Commit

Permalink
Problem: zmsg_remove updates size no matter if frame is/isn't a member
Browse files Browse the repository at this point in the history
Solution: decrease the size only if the frame was actually removed
and add a simple test case to cover this
  • Loading branch information
bluca committed Jan 1, 2017
1 parent ddeca53 commit 7417c7d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/zmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,17 @@ zmsg_remove (zmsg_t *self, zframe_t *frame)
assert (self);
assert (zmsg_is (self));

self->content_size -= zframe_size (frame);
// zlist_remove does not give feedback on whether an element was actually
// removed or not. To avoid wrongly decreasing the size in case the frame
// is not actually part of the zmsg, check the size before and after.
// We could do a search but that's a full scan, while the size is saved
// by zlist so checking it is much cheaper.
size_t num_frames_before = zlist_size (self->frames);
zlist_remove (self->frames, frame);
size_t num_frames_after = zlist_size (self->frames);

if (num_frames_before > num_frames_after)
self->content_size -= zframe_size (frame);
}


Expand Down Expand Up @@ -1217,6 +1226,20 @@ zmsg_test (bool verbose)
zsock_destroy (&client);
zsock_destroy (&server);
#endif

// Test message length calculation after removal
msg = zmsg_new ();
zmsg_addstr (msg, "One");
zmsg_addstr (msg, "Two");
size_t size_before = zmsg_content_size (msg);
frame = zframe_new ("Three", strlen ("Three"));
assert (frame);
zmsg_remove (msg, frame);
size_t size_after = zmsg_content_size (msg);
assert (size_before == size_after);
zframe_destroy (&frame);
zmsg_destroy (&msg);

// @end
printf ("OK\n");
}

0 comments on commit 7417c7d

Please sign in to comment.