summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-10-14 16:37:18 +0200
committerAnton Kling <anton@kling.gg>2024-10-14 16:37:18 +0200
commit3fd4be7cb2744d852cf99e627f7bfea3fd83bd3c (patch)
tree0d2e2a9d5b85aaf631e551ba88be0930e715c711
parent3011c0589b8ab7cbfed127ce45a04f154441b406 (diff)
libc: Copy string views to preallocated buffers
-rw-r--r--userland/httpd/httpd.c8
-rw-r--r--userland/libc/include/tb/sv.h1
-rw-r--r--userland/libc/tb/sv.c9
3 files changed, 15 insertions, 3 deletions
diff --git a/userland/httpd/httpd.c b/userland/httpd/httpd.c
index 845355a..aeae6aa 100644
--- a/userland/httpd/httpd.c
+++ b/userland/httpd/httpd.c
@@ -107,9 +107,11 @@ void parse_incoming_request(struct http_request *request) {
return;
}
- char *p = SV_TO_C(path);
- request->file_fd = open(p, O_RDONLY);
- free(p);
+ {
+ char path_buffer[256];
+ sv_to_cstring_buffer(path, path_buffer, 256);
+ request->file_fd = open(path_buffer, O_RDONLY);
+ }
if (-1 == request->file_fd) {
request->status_code = 404;
return;
diff --git a/userland/libc/include/tb/sv.h b/userland/libc/include/tb/sv.h
index 3d4f9fb..e1ca453 100644
--- a/userland/libc/include/tb/sv.h
+++ b/userland/libc/include/tb/sv.h
@@ -17,6 +17,7 @@ struct sv {
};
char *SV_TO_C(struct sv s);
+size_t sv_to_cstring_buffer(struct sv s, char *buffer, size_t length);
struct sv sv_split_delim(const struct sv input, struct sv *rest, char delim);
struct sv sv_end_split_delim(const struct sv input, struct sv *rest,
char delim);
diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c
index 3f947ef..4ff24fb 100644
--- a/userland/libc/tb/sv.c
+++ b/userland/libc/tb/sv.c
@@ -11,6 +11,15 @@ char *SV_TO_C(struct sv s) {
return c_string;
}
+size_t sv_to_cstring_buffer(struct sv s, char *buffer, size_t length) {
+ if (0 == length || length - 1 < s.length) {
+ return s.length;
+ }
+ memcpy(buffer, s.s, s.length);
+ buffer[s.length] = '\0';
+ return s.length;
+}
+
struct sv sv_next(struct sv s) {
if (0 == s.length) {
return s;