[zeromq-dev] [PATCH] prefix tree bug

Jon Dyte jon at totient.co.uk
Sun Aug 29 01:01:48 CEST 2010


Prior to this patch prefix_tree asserted for me with this test program

#include <prefix_tree.hpp>
#include <iostream>
#include <stdio.h>

int main()

        zmq::prefix_tree_t m;

        for (int i = 0; i < 512; i++)
            m.add((unsigned char*)&i,sizeof(int));

        int count = 0;
        for (int i = 0; i < 512; i++)
            count += m.check((unsigned char*)&i,sizeof(int));

this is because as it adds the 255th element at a node it attempts to
calculate the count member var which is an unsigned char via
count = (255 -0) + 1;
and pass the result to realloc. Unfortunately the result is zero and 
realloc returns null;
the prefix_tree asserts.

I have fixed it by making the count an unsigned short.

The fix works for me and the program above returns 512 as expected and
valgrind doesnt gripe either, but there may be a better way.

I'm submitting it under the MIT/X11 license should it be deemed acceptable.


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pf.txt
URL: <https://lists.zeromq.org/pipermail/zeromq-dev/attachments/20100829/b520d09c/attachment.txt>

More information about the zeromq-dev mailing list