Check for read errors in the open routine.
authorGuy Harris <guy@alum.mit.edu>
Sat, 7 Nov 2015 22:46:18 +0000 (14:46 -0800)
committerGuy Harris <guy@alum.mit.edu>
Sat, 7 Nov 2015 22:46:47 +0000 (22:46 +0000)
While we're at it, rename a variable to avoid colliding with the read()
routine.

Change-Id: I6629ec761f48751f34a2e7d04180d7583ad85710
Reviewed-on: https://code.wireshark.org/review/11626
Reviewed-by: Guy Harris <guy@alum.mit.edu>
wiretap/json.c

index 9e81f2e56d8e87c2921eb7aa3fb52d2472ab5c09..e7cf2d68a805003be13c304e7da359c661fb61da 100644 (file)
@@ -92,18 +92,29 @@ static gboolean json_read(wtap *wth, int *err, gchar **err_info, gint64 *data_of
     return json_read_file(wth, wth->fh, &wth->phdr, wth->frame_buffer, err, err_info);
 }
 
-wtap_open_return_val json_open(wtap *wth, int *err, gchar **err_info _U_)
+wtap_open_return_val json_open(wtap *wth, int *err, gchar **err_info)
 {
     guint8* filebuf;
-    guint read;
+    int bytes_read;
 
     filebuf = (guint8*)g_malloc0(MAX_FILE_SIZE);
     if (!filebuf)
         return WTAP_OPEN_ERROR;
 
-    read = file_read(filebuf, MAX_FILE_SIZE, wth->fh);
+    bytes_read = file_read(filebuf, MAX_FILE_SIZE, wth->fh);
+    if (bytes_read < 0) {
+        /* Read error. */
+        *err = file_error(wth->fh, err_info);
+        g_free(filebuf);
+        return WTAP_OPEN_ERROR;
+    }
+    if (bytes_read == 0) {
+        /* empty file, not *anybody's* */
+        g_free(filebuf);
+        return WTAP_OPEN_NOT_MINE;
+    }
 
-    if (jsmn_is_json(filebuf, read) == FALSE) {
+    if (jsmn_is_json(filebuf, bytes_read) == FALSE) {
         g_free(filebuf);
         return WTAP_OPEN_NOT_MINE;
     }