Index | Thread | Search

From:
Jan Schreiber <jes@posteo.de>
Subject:
Re: httpd: add missing NULL check
To:
tech@openbsd.org
Date:
Wed, 01 Apr 2026 19:20:18 +0000

Download raw body.

Thread
Hi,

last diff didn't conform to style(9), sorry
I extracted the open for the gz file descriptor and it's now one level less
indentation.

Jan


diff --git usr.sbin/httpd/server_file.c usr.sbin/httpd/server_file.c
index cdcc11cd800..2c8aa9ce645 100644
--- usr.sbin/httpd/server_file.c
+++ usr.sbin/httpd/server_file.c
@@ -188,26 +188,31 @@ server_file_access(struct httpd *env, struct 
client *clt,
                                 return (500);
                         }

-                       if ((gzfd = open(gzpath, O_RDONLY)) != -1) {
-                               /* .gz must be a file, and not older */
-                               if (fstat(gzfd, &gzst) != -1 &&
-                                   S_ISREG(gzst.st_mode) &&
-                                   timespeccmp(&gzst.st_mtim, &st.st_mtim,
-                                   >=)) {
-  kv_add(&resp->http_headers,
-                                           "Content-Encoding", "gzip");
+                       gzfd = open(gzpath, O_RDONLY);
+                       if (gzfd == -1) {
+                               goto done;
+                       }
+
+                       /* .gz must be a file, and not older */
+                       if (fstat(gzfd, &gzst) != -1 &&
+                           S_ISREG(gzst.st_mode) &&
+                           timespeccmp(&gzst.st_mtim, &st.st_mtim, >=)) {
+                               if (kv_add(&resp->http_headers,
+                                   "Content-Encoding", "gzip") == NULL) {
+                                       close(gzfd);
+                               } else {
                                         /* Use original file timestamp */
                                         gzst.st_mtim = st.st_mtim;
                                         st = gzst;
                                         close(fd);
                                         fd = gzfd;
-                               } else {
-                                       close(gzfd);
                                 }
+                       } else {
+                               close(gzfd);
                         }
                 }
         }
-
+done:
         return (server_file_request(env, clt, media, fd, &st));
  }

On 4/1/26 20:47, Jan Schreiber wrote:
> Hello,
>
> if the kv_add call fails a gzipped response would be send without the 
> gzip header.
> With this change the fallback is to send an uncompressed response.
>
> Jan
>
> diff --git usr.sbin/httpd/server_file.c usr.sbin/httpd/server_file.c
> index cdcc11cd800..d740675f28d 100644
> --- usr.sbin/httpd/server_file.c
> +++ usr.sbin/httpd/server_file.c
> @@ -194,13 +194,16 @@ server_file_access(struct httpd *env, struct 
> client *clt,
>                                     S_ISREG(gzst.st_mode) &&
> timespeccmp(&gzst.st_mtim, &st.st_mtim,
>                                     >=)) {
> -  kv_add(&resp->http_headers,
> -                                           "Content-Encoding", "gzip");
> -                                       /* Use original file timestamp */
> -                                       gzst.st_mtim = st.st_mtim;
> -                                       st = gzst;
> -                                       close(fd);
> -                                       fd = gzfd;
> +                                       if (kv_add(&resp->http_headers,
> +                                           "Content-Encoding", 
> "gzip") == NULL) {
> +                                               close(gzfd);
> +                                       } else {
> +                                               /* Use original file 
> timestamp */
> +                                               gzst.st_mtim = 
> st.st_mtim;
> +                                               st = gzst;
> +                                               close(fd);
> +                                               fd = gzfd;
> +                                       }
>                                 } else {
>                                         close(gzfd);
>                                 }
>