Skip to content

Commit

Permalink
Merge pull request #49 from alanjian85/master
Browse files Browse the repository at this point in the history
Eliminate warnings caused by the result of fread
  • Loading branch information
jserv committed Jul 12, 2022
2 parents 2c73d9f + 0d712b9 commit ab6a0fe
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 8 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static Response *staticHandler(Request *req)
// GET LENGTH
char *buff;
char lens[25];
size_t len;
size_t len, readLen;

fseek(file, 0, SEEK_END);
len = ftell(file);
Expand All @@ -128,7 +128,13 @@ static Response *staticHandler(Request *req)
Response *response = responseNew();

buff = malloc(sizeof(char) * len);
fread(buff, sizeof(char), len, file);
readLen = fread(buff, sizeof(char), len, file);
if (readLen != len) {
fclose(file);
free(buff);
responseDel(response);
return NULL;
}
responseSetBody(response, bsNewLen(buff, len));
fclose(file);
free(buff);
Expand Down
11 changes: 9 additions & 2 deletions src/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ char *templateRender(Template *template)
char *pos, *buff, *incBs, *val;
char *segment;
bool rep = false;
size_t len;
size_t len, readLen;

if (!file) {
fprintf(stderr, "error: template '%s' not found\n", template->filename);
Expand All @@ -50,7 +50,14 @@ char *templateRender(Template *template)
rewind(file);

buff = malloc(sizeof(char) * (len + 2));
fread(buff + 1, sizeof(char), len, file);
readLen = fread(buff + 1, sizeof(char), len, file);
if (readLen != len) {
if (feof(file))
fprintf(stderr, "error: unexpected end of file in template '%s'\n", template->filename);
else
fprintf(stderr, "error: failed to read template '%s'\n", template->filename);
exit(1);
}
fclose(file);

buff[0] = ' ';
Expand Down

0 comments on commit ab6a0fe

Please sign in to comment.