Skip to content

Commit 46e3fdd

Browse files
committed
fix(webui): improve null pointer handling in encode/decode
- use Zig optional binding to handle null pointers from C functions - ensure proper error return when encoding or decoding fails
1 parent 8392e7d commit 46e3fdd

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

src/webui.zig

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -312,9 +312,12 @@ pub fn setIcon(self: webui, icon: [:0]const u8, icon_type: [:0]const u8) void {
312312
/// you need free the return memory with free function
313313
pub fn encode(str: [:0]const u8) ![]u8 {
314314
const ptr = c.webui_encode(str.ptr);
315-
if (ptr == null) return WebUIError.EncodeError;
316-
const len = std.mem.len(ptr);
317-
return ptr[0..len];
315+
if (ptr) |valid_ptr| {
316+
const len = std.mem.len(valid_ptr);
317+
return valid_ptr[0..len];
318+
} else {
319+
return WebUIError.EncodeError;
320+
}
318321
}
319322

320323
/// Base64 decoding.
@@ -323,9 +326,12 @@ pub fn encode(str: [:0]const u8) ![]u8 {
323326
/// you need free the return memory with free function
324327
pub fn decode(str: [:0]const u8) ![]u8 {
325328
const ptr = c.webui_decode(str.ptr);
326-
if (ptr == null) return WebUIError.DecodeError;
327-
const len = std.mem.len(ptr);
328-
return ptr[0..len];
329+
if (ptr) |valid_ptr| {
330+
const len = std.mem.len(valid_ptr);
331+
return valid_ptr[0..len];
332+
} else {
333+
return WebUIError.DecodeError;
334+
}
329335
}
330336

331337
/// Safely free a buffer allocated by WebUI using

0 commit comments

Comments
 (0)