]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
Refactor finish_document_data to use goto better
authorAZero13 <gfunni234@gmail.com>
Mon, 3 Nov 2025 23:47:08 +0000 (18:47 -0500)
committerAZero13 <gfunni234@gmail.com>
Tue, 4 Nov 2025 00:01:43 +0000 (19:01 -0500)
tools/ippeveprinter.c

index 6b04247ee36ae7f88c7d4c2cda28be9bd26fff33..d6946a883b2ecabee74b637c46eed4c32f1f9b5e 100644 (file)
@@ -2070,7 +2070,6 @@ finish_document_data(
   if ((job->fd = create_job_file(job, filename, sizeof(filename), client->printer->directory, NULL)) < 0)
   {
     respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to create print file: %s", strerror(errno));
-
     goto abort_job;
   }
 
@@ -2081,43 +2080,22 @@ finish_document_data(
   {
     if (write(job->fd, buffer, (size_t)bytes) < bytes)
     {
-      int error = errno;               // Write error
-
-      close(job->fd);
-      job->fd = -1;
-
-      unlink(filename);
-
-      respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to write print file: %s", strerror(error));
-
-      goto abort_job;
+      respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to write print file: %s", strerror(errno));
+      goto cleanup_and_abort;
     }
   }
 
   if (bytes < 0)
   {
-    // Got an error while reading the print data, so abort this job.
-    close(job->fd);
-    job->fd = -1;
-
-    unlink(filename);
-
     respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to read print file.");
-
-    goto abort_job;
+    goto cleanup_and_abort;
   }
 
+  // Close the file descriptor
   if (close(job->fd))
   {
-    int error = errno;                 // Write error
-
-    job->fd = -1;
-
-    unlink(filename);
-
-    respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to write print file: %s", strerror(error));
-
-    goto abort_job;
+    respond_ipp(client, IPP_STATUS_ERROR_INTERNAL, "Unable to close print file: %s", strerror(errno));
+    goto cleanup_and_abort;
   }
 
   job->fd       = -1;
@@ -2151,6 +2129,17 @@ finish_document_data(
   cupsArrayDelete(ra);
   return;
 
+  // Cleanup file on error, then abort job...
+  cleanup_and_abort:
+
+  if (job->fd >= 0)
+  {
+    close(job->fd);
+    job->fd = -1;
+  }
+
+  unlink(filename);
+
   // If we get here we had to abort the job...
   abort_job: