[CDRIVER-2028] mongoc_gridfs_file_get_upload_date() returns arbitrary values on 32-bit system Created: 04/Feb/17  Updated: 16/May/17  Resolved: 09/May/17

Status: Closed
Project: C Driver
Component/s: GridFS
Affects Version/s: 1.5.3
Fix Version/s: 1.7.0

Type: Bug Priority: Major - P3
Reporter: Arseny Vakhrushev Assignee: A. Jesse Jiryu Davis
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: File libmongoc6.c    

 Description   

I am observing strange behaviour in mongoc_gridfs_file_get_upload_date() in my 32-bit chroot system. On a 64-bit system, it seems to return credible results. On a 32-bit system, the returned results barely resemble "milliseconds since the UNIX epoch", if at all.

The code:

#include <mongoc.h>
 
int main() {
	mongoc_client_t *client;
	mongoc_gridfs_t *gridfs;
	mongoc_gridfs_file_t *file;
	bson_error_t error;
	int64_t t;
 
	mongoc_init();
 
	client = mongoc_client_new("mongodb://127.0.0.1");
	BSON_ASSERT(client);
	gridfs = mongoc_client_get_gridfs(client, "test-gridfs", 0, &error);
	BSON_ASSERT(gridfs);
 
	file = mongoc_gridfs_create_file(gridfs, 0);
	BSON_ASSERT(file);
 
	t = mongoc_gridfs_file_get_upload_date(file);
	printf("upload_date: %lld\n", t); // This fails on "bad days" :-)
	BSON_ASSERT(t > 0);
 
	mongoc_gridfs_file_destroy(file);
	mongoc_gridfs_destroy(gridfs);
	mongoc_client_destroy(client);
	mongoc_cleanup();
	return 0;
}

The output on a 64-bit system:

$ ./a.out
upload_date: 1486175015000
$ ./a.out
upload_date: 1486175016000
$ ./a.out
upload_date: 1486175017000
$ ./a.out
upload_date: 1486175018000

Seems legit - ascending 64-bit millisecond timestamp value every time the program is run.

The output on a 32-bit system:

$ ./a.out
upload_date: 116451584
$ ./a.out
upload_date: 116452584
$ ./a.out
upload_date: 116454584
$ ./a.out
upload_date: 116457584
$ ./a.out
upload_date: 116458584
$ ./a.out
upload_date: 116460584
$ ./a.out
upload_date: 116461584

Doesn't look like an ascending 64-bit millisecond timestamp at all. On "bad days", values can even be negative as some arbitrary internal bit distortion takes place.



 Comments   
Comment by Githook User [ 16/May/17 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-2028 32-bit overflow in GridFS uploadDate
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/d065d85036a91f80b539047e7e02323f950fce26

Comment by A. Jesse Jiryu Davis [ 13/Feb/17 ]

Should be:

file->upload_date = ((int64_t)time (NULL)) * 1000;

Comment by A. Jesse Jiryu Davis [ 13/Feb/17 ]

Looks like a bug in _mongoc_gridfs_file_new, probably here:

file->upload_date = time (NULL) * 1000;

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