Const pointer with large array size

Hey, please could you help me to understand what is the expected behaviour of code snippet bellow?

const t: *const [1000]u8 = "asdf";
std.debug.print("string: {s}\n", .{t});

I’m using a scaffold build file from zig init, and using zig build && ./zig-out/bin/main to test it.

Compilation is fine with no errors. Should I get a panic instead or it’s expected?

I do see different kind of overflow data :grinning: depends on release mode. Please see few examples:

Buil type cmd Output
zig build string: asdfinteger overflowattempt to cast negative value to unsigned integerh�Fstring: {s} invalid error codethread {} panic: {s} Panicked during a panic. Aborting. (msg truncated)@memcpy arguments aliasswitch on corrupt valueunable to install segfault handler, maybe adjust have_segfault_handling_support in std/debug.zigreached unreachable codeDeadlock detectedincorrect alignment’noreturn’ function returnedattempt to use null valueinteger cast truncated bitscast causes pointer to be null__TEXT__unwind_info__eh_frame{s}:{d}:{d}???:?:?: 0x{x} in {s} ({s}) ^???for loop over objects with non-equal lengths@memcpy arguments have non-equal lengths__DWARF__debug_info__debug_abbrev__debug_str__debug_str_offsets__debug_line__debug_line_str__debug_ranges__debug_loclists__debug_rnglists__debug_addr__debug_names__debug_frame__eh_frame_h
zig build --release=small string: asdf00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupported 8888@��0��04X���� t
zig build --release=fast string: asdf(msg truncated)failed to set noop SIGPIPE handler: {s}00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupportedDiskQuotaFileTooBigInputOutputNoSpaceLeftDeviceBusyInvalidArgumentAccessDeniedBrokenPipeSystemResourcesOperationAbortedNotOpenForWritingLockViolationWouldBlockConnectionResetByPeerUnexpectedOverflowInvalidUtf8Utf8ExpectedContinuationUtf8OverlongEncodingUtf8EncodesSurrogateHalfUtf8CodepointTooLargeUtf8InvalidStartByteTruncatedInputUtf8CannotEncodeSurrogateHalfCodepointTooLargeMissingDebugInfoUnsupportedOperatingSystemInvalidDebugInfoOutOfMemorySharingViolationPathAlreadyExistsFileNotFoundPipeBusyNameTooLongInvalidWtf8BadPathNameNetworkNotFoundAntivirusInterferenceSymLinkLoopProcessFdQuotaExceededSystemFdQuotaExceededNoDeviceIsDirNotDirFileLocksNotSupportedFileBusy
zig build --release=safe string: asdf__TEXT__unwind_info__eh_frame__DWARF__debug_info__debug_abbrev__debug_str__debug_str_offsets__debug_line__debug_line_str__debug_ranges__debug_loclists__debug_rnglists__debug_addr__debug_names__debug_frame__eh_frame_hdr(msg truncated)failed to set noop SIGPIPE handler: {s}00010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899OperationNotSupportedDiskQuotaFileTooBigInputOutputNoSpaceLeftDeviceBusyInvalidArgumentAccessDeniedBrokenPipeSystemResourcesOperationAbortedNotOpenForWritingLockViolationWouldBlockConnectionResetByPeerUnexpectedMissingDebugInfoUnsupportedOperatingSystemInvalidDebugInfoOutOfMemorySharingViolationPathAlreadyExistsFileNotFoundPipeBusyNameTooLongInvalidUtf8InvalidWtf8BadPathNameNetworkNotFoundAntivirusInterferenceSymLinkLoopProcessFdQuotaExc

Many thanks in advance.

Best,
Felipe

It should give you a compiler error, something like

test.zig:10:29: error: expected type '*const [1000]u8', found '*const [4:0]u8'
 const t: *const [1000]u8 = "asdf";
                            ^~~~~~

This looks like it could be a compiler bug.
What version of zig you are using?

@IntegratedQuantum I’m using 0.12.0 installed by homebrew.

$ zig version
0.12.0

$ sw_vers
ProductName:		macOS
ProductVersion:		14.4.1
BuildVersion:		23E224

btw I was expecting an compiler error like you’ve sent. Currently I’m using on MacOS ARM M1 arch.

Have you using same version but diff arch probably?

I was using a slightly older version (0.12.0-dev.3518+d2be725e4).
I just tried zig 0.12 and I get the same behavior.

It’s probably best if you report this on github.

Yeah, I’m actually doing that right now… afterwards I’ll post the link over here.

Many thanks for the confirmation @IntegratedQuantum .

Yes, it’s a bug.

This compiles and panics (since 995 bytes are undefined):

    const ts: *const [4:0]u8 = &[4:0]u8{ 'a', 's', 'd', 'f' };
    const t: *const [1000]u8 = ts;

This does not compile:

    const ts: *const [4]u8 = &[4]u8{ 'a', 's', 'd', 'f' };
    const t: *const [1000]u8 = ts;

error: expected type '*const [1000]u8', found '*const [4]u8'

1 Like

Many thanks for confirming this @dimdin and to catch it by other example, which doesn’t not compile here as well.

Bug reported it.

3 Likes