#Cleaner way to strcmp against string literal?
1 messages · Page 1 of 1 (latest)
i guess you could just do:
const compare_string = "abcd.exe";
if (!std.mem.eql(u8, compare_string, std.fs.path.basename(&file_name)[0..compare_string.len]))
return 1;
std.mem.eql already checks if the lengths are equal
it does not work
without explicitly defining slice length
Actually nevermind- i think its bc there is invisible whitespace padding on the right
i could maybe use std mem span with mem trimRight? i dont know.
std.mem.startsWith?
This works for me:
const std = @import("std");
const print = std.debug.print;
pub fn main() void {
compare("/usr/home/xyz/abcd.exe");
compare("/usr/home/xyz/abcde.exe");
compare("/usr/home/xyz/abce.exe");
}
fn compare(file_name: []const u8) void {
if (std.mem.eql(u8, "abcd.exe", std.fs.path.basename(file_name))) {
print("{s}: equal\n", .{file_name});
} else {
print("{s}: not equal\n", .{file_name});
}
}```
var file_path = [_]u8{0} ** win32.MAX_PATH;
_ = std.fs.selfExePath(&file_path) catch unreachable;
if (!std.mem.eql(u8, "zt.exe", std.fs.path.basename(&file_path)[0..6]))
return 1;
What in this code could be causing a silent crash stack overflow in Release build?
if i remove these lines i don't crash
full code
pub fn DllMain(h_module: win.HINSTANCE, fdw_reason: u32, lp_reserved: ?*anyopaque) c_int {
_ = lp_reserved;
if (fdw_reason != win32.DLL_PROCESS_ATTACH)
return 1;
var file_path = [_]u8{0} ** win32.MAX_PATH;
_ = std.fs.selfExePath(&file_path) catch unreachable;
if (!std.mem.eql(u8, "zt.exe", std.fs.path.basename(&file_path)[0..6]))
return 1;
_ = win32.DisableThreadLibraryCalls(h_module);
_ = mem_utils.createThread(&engine.init);
return 1;
}
i think its maybe not cleaning up the stack in DllMain
i know that the mem equal is not returning
😦
maybe try writing it like this
var buf = [_]u8{0} ** win32.MAX_PATH;
const file_path = std.fs.selfExePath(&buf) catch unreachable;
i think the issue you're having below is that you're passing the entire buffer to fs.path.basename()
the buffer includes all the trailing zeroes
unlikely to be your exact problem, but be aware you can't create threads in DllMain
so here's how that function might look:
pub fn DllMain(h_module: win.HINSTANCE, fdw_reason: u32, lp_reserved: ?*anyopaque) c_int {
_ = lp_reserved;
if (fdw_reason != win32.DLL_PROCESS_ATTACH)
return 1;
var buf = [_]u8{0} ** win32.MAX_PATH;
const file_path = std.fs.selfExePath(&buf) catch unreachable;
if (!std.mem.eql(u8, "zt.exe", std.fs.path.basename(file_path)))
return 1;
_ = win32.DisableThreadLibraryCalls(h_module);
_ = mem_utils.createThread(&engine.init);
return 1;
}
it works fine
and if i move the exact same code to engine.init it works fine
thats how i fixed it just now..
ive been calling win32 CreateThread in dllmain for years
it can work, sure, but you are calling it during loader lock
whats loader lock