Browse Source

Avoid leaking file descriptors on read error.

Some file descriptors might not be properly closed on read error. Those
will accumulate over time and might reach /proc/sys/fs/file-max
(currently set to 1M on my Ubuntu system).
newsoft 7 years ago
parent
commit
27be5f3d6a
1 changed files with 12 additions and 0 deletions
  1. 12 0
      src/handle_form.inl

+ 12 - 0
src/handle_form.inl

@@ -473,6 +473,10 @@ mg_handle_form_request(struct mg_connection *conn,
 						r = mg_read(conn, buf + (size_t)buf_fill, to_read);
 						if (r < 0) {
 							/* read error */
+							if (fstore.access.fp) {
+								mg_fclose(&fstore.access);
+								remove_bad_file(conn, path);
+							}
 							return -1;
 						}
 						if (r != (int)to_read) {
@@ -869,6 +873,10 @@ mg_handle_form_request(struct mg_connection *conn,
 				            sizeof(buf) - 1 - (size_t)buf_fill);
 				if (r < 0) {
 					/* read error */
+					if (fstore.access.fp) {
+						mg_fclose(&fstore.access);
+						remove_bad_file(conn, path);
+					}
 					mg_free(boundary);
 					return -1;
 				}
@@ -876,6 +884,10 @@ mg_handle_form_request(struct mg_connection *conn,
 				buf[buf_fill] = 0;
 				if (buf_fill < 1) {
 					/* No data */
+					if (fstore.access.fp) {
+						mg_fclose(&fstore.access);
+						remove_bad_file(conn, path);
+					}
 					mg_free(boundary);
 					return -1;
 				}