summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <lukeshu@sbcglobal.net>2016-03-15 18:25:23 -0400
committerLuke Shumaker <lukeshu@sbcglobal.net>2016-03-15 18:42:24 -0400
commita18ffc0f109dcff76815eb7acfea67aeb9e42fe2 (patch)
tree5ff12fc52c735f97f0c21276e47cc04b96f45fc8
parent5b36bb025a300e39040eac6cf9cf87aae1f4e7a3 (diff)
fixity fix
-rw-r--r--.gitignore2
-rw-r--r--multipart-replace-http-server.c12
-rw-r--r--multipart-replace.c15
3 files changed, 19 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index 2e01979..52ef0a6 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,4 +3,4 @@
/multipart-replace-http-server
*.o
-.*.mk \ No newline at end of file
+.*.mk
diff --git a/multipart-replace-http-server.c b/multipart-replace-http-server.c
index a6a7870..2b30e0f 100644
--- a/multipart-replace-http-server.c
+++ b/multipart-replace-http-server.c
@@ -153,12 +153,12 @@ int sockstream_listen(const char *type, const char *addr) {
struct addrinfo *ai_addr = NULL;
struct addrinfo ai_hints = { 0 };
- char *col = strrchr(addr, ':');
+ char *host = strdupa(addr);
+ char *col = strrchr(host, ':');
if (col == NULL) {
errno = EINVAL;
return -EAI_SYSTEM;
}
- char *host = strdupa(addr);
char *port = &col[1];
*col = '\0';
if (host[0] == '\0')
@@ -169,7 +169,9 @@ int sockstream_listen(const char *type, const char *addr) {
ai_hints.ai_flags = AI_PASSIVE;
int r = getaddrinfo(host, port, &ai_hints, &ai_addr);
- if (r != 0)
+ if (r < 0)
+ return r;
+ if (r > 0)
return -r;
int sock = socket(ai_addr->ai_family, ai_addr->ai_socktype, ai_addr->ai_protocol);
@@ -264,7 +266,7 @@ void usage() {
}
int main(int argc, char *argv[]) {
- if (strcmp(argv[1], "-h") == 0) {
+ if (argc >=2 && strcmp(argv[1], "-h") == 0) {
usage();
return EXIT_SUCCESS;
}
@@ -281,7 +283,7 @@ int main(int argc, char *argv[]) {
error(1, 0, "Opening socket %s %s: %s", argv[1], argv[2], gai_strerror(-sock));
}
- for (int i = 2; i < argc; i++)
+ for (int i = 3; i < argc; i++)
file_add(argv[i]);
signal(SIGPIPE, SIG_IGN);
diff --git a/multipart-replace.c b/multipart-replace.c
index 30ae7bb..5f0ca4e 100644
--- a/multipart-replace.c
+++ b/multipart-replace.c
@@ -38,7 +38,6 @@ ssize_t safe_atoi(char *str) {
return atoi(str);
}
-#define rgetline() do { if ((line_len = getline(&line_buf, &line_cap, stream)) < 0) { error(0, 0, "source hung up"); return; } } while(0)
void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const char *boundary) {
FILE *stream = fdopen(fd, "r");
boundary = boundary_line(boundary);
@@ -52,7 +51,11 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
content_length = -1;
/* scan for the first non-empty line */
do {
- rgetline();
+ line_len = getline(&line_buf, &line_cap, stream);
+ if (line_len < 0) {
+ error(0, 0, "source hung up");
+ return;
+ }
} while (strcmp(line_buf, "\r\n") == 0);
/* make sure it matches the boundary separator */
if (strcmp(line_buf, boundary) != 0) {
@@ -61,7 +64,11 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
}
/* read the frame header (MIME headers) */
do {
- rgetline();
+ line_len = getline(&line_buf, &line_cap, stream);
+ if (line_len < 0) {
+ error(0, 0, "source hung up");
+ return;
+ }
/* append the line to the frame contents */
if ((ssize_t)s->back->cap < s->back->len + line_len)
s->back->buf = xrealloc(s->back->buf, s->back->cap = s->back->len + line_len);
@@ -80,7 +87,7 @@ void multipart_replace_reader(struct multipart_replace_stream *s, int fd, const
/* read the frame contents */
if ((ssize_t)s->back->cap < s->back->len + content_length)
s->back->buf = xrealloc(s->back->buf, s->back->cap = s->back->len + content_length);
- if (fread(s->back->buf, s->back->len, 1, stream) != 1) {
+ if (fread(&s->back->buf[s->back->len], content_length, 1, stream) != 1) {
error(0, ferror(stream), "fread(%zd)", s->back->len);
return;
}