Boost logo

Boost :

From: Richard Hadsell (hadsell_at_[hidden])
Date: 2002-09-04 15:05:39

This is a bug in the 1.28.0 version of kmp_compile. Perhaps it has been fixed
in the CVS version, but I want to get the fix into your upcoming release of

In boost/regex/detail/regex_kmp.hpp, the kmp_compile function has a mistake in
the calculations of the allocation size for kmp_info and its pstr pointer. The
kmp_next member of kmp_info is an array of std::ptrdiff_t elements. The
calculations use sizeof(int) instead of sizeof(std::ptrdiff_t) to allow for the
array. On my alpha processor, std::ptrdiff_t is 8 bytes, while int is 4 bytes.

I haven't confirmed that my fix works, but I am fairly confident that this is a
bug, and I want to make sure you have time to get it into 1.29.0:

diff -u -r1.1.1.1 regex_kmp.hpp
--- boost/regex/detail/regex_kmp.hpp 2002/06/19 18:25:34
+++ boost/regex/detail/regex_kmp.hpp 2002/09/04 19:38:27
@@ -61,7 +61,7 @@
    i = 0;
    m = boost::re_detail::distance(first, last);
- std::size_t size = sizeof(kmp_info<charT>) + sizeof(int)*m +
+ std::size_t size = sizeof(kmp_info<charT>) + sizeof(std::ptrdiff_t)*m +
    // allocate struct and fill it in:
@@ -70,7 +70,7 @@
    pinfo->size = size;
    pinfo->len = m;
- charT* p = reinterpret_cast<charT*>(reinterpret_cast<char*>(pinfo) +
sizeof(kmp_info<charT>) + sizeof(int)*(m+1));
+ charT* p = reinterpret_cast<charT*>(reinterpret_cast<char*>(pinfo) +
sizeof(kmp_info<charT>) + sizeof(std::ptrdiff_t)*(m+1));
    pinfo->pstr = p;
    while(first != last)

While looking at this, I noticed that your calculations allow for m+1 additional
entries in the kmp_next array. Since kmp_info already includes 1 entry, perhaps
this is overkill. I only see initialization of n+1 entries in this routine, not
n+2. I leave it to you to determine whether you could save that extra 4 or 8

Dick Hadsell			914-259-6320  Fax: 914-259-6499
Reply-to:			hadsell_at_[hidden]
Blue Sky Studios      
44 South Broadway, White Plains, NY 10601

Boost list run by bdawes at, gregod at, cpdaniel at, john at