#Trace/breakpoint trap (core dumped)

1 messages · Page 1 of 1 (latest)

digital delta
#

I am getting this message when running zig test src/crypto/encryption.zig --library crypto on this file:

const std = @import("std");

const crypto = @cImport({
    @cInclude("openssl/evp.h");
    @cInclude("openssl/aes.h");
});

const CipherError = error{
    CIPHER_INIT_ERROR,
    ENCRYPTION_INIT_ERROR,
    DECRYPTION_INIT_ERROR,
};

pub const Cipher = struct {
    key: [16]u8,
    block_size: usize,
    enc_ctx: *const crypto.EVP_CIPHER_CTX,
    dec_ctx: *const crypto.EVP_CIPHER_CTX,

    pub fn init(key: [16]u8) !Cipher {
        const c_key: [*c]const u8 = key[0..];

        const enc_ctx: crypto.EVP_CIPHER_CTX = undefined;
        const dec_ctx: crypto.EVP_CIPHER_CTX = undefined;

        if (crypto.EVP_CIPHER_CTX_init(@constCast(&enc_ctx)) == 0) return CipherError.CIPHER_INIT_ERROR;
        if (crypto.EVP_EncryptInit_ex2(@constCast(&enc_ctx), crypto.EVP_aes_128_cfb8(), c_key, c_key, null) == 0) return CipherError.ENCRYPTION_INIT_ERROR;

        if (crypto.EVP_CIPHER_CTX_init(@constCast(&dec_ctx)) == 0) return CipherError.CIPHER_INIT_ERROR;
        if (crypto.EVP_DecryptInit_ex2(@constCast(&dec_ctx), crypto.EVP_aes_128_cfb8(), c_key, c_key, null) == 0) return CipherError.DECRYPTION_INIT_ERROR;

        const block_size = crypto.EVP_CIPHER_block_size(crypto.EVP_aes_128_cfb8());

        return .{
            .key = key,
            .block_size = @intCast(block_size),
            .enc_ctx = &enc_ctx,
            .dec_ctx = &dec_ctx,
        };
    }
};

test Cipher {
    std.testing.refAllDecls(Cipher);
}

I'm not sure what is even going on to cause this issue. I'm not really testing anything, just using the test command to see if it compiles. Anything, even just pointing in the right direction would help. Thanks!

digital delta
#

Ok, I think I found the issue. If I reference the file in my main file and then call zig build test it complies and runs fine. I think that means there is just something funky/not being included when using just zig test

#

Nevermind, I think I just messed up the test reference. It doesn't work with zig build test. It says more now, but it's all just as useless as Trace/breakpoint trap (core dumped). It just says that the test command terminated unexpectedly.

hollow bobcat
#

Problem is you are casting away the const properties of the enc_ctx and dec_ct variables

#

And the functions you pass them to are trying to modify them, which is illegal behaviour

#

Also you are returning pointers to the stack allocated variables enc_ctx and dec_ctx, which will be invalidated when the function returns