Skip to content

Commit

Permalink
Merge pull request #332 from zevv/328
Browse files Browse the repository at this point in the history
handle fwrite() errors more gracefully
  • Loading branch information
l8gravely committed Sep 4, 2024
2 parents e66302f + 1b64bc4 commit a58fa4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/duc/cmd-cgi.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,11 @@ static void include_file(const char *fname)
printf("<!-- start include -->\n");
for(;;) {
char buf[4096];
size_t n = fread(buf, 1, sizeof(buf), f);
if(n == 0) break;
fwrite(buf, 1, n, stdout);
size_t in = fread(buf, 1, sizeof(buf), f);
if(in == 0) break;
size_t out = fwrite(buf, 1, in, stdout);
if(out == 0) break;
if(out < in) break;
}
printf("<!-- end include -->\n");
fclose(f);
Expand Down
4 changes: 3 additions & 1 deletion src/duc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ static void printi(const char *s, int pos, int indent, int max)
const char *p1 = s;
const char *p2 = s;
int i;
int out;

while(*p1) {
while(*p2 && !isspace(*p2)) p2++;
Expand All @@ -222,7 +223,8 @@ static void printi(const char *s, int pos, int indent, int max)
pos = indent;
}

fwrite(p1, l, 1, stdout);
out = fwrite(p1, l, 1, stdout);
if (out == 0) break;
while(*p2 && isspace(*p2)) p2++;
pos += l;
if(pos < max) {
Expand Down
5 changes: 4 additions & 1 deletion src/libduc-graph/graph-cairo.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ struct cairo_backend_data {
static cairo_status_t cairo_writer(void *closure, const unsigned char *data, unsigned int length)
{
FILE *f = closure;
fwrite(data, length, 1, f);
size_t out = fwrite(data, length, 1, f);
if (out == 0 || out < length) {
return CAIRO_STATUS_WRITE_ERROR;
}
return CAIRO_STATUS_SUCCESS;
}

Expand Down

0 comments on commit a58fa4e

Please sign in to comment.