[CDRIVER-4091] Truncation in strncpy() causes build error Created: 16/Jul/21  Updated: 28/Oct/23  Resolved: 26/Jul/21

Status: Closed
Project: C Driver
Component/s: Build, errors
Affects Version/s: None
Fix Version/s: 1.19.0

Type: Bug Priority: Unknown
Reporter: Jesse Williamson (Inactive) Assignee: Jesse Williamson (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

gcc complains that because the destination length is computed based on the source string, the trailing NULL won't be copied:

test->cursor_ns = (char *) bson_malloc0 (strlen (temp));
strncpy (test->cursor_ns, temp, strlen (temp));

 

 



 Comments   
Comment by Githook User [ 19/Jul/21 ]

Author:

{'name': 'Jesse Williamson', 'email': 'nerd.cpp@gmail.com', 'username': 'chardan'}

Message: CDRIVER-4091 - Truncation in strncpy() causes build error (#829)

https://jira.mongodb.org/browse/CDRIVER-4091

gcc 9.3.0 complains that because the strncpy() call uses the source's size as a bound,
the trailing NULL will not be copied.

Replaced with:
updated allocation size of destination to include room for the NULL character
eliminated redundant srlen()
changed strncpy() call to strcpy(), which includes the trailing NULL

Signed-off-by: Jesse Williamson <jesse.williamson@mongodb.com>
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/7c12ebcf44a1ea08b3dc792a4bd17a9257dc2a21

Comment by Jesse Williamson (Inactive) [ 16/Jul/21 ]

https://github.com/mongodb/mongo-c-driver/pull/829

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