From ece8480fc2190229721bf1690116fa6d767f4c0e Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Tue, 16 Jul 2024 00:27:56 +0500 Subject: [PATCH 1/2] std.c.setlocale: fix return type to nullable pointer According to https://en.cppreference.com/mwiki/index.php?title=c/locale/setlocale&oldid=171500 , `setlocale` "returns null value on failure": > Return value > pointer to a narrow null-terminated string identifying the C locale > after applying the changes, if any, or null pointer on failure. Example program: ```zig const std = @import("std"); pub fn main() void { const ptr = std.c.setlocale(.ALL, "non_existent"); std.debug.print("ptr = {d}\n", .{@intFromPtr(ptr)}); } ``` Output: ```console ptr = 0 ``` Signed-off-by: Eric Joldasov --- lib/std/c.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/c.zig b/lib/std/c.zig index aeb48a3cc2..ac4b3cd1d0 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -1914,7 +1914,7 @@ pub const LC = enum(c_int) { IDENTIFICATION = 12, }; -pub extern "c" fn setlocale(category: LC, locale: ?[*:0]const u8) [*:0]const u8; +pub extern "c" fn setlocale(category: LC, locale: ?[*:0]const u8) ?[*:0]const u8; pub const getcontext = if (builtin.target.isAndroid()) @compileError("android bionic libc does not implement getcontext") From 5af68a651c1f923286ffcd014ef540ab13d26c45 Mon Sep 17 00:00:00 2001 From: Eric Joldasov Date: Tue, 16 Jul 2024 00:32:29 +0500 Subject: [PATCH 2/2] std.c.LC: mark enum as non-exhaustive They are implementation-defined and can have values other than hard-coded here. Also, standard permits other values not mentioned there: > Additional macro definitions, beginning with the characters LC_ > and an uppercase letter, may also be specified by the implementation. Signed-off-by: Eric Joldasov --- lib/std/c.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/std/c.zig b/lib/std/c.zig index ac4b3cd1d0..ee958fcab0 100644 --- a/lib/std/c.zig +++ b/lib/std/c.zig @@ -1912,6 +1912,7 @@ pub const LC = enum(c_int) { TELEPHONE = 10, MEASUREMENT = 11, IDENTIFICATION = 12, + _, }; pub extern "c" fn setlocale(category: LC, locale: ?[*:0]const u8) ?[*:0]const u8;