[SERVER-7293] Useless/incorrect uassert in BSONElement constructor Created: 08/Oct/12  Updated: 08/Oct/12  Resolved: 08/Oct/12

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: 2.0.7, 2.2.0
Fix Version/s: None

Type: Bug Priority: Trivial - P5
Reporter: Tad Marshall Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Operating System: ALL
Participants:

 Description   

In src/mongo/bson/bsonelement.h at line 439 in today's master branch, a uassert tests the return value from strnlen() for -1. This function cannot return -1 under any conceivable circumstances, so the test is useless.

        // @param maxLen don't scan more than maxLen bytes
        explicit BSONElement(const char *d, int maxLen) : data(d) {
            if ( eoo() ) {
                totalSize = 1;
                fieldNameSize_ = 0;
            }
            else {
                totalSize = -1;
                fieldNameSize_ = -1;
                if ( maxLen != -1 ) {
                    int size = (int) strnlen( fieldName(), maxLen - 1 );
                    uassert( 10333 ,  "Invalid field name", size != -1 );
                    fieldNameSize_ = size + 1;
                }
            }
        }

The only way that strnlen() could return a value that when cast to int would be equal to -1 is if maxLen was zero and the "field name" consisted of 0xFFFFFFFF non-zero characters.

The test should be removed or replaced with something more useful.



 Comments   
Comment by Tad Marshall [ 08/Oct/12 ]

This is not a call to the 'strnlen' function you read about in gcc and MSVC documentation, it is a call to a local function that does return -1. Not a bug.

Generated at Thu Feb 08 03:14:07 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.