bugGNU source-highlight - Bugs: bug #64722, Write errors are ignored

 
 

bug #64722: Write errors are ignored

Submitter:  avih <avih>
Submitted:  Wed 27 Sep 2023 08:11:59 PM UTC
   
 
Category:  None Severity:  3 - Normal
Item Group:  None Status:  None
Privacy:  Public Assigned to:  None
Open/Closed:  Open
* Mandatory Fields

Add a New Comment Rich Markup
   

Thu 28 Sep 2023 01:45:16 PM UTC, comment #1: 

While the posted patch is minimal and exits loudly on all write errors, here's a patch which I'll be using in my own windows build, which tries to detect a SIGPIPE-like scenario, by testing that outputBuff is stdout (cout), and that stdout is a pipe (in C, because I don't know how to do that in C++).

If it detects a write error to a pipe, then it exits quietly with code 128+SIGPIPE:


diff --git a/lib/srchilite/bufferedoutput.cpp b/lib/srchilite/bufferedoutput.cpp
index 18e614e..664bab2 100644
--- a/lib/srchilite/bufferedoutput.cpp
+++ b/lib/srchilite/bufferedoutput.cpp
@@ -4,10 +4,14 @@
 // Copyright: See COPYING file that comes with this distribution
 //

+#include <sys/stat.h>
+#include <iostream>
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif

+#include "ioexception.h"
 #include "bufferedoutput.h"

 namespace srchilite {
@@ -26,6 +30,22 @@ void BufferedOutput::output(const std::string &s) {

     if (alwaysFlush)
         outputBuff << flush;
+
+    if (outputBuff.bad()) {
+        struct stat sb;
+
+        if (&outputBuff == &cout &&
+            fstat(1, &sb) == 0 &&
+            (sb.st_mode & S_IFMT) == S_IFIFO)
+        {
+            // write error to outputBuff, which is cout, which is a pipe.
+            // likely a SIGPIPE case, on a system without it - like windows.
+            // emulate SIGPIPE quietly.
+            exit(128 + 13);  // 128+SIGPIPE
+        }
+
+        throw IOException("cannot write", "");
+    }
 }

 void BufferedOutput::postLineInsert(const std::string &s) {


It would be nicer to do that in pure C++, and possibly even detect the case more accurately, but I don't know how to do that.

I can test patches on windows though, if you have other suggested solutions.

avih <avih>
Wed 27 Sep 2023 08:11:59 PM UTC, original submission:  

While write errors are not common, they can still happen, e.g. with full disk, and currently they're ignored, so it's probably generally useful to do something about it - like abort.

There's one case where it has a practical implication though, and that's when writing to a pipe which gets closed.

On *nix it probably closes due to SIGPIPE, but on windows apparently it keeps running until all the input is highlighted.

If the input is big, then it can take noticeable time until it closes while trying to write to a closed pipe.

Practical test case of this is piping into `less` on Windows - which only dequeues the pipe according to the user progress while paging the file.

So if piping into less and then quitting less (before the user paged to the end of the input), it will keep going until all the input is highlighted.

Attached is a suggested git patch to solve it. There are probably other solutions with similar effect.

avih <avih>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

Attached Files
file #55174:  0001-output-exit-on-write-error.patch added by avih (2KiB - application/octet-stream)

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by avih (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2023-09-27 avih Attached File- Added 0001-output-exit-on-write-error.patch, #55174

    Back to the top

    Powered by Savane 3.13-4448.
    Corresponding source code