diff --git a/src/seeder/dns.cpp b/src/seeder/dns.cpp --- a/src/seeder/dns.cpp +++ b/src/seeder/dns.cpp @@ -81,11 +81,6 @@ } // add dot in output if (!init) { - // The maximum size of a query name is 255. The buffer must have - // room for at least the '.' and a valid non-'.' character - if (bufused > 253) { - return -1; - } if (bufused == bufsize - 1) { return -2; } @@ -109,9 +104,15 @@ if (octet > 63) { return -1; } + // The maximum size of a query name is 255. The buffer must have + // room for the null-character at the end of the buffer after writing + // the label. + if (octet + bufused > 255) { + return -1; + } // copy label while (octet) { - if (*inpos == inend || bufused > 254) { + if (*inpos == inend) { return -1; } if (bufused == bufsize - 1) { diff --git a/src/seeder/test/dns_tests.cpp b/src/seeder/test/dns_tests.cpp --- a/src/seeder/test/dns_tests.cpp +++ b/src/seeder/test/dns_tests.cpp @@ -129,9 +129,13 @@ CheckParseName(maxLengthQName); // Check that a query name that is too long causes an error + std::string overSizedQName = maxLengthQName; + // Split the last label into two while adding an extra character to make + // sure the function does not error because of an oversized label + overSizedQName.insert(overSizedQName.end() - 3, '.'); // Allocates an extra large buffer to guarantee an error is not caused by // the buffer size - CheckParseNameError(maxLengthQName + "a", -1, 2 * maxLengthQName.size()); + CheckParseNameError(overSizedQName, -1, 2 * overSizedQName.size()); } BOOST_AUTO_TEST_SUITE_END()