Uploaded image for project: 'C Driver'
  1. C Driver
  2. CDRIVER-2002

Given mongoc_gridfs_file_readv() returning -1 (failure), mongoc_gridfs_file_error() doesn't indicate error condition

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 1.7.0
    • Affects Version/s: 1.5.3
    • Component/s: GridFS
    • None

      There is a case when mongoc_gridfs_file_readv() returns -1 (failure), but the subsequent call to mongoc_gridfs_file_error() returns false indicating that there is no actual error.

      This seems to contradict with what the manual states about mongoc_gridfs_file_readv():

      Returns the number of bytes read, or -1 on failure. Use mongoc_gridfs_file_error to retrieve error details.

      Interestingly, but for mongoc_gridfs_file_writev(), it states that errno will be set:

      Returns the number of bytes written or -1 upon error and errno is set.

      Not sure how potential database failures will be propagated via errno, but at least I can't blame this one for now albeit it looks a bit inconsistent.

      Please see the example below.

      #include <mongoc.h>
      
      int main() {
      	mongoc_client_t *client;
      	mongoc_gridfs_t *gridfs;
      	mongoc_gridfs_file_t *file;
      	bson_error_t error;
      	ssize_t n;
      	char buf[] = "Some content for a new file";
      	mongoc_iovec_t iov = { .iov_base = buf, .iov_len = sizeof buf };
      
      	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); // Create file
      	BSON_ASSERT(file);
      	BSON_ASSERT(mongoc_gridfs_file_writev(file, &iov, 1, 0) == sizeof buf); // Write contents
      	BSON_ASSERT(mongoc_gridfs_file_seek(file, 0, SEEK_SET) == 0); // Set position to 0
      	BSON_ASSERT(mongoc_gridfs_file_save(file)); // Save file
      	BSON_ASSERT(mongoc_gridfs_file_remove(file, &error)); // Remove file
      
      	// Now the issue ....
      
      	// mongoc_gridfs_file_readv() fails as expected
      	BSON_ASSERT(mongoc_gridfs_file_readv(file, &iov, 1, sizeof buf, 0) == -1);
      
      	// Now it's time to check for the actual error, right?
      	// But the following will fail ...
      	BSON_ASSERT(mongoc_gridfs_file_error(file, &error));
      
      	// Similarly, mongoc_gridfs_file_writev() fails as expected
      	BSON_ASSERT(mongoc_gridfs_file_writev(file, &iov, 1, 0) == -1);
      
      	// The following will fail if uncommented. Maybe that's expected though.
      	// BSON_ASSERT(mongoc_gridfs_file_error(file, &error));
      
      
      	mongoc_gridfs_file_destroy(file);
      	mongoc_gridfs_destroy(gridfs);
      	mongoc_client_destroy(client);
      	mongoc_cleanup();
      	return 0;
      }
      

      The output:

      $ ./a.out
      libmongoc3.c:34 main(): precondition failed: mongoc_gridfs_file_error(file, &error)
      

            Assignee:
            jesse@mongodb.com A. Jesse Jiryu Davis
            Reporter:
            neoxic Arseny Vakhrushev
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: