ngtcp2: Fix example server build on 32-bit targets

The example server keeps the file size in a uint64_t and uses it as the
size of a std::span, which clang rejects as narrowing on targets with a
32-bit size_t, such as riscv32:

  examples/server.cc:236:54: error: non-constant-expression cannot be
  narrowed from type 'uint64_t' (aka 'unsigned long long') to
  'size_type' (aka 'unsigned int') in initializer list [-Wc++11-narrowing]

Add a patch that stores the length as a size_t and rejects files too
large to map instead of truncating their size.

AI-Generated: Uses Claude Code
Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
This commit is contained in:
Khem Raj committed 2026-10-21 23:40:17 -07:00
1 parent c2a6b2a499
commit 2344409c57
2 files changed
+66 -1

No files matched your search

@@ -0,0 +1,63 @@
From: Khem Raj <khem.raj@oss.qualcomm.com>
Date: Wed, 21 Oct 2026 09:00:00 -0700
Subject: [PATCH] examples/server: Do not truncate file sizes on 32-bit
targets
FileEntry::len is a uint64_t, but it is used for mmap() and as the size
of the resp_data std::span, both of which take a size_t. On targets
where size_t is 32 bits, such as riscv32, clang rejects the narrowing in
the span initializer:
examples/server.cc:236:54: error: non-constant-expression cannot be
narrowed from type 'uint64_t' (aka 'unsigned long long') to
'size_type' (aka 'unsigned int') in initializer list [-Wc++11-narrowing]
The value is already truncated earlier, where st_size is cast to size_t
before mmap(), so a file larger than the address space would be served
with the wrong length.
Store the length as a size_t, and fail open_file() with
Error::INTEGER_OVERFLOW when the file is too large to map, instead of
truncating its size.
Upstream-Status: Submitted [https://github.com/ngtcp2/ngtcp2/pull/2330]
Signed-off-by: Khem Raj <khem.raj@oss.qualcomm.com>
---
examples/server.cc | 7 +++++++
examples/server.h | 2 +-
2 files changed, 8 insertions(+), 1 deletion(-)
--- a/examples/server.cc
+++ b/examples/server.cc
@@ -31,6 +31,8 @@
#include <memory>
#include <fstream>
#include <iomanip>
+#include <limits>
+#include <utility>
#include <unistd.h>
#include <getopt.h>
@@ -216,6 +218,11 @@
close(fd);
} else {
fe.fd = fd;
+ // A file larger than the address space cannot be mapped.
+ if (std::cmp_greater(st.st_size, std::numeric_limits<size_t>::max())) {
+ close(fd);
+ return std::unexpected{Error::INTEGER_OVERFLOW};
+ }
fe.len = static_cast<size_t>(st.st_size);
if (fe.len) {
fe.map = mmap(nullptr, fe.len, PROT_READ, MAP_SHARED, fd, 0);
--- a/examples/server.h
+++ b/examples/server.h
@@ -68,7 +68,7 @@
};
struct FileEntry {
- uint64_t len{};
+ size_t len{};
void *map{};
int fd{};
uint8_t flags{};
@@ -4,7 +4,9 @@ BUGTRACKER = "https://github.com/ngtcp2/ngtcp2/issues"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://COPYING;md5=de0966c8ff4f62661a3da92967a75434"
SRC_URI = "gitsm://github.com/ngtcp2/ngtcp2;protocol=https;branch=main;tag=v${PV}"
SRC_URI = "gitsm://github.com/ngtcp2/ngtcp2;protocol=https;branch=main;tag=v${PV} \
file://0001-examples-server-Do-not-truncate-file-sizes-on-32-bit.patch \
"
SRCREV = "f9e9ff01ad2c8116bc09de4f644b0028a61486a6"
DEPENDS = "brotli libev nghttp3"