Skip to content

Commit

Permalink
extract_byte_string will return none when reading i8* null byte strings.
Browse files Browse the repository at this point in the history
  • Loading branch information
idavis committed May 2, 2023
1 parent acaeb54 commit 29b30da
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pyqir/tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ def test_global_string() -> None:
assert value.decode("utf-8") == "Hello World!\0"


def test_null_i8ptr_string() -> None:
llvm_ir = """
define void @main() {
call void @a(i8* null)
ret void
}
declare void @a(i8*)
"""

module = Module.from_ir(Context(), llvm_ir, "module")
null_ptr_value = module.functions[0].basic_blocks[0].instructions[0].operands[0]
string = extract_byte_string(null_ptr_value)
assert string is None


def test_parser_zext_support() -> None:
bitcode = Path("tests/select.bc").read_bytes()
mod = Module.from_bitcode(Context(), bitcode)
Expand Down
4 changes: 4 additions & 0 deletions qirlib/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ pub unsafe fn global_string(module: LLVMModuleRef, value: &[u8]) -> LLVMValueRef
}

pub unsafe fn extract_string(value: LLVMValueRef) -> Option<Vec<u8>> {
if LLVMIsNull(value) != 0 {
return None;
}

if !is_byte_string(LLVMTypeOf(value)) {
return None;
}
Expand Down

0 comments on commit 29b30da

Please sign in to comment.