Luukdegram / apple_pie

Basic HTTP server implementation in Zig
MIT License
163 stars 21 forks source link

Incorrect alignment when capturing u32 #76

Closed joachimschmidt557 closed 2 years ago

joachimschmidt557 commented 2 years ago

Steps to reproduce

Apply this patch to the router example:

diff --git a/examples/router.zig b/examples/router.zig
index 5b7d909..eabe465 100644
--- a/examples/router.zig
+++ b/examples/router.zig
@@ -28,7 +28,7 @@ pub fn main() !void {
             builder.get("/", null, index),
             builder.get("/headers", null, headers),
             builder.get("/files/*", null, serveFs),
-            builder.get("/hello/:name", []const u8, hello),
+            builder.get("/hello/:name", u32, hello),
             builder.get("/route", null, route),
             builder.get("/posts/:post/messages/:message", struct {
                 post: []const u8,
@@ -74,13 +74,13 @@ fn hello(ctx: *Context, resp: *http.Response, req: http.Request, captures: ?*con
     _ = req;
     _ = ctx;
     const name = @ptrCast(
-        *const []const u8,
+        *const u32,
         @alignCast(
-            @alignOf(*const []const u8),
+            @alignOf(*const u32),
             captures,
         ),
     );
-    try resp.writer().print("Hello {s}\n", .{name.*});
+    try resp.writer().print("Hello {}\n", .{name.*});
 }

 /// Serves a file

Expected behavior

joachim@dustbowl ~> curl http://localhost:8080/hello/123
Hello 123

Actual behavior

joachim@dustbowl ~> curl http://localhost:8080/hello/123
curl: (52) Empty reply from server
joachim@dustbowl ~ [52]> 

The server crashes with this backtrace:

thread 32959 panic: incorrect alignment
/home/joachim/src/apple_pie/examples/router.zig:80:13: 0x26b11b in hello (router)
            captures,
            ^
/home/joachim/src/apple_pie/src/router.zig:100:33: 0x2723be in .apple_pie.router.Router(*Context,[]const .apple_pie.router.Route(*Context){(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant)}).handle (router)
            return route.handler(ctx, res, req, @ptrCast(?*const anyopaque, &param));
                                ^
/home/joachim/src/apple_pie/src/router.zig:129:47: 0x270f15 in .apple_pie.router.Router(*Context,[]const .apple_pie.router.Route(*Context){(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant),(struct .apple_pie.router.Route(*Context) constant)}).serve (router)
                            return Self.handle(route, object.params[0..object.param_count], context, response, request);
                                              ^
/home/joachim/src/apple_pie/src/server.zig:113:26: 0x273092 in .apple_pie.server.Server.run (router)
                .frame = async client.run(gpa, &clients, context),
                         ^
/home/joachim/src/zig/lib/std/event/loop.zig:1415:25: 0x2b935d in std.event.loop.Loop.workerRun (router)
                        resume handle;
                        ^
/home/joachim/src/zig/lib/std/event/loop.zig:702:23: 0x25287d in std.event.loop.Loop.run (router)
        self.workerRun();
                      ^
/home/joachim/src/zig/lib/std/start.zig:488:21: 0x2243f9 in std.start.callMainWithArgs (router)
            loop.run();
                    ^
/home/joachim/src/zig/lib/std/start.zig:409:17: 0x222506 in std.start.posixCallMainAndExit (router)
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^
/home/joachim/src/zig/lib/std/start.zig:322:5: 0x222312 in std.start._start (router)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
The following command terminated unexpectedly:
cd /home/joachim/src/apple_pie && /home/joachim/src/apple_pie/zig-out/bin/router 
Luukdegram commented 2 years ago

Thanks to #80 this is no longer an issue.