[CDRIVER-2575] misaligned address in mongoc-client.c Created: 28/Mar/18  Updated: 28/Oct/23  Resolved: 25/Apr/18

Status: Closed
Project: C Driver
Component/s: libmongoc
Affects Version/s: 1.9.3
Fix Version/s: 1.10.0

Type: Task Priority: Major - P3
Reporter: Jeroen Ooms [X] Assignee: Unassigned
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

UBSAN is reporting the following problem, even when I compile without BSON_EXTRA_ALIGN:

gridfs> fs <- gridfs(url = "mongodb+srv://readwrite:test@cluster0-84vdt.mongodb.net/test")
mongoc/mongoc-client.c:279:11: runtime error: load of misaligned address 0x7ffd21a4a479 for type 'short int', which requires 2 byte alignment
0x7ffd21a4a479: note: pointer points here
 00 00 00  00 69 89 1a 63 6c 75 73  74 65 72 30 2d 73 68 61  72 64 2d 30 30 2d 30 31  2d 38 34 76 64
              ^



 Comments   
Comment by Githook User [ 25/Apr/18 ]

Author:

{'email': 'kevin.albertson@10gen.com', 'username': 'kevinAlbs', 'name': 'Kevin Albertson'}

Message: CDRIVER-2575 memcpy buffer before casting
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/68359aeb5ecde3441e376a61b2d47af5eabbdd53

Comment by A. Jesse Jiryu Davis [ 05/Apr/18 ]

Need to memcpy from "data" to a local uint16_t before calling ntohs.

Comment by Jeroen Ooms [X] [ 29/Mar/18 ]

This case was using gcc 7.3.0 from debian unstable with:

gcc -std=gnu99 -fsanitize=undefined -fno-omit-frame-pointer

I get a lot of alignment errors if I compile with

-DBSON_EXTRA_ALIGN

but that is expected. However it should not give any alignment errors when compiling without this macro.

Also these sanitizers check at runtime so perhaps my examples are not hitting all libmongoc code.

Comment by A. Jesse Jiryu Davis [ 29/Mar/18 ]

Thanks Jeroen, which compiler version and flags please? We're a little confused because -fsanitize=address gives us many more warnings than this one.

Comment by Jeroen Ooms [X] [ 28/Mar/18 ]

I'm using 1.9.3 on Linux. So this is about this line in the C driver:

port = ntohs (*(short *) (data + 4));

Comment by Kevin Albertson [ 28/Mar/18 ]

Hi Jeroen, what version of the C driver are you using and what platform are you building it on?

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