fixing common off-by-one error with respect to maximum message size
[oweals/gnunet.git] / src / fs / fs.c
index 34850f14cb38fae22802e5b330cb780a7c0e25b6..9ec35ed2f746ffd3a215ee909ed0a1758992cd74 100644 (file)
@@ -583,11 +583,14 @@ GNUNET_FS_remove_sync_file_ (struct GNUNET_FS_Handle *h,
       return;
     }
   filename = get_serialization_file_name (h, ext, ent);
-  if (0 != UNLINK (filename))
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                             "unlink", 
-                             filename);
-  GNUNET_free (filename);
+  if (filename != NULL)
+    {
+      if (0 != UNLINK (filename))
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                 "unlink", 
+                                 filename);
+      GNUNET_free (filename);
+    }
 }
 
 
@@ -614,11 +617,14 @@ remove_sync_file_in_dir (struct GNUNET_FS_Handle *h,
       return;
     }
   filename = get_serialization_file_name_in_dir (h, ext, uni, ent);
-  if (0 != UNLINK (filename))
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                             "unlink", 
-                             filename);
-  GNUNET_free (filename);
+  if (filename != NULL)
+    {
+      if (0 != UNLINK (filename))
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                 "unlink", 
+                                 filename);
+      GNUNET_free (filename);
+    }
 }
 
 
@@ -1030,6 +1036,8 @@ make_serialization_file_name (struct GNUNET_FS_Handle *h,
   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
     return NULL; /* persistence not requested */
   dn = get_serialization_file_name (h, ext, "");
+  if (dn == NULL)
+    return NULL;
   if (GNUNET_OK !=
       GNUNET_DISK_directory_create_for_file (dn))
     {
@@ -1067,6 +1075,8 @@ make_serialization_file_name_in_dir (struct GNUNET_FS_Handle *h,
   if (0 == (h->flags & GNUNET_FS_FLAGS_PERSISTENCE))
     return NULL; /* persistence not requested */
   dn = get_serialization_file_name_in_dir (h, ext, uni, "");
+  if (dn == NULL)
+    return NULL;
   if (GNUNET_OK !=
       GNUNET_DISK_directory_create_for_file (dn))
     {
@@ -1271,18 +1281,23 @@ GNUNET_FS_file_information_sync_ (struct GNUNET_FS_FileInformation * fi)
   if (GNUNET_OK !=
       GNUNET_BIO_write_close (wh))
     {
+      wh = NULL;
       GNUNET_break (0);
       goto cleanup;
     }
   return; /* done! */
  cleanup:
-  (void) GNUNET_BIO_write_close (wh);
+  if (wh != NULL)
+    (void) GNUNET_BIO_write_close (wh);
   GNUNET_free_non_null (chks);
   GNUNET_free_non_null (ksks);
   fn = get_serialization_file_name (fi->h, GNUNET_FS_SYNC_PATH_FILE_INFO, fi->serialization);
-  if (0 != UNLINK (fn))
-    GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
-  GNUNET_free (fn);
+  if (NULL != fn)
+    {
+      if (0 != UNLINK (fn))
+       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink", fn);
+      GNUNET_free (fn);
+    }
   GNUNET_free (fi->serialization);
   fi->serialization = NULL;  
 }
@@ -1332,6 +1347,7 @@ find_file_position (struct GNUNET_FS_FileInformation *pos,
  * @param uri pointer to the keywords that will be used for this entry (can be modified)
  * @param anonymity pointer to selected anonymity level (can be modified)
  * @param priority pointer to selected priority (can be modified)
+ * @param do_index should we index?
  * @param expirationTime pointer to selected expiration time (can be modified)
  * @param client_info pointer to client context set upon creation (can be modified)
  * @return GNUNET_OK to continue (always)
@@ -1344,6 +1360,7 @@ fip_signal_resume(void *cls,
                  struct GNUNET_FS_Uri **uri,
                  uint32_t *anonymity,
                  uint32_t *priority,
+                 int *do_index,
                  struct GNUNET_TIME_Absolute *expirationTime,
                  void **client_info)
 {
@@ -1744,11 +1761,20 @@ GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
        }
       fn = GNUNET_DISK_mktemp (dir);
       GNUNET_free (dir);
+      if (fn == NULL)
+       return;
       dc->serialization = get_serialization_short_name (fn);
     }
   else
     {
       fn = get_download_sync_filename (dc, dc->serialization, "");
+      if (fn == NULL)
+       {
+         GNUNET_free (dc->serialization);
+         dc->serialization = NULL;
+         GNUNET_free (fn);
+         return;
+       }
     }
   wh = GNUNET_BIO_write_open (fn);
   if (wh == NULL)
@@ -1810,6 +1836,7 @@ GNUNET_FS_download_sync_ (struct GNUNET_FS_DownloadContext *dc)
       goto cleanup;
     }
   GNUNET_free_non_null (uris);
+  uris = NULL;
   if (GNUNET_OK !=
       GNUNET_BIO_write_close (wh))
     {
@@ -2221,19 +2248,22 @@ deserialize_search_result (void *cls,
       drh = get_read_handle (sc->h, 
                             GNUNET_FS_SYNC_PATH_CHILD_DOWNLOAD,
                             download);
-      deserialize_download (sc->h,
-                           drh,
-                           NULL,
-                           sr,
-                           download);
-      if (GNUNET_OK !=
-         GNUNET_BIO_read_close (drh, &emsg))
+      if (drh != NULL)
        {
-         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                     _("Failed to resume sub-download `%s': %s\n"),
-                     download,
-                     emsg);
-         GNUNET_free (emsg);
+         deserialize_download (sc->h,
+                               drh,
+                               NULL,
+                               sr,
+                               download);
+         if (GNUNET_OK !=
+             GNUNET_BIO_read_close (drh, &emsg))
+           {
+             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                         _("Failed to resume sub-download `%s': %s\n"),
+                         download,
+                         emsg);
+             GNUNET_free (emsg);
+           }
        }
       GNUNET_free (download);
     }
@@ -2242,18 +2272,21 @@ deserialize_search_result (void *cls,
       drh = get_read_handle (sc->h, 
                             GNUNET_FS_SYNC_PATH_CHILD_SEARCH,
                             update_srch);
-      deserialize_search (sc->h,
-                         drh,
-                         sr,
-                         update_srch);
-      if (GNUNET_OK !=
-         GNUNET_BIO_read_close (drh, &emsg))
+      if (drh != NULL)
        {
-         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-                     _("Failed to resume sub-search `%s': %s\n"),
-                     update_srch,
-                     emsg);
-         GNUNET_free (emsg);
+         deserialize_search (sc->h,
+                             drh,
+                             sr,
+                             update_srch);
+         if (GNUNET_OK !=
+             GNUNET_BIO_read_close (drh, &emsg))
+           {
+             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                         _("Failed to resume sub-search `%s': %s\n"),
+                         update_srch,
+                         emsg);
+             GNUNET_free (emsg);
+           }
        }
       GNUNET_free (update_srch);      
     }
@@ -2261,6 +2294,15 @@ deserialize_search_result (void *cls,
                                     &sr->key,
                                     sr,
                                     GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_close (rh, &emsg))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failure while resuming search operation `%s': %s\n"),
+                 filename,
+                 emsg);
+      GNUNET_free (emsg);
+    }
   return GNUNET_OK;
  cleanup:
   GNUNET_free_non_null (download);
@@ -2272,6 +2314,15 @@ deserialize_search_result (void *cls,
     GNUNET_CONTAINER_meta_data_destroy (sr->meta);
   GNUNET_free (sr->serialization);
   GNUNET_free (sr);  
+  if (GNUNET_OK !=
+      GNUNET_BIO_read_close (rh, &emsg))
+    {
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                 _("Failure while resuming search operation `%s': %s\n"),
+                 filename,
+                 emsg);
+      GNUNET_free (emsg);
+    }
   return GNUNET_OK;
 }
 
@@ -2798,7 +2849,8 @@ deserialize_search_file (void *cls,
       return GNUNET_OK;
     }
   sc = deserialize_search (h, rh, NULL, ser);
-  sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
+  if (sc != NULL)
+    sc->top = GNUNET_FS_make_top (h, &GNUNET_FS_search_signal_suspend_, sc);
   GNUNET_free (ser);
   if (GNUNET_OK !=
       GNUNET_BIO_read_close (rh, &emsg))