#Cleaner way to strcmp against string literal?

1 messages · Page 1 of 1 (latest)

solemn knot
#

Is this the best way to do this?

if (!std.mem.eql(u8, "abcd.exe", std.fs.path.basename(&file_name)[0..8]))
        return 1;

Or is there a cleaner way that does not involve hardcoding the string literal length

stone halo
#

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;
frozen flume
#

std.mem.eql already checks if the lengths are equal

solemn knot
#

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.

median zodiac
#

std.mem.startsWith?

tiny abyss
#

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});
    }
}```
solemn knot
#
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

#

😦

dreamy idol
#

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

wet edge
#

unlikely to be your exact problem, but be aware you can't create threads in DllMain

dreamy idol
#

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;
}
solemn knot
#

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

wet edge
#

it can work, sure, but you are calling it during loader lock

solemn knot
#

whats loader lock