[CDRIVER-2618] ASSERT_CMP macros should copy argument values Created: 18/Apr/18  Updated: 28/Oct/23  Resolved: 15/Jun/18

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: None
Fix Version/s: 1.11.0

Type: Improvement Priority: Minor - P4
Reporter: Kevin Albertson Assignee: Spencer Mckenney
Resolution: Fixed Votes: 0
Labels: neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

The ASSERT_CMP macros like ASSERT_CMPINT use the arguments that are being compared directly in the error message.

For example, the ASSERT_CMPINT approximately looks like:

#define ASSERT_CMPINT(a, b)                  \
if (a != b) {                                \
   printf("assert failure: %d != %d", a, b); \
   abort();                                  \ 
} 

The caveat is that if a or b are function calls (that aren't idempotent), the error message might be misleading.

Here's an example of one such case:

ASSERT_CMPINT (
    0, ==, bson_json_reader_read (reader, &bson_out, &error));

Since b is substituted for that function call, that function gets called twice, and the error message could produce something confusing like "0 != 0".



 Comments   
Comment by Githook User [ 15/Jun/18 ]

Author:

{'username': 'spencemc', 'name': 'Spencer McKenney', 'email': 'spencermck@me.com'}

Message: CDRIVER-2618 assert_cmp macros copy arguments
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/2b55a7f1b3eb2e42c03efbf2aea60f3a241f41f0

Comment by Spencer Mckenney [ 14/Jun/18 ]

CR: https://mongodbcr.appspot.com/212080001/

Generated at Wed Feb 07 21:15:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.