Skip to content

Commit b53afc5

Browse files
committed
update dl_iterate_phdr test case to new API
1 parent 8fe636d commit b53afc5

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

lib/std/os/test.zig

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,19 @@ test "sigaltstack" {
165165
// analyzed
166166
const dl_phdr_info = if (@hasDecl(os, "dl_phdr_info")) os.dl_phdr_info else c_void;
167167

168-
fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 {
169-
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
170-
return 0;
171-
172-
var counter = data.?;
168+
const IterFnError = error{
169+
MissingPtLoadSegment,
170+
MissingLoad,
171+
BadElfMagic,
172+
FailedConsistencyCheck,
173+
};
174+
175+
fn iter_fn(info: *dl_phdr_info, size: usize, counter: *usize) IterFnError!void {
173176
// Count how many libraries are loaded
174177
counter.* += @as(usize, 1);
175178

176179
// The image should contain at least a PT_LOAD segment
177-
if (info.dlpi_phnum < 1) return -1;
180+
if (info.dlpi_phnum < 1) return error.MissingPtLoadSegment;
178181

179182
// Quick & dirty validation of the phdr pointers, make sure we're not
180183
// pointing to some random gibberish
@@ -189,25 +192,23 @@ fn iter_fn(info: *dl_phdr_info, size: usize, data: ?*usize) callconv(.C) i32 {
189192
// Find the ELF header
190193
const elf_header = @intToPtr(*elf.Ehdr, reloc_addr - phdr.p_offset);
191194
// Validate the magic
192-
if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return -1;
195+
if (!mem.eql(u8, elf_header.e_ident[0..4], "\x7fELF")) return error.BadElfMagic;
193196
// Consistency check
194-
if (elf_header.e_phnum != info.dlpi_phnum) return -1;
197+
if (elf_header.e_phnum != info.dlpi_phnum) return error.FailedConsistencyCheck;
195198

196199
found_load = true;
197200
break;
198201
}
199202

200-
if (!found_load) return -1;
201-
202-
return 42;
203+
if (!found_load) return error.MissingLoad;
203204
}
204205

205206
test "dl_iterate_phdr" {
206207
if (builtin.os == .windows or builtin.os == .wasi or builtin.os == .macosx)
207208
return error.SkipZigTest;
208209

209210
var counter: usize = 0;
210-
expect(os.dl_iterate_phdr(usize, iter_fn, &counter) != 0);
211+
try os.dl_iterate_phdr(&counter, IterFnError, iter_fn);
211212
expect(counter != 0);
212213
}
213214

0 commit comments

Comments
 (0)