1
Fork 0

also add a failing shell test

main
Motiejus Jakštys 2022-02-16 11:55:13 +02:00 committed by Motiejus Jakštys
parent 67e8a1d83c
commit e2bc4e6094
1 changed files with 12 additions and 13 deletions

View File

@ -43,22 +43,21 @@ const ShellPopcon = struct {
.blob = try BoundedArray(u8, MaxShells * MaxShellLen).init(0), .blob = try BoundedArray(u8, MaxShells * MaxShellLen).init(0),
.indices = StringHashMap(u10).init(allocator), .indices = StringHashMap(u10).init(allocator),
}; };
var offset: u10 = 0; var fullOffset: u12 = 0;
var idx: u10 = 0; var idx: u10 = 0;
while (idx < shells.len) { while (idx < shells.len) {
const len = @intCast(u6, shells.get(idx).len); const len = @intCast(u6, shells.get(idx).len);
try self.blob.appendSlice(shells.get(idx)); try self.blob.appendSlice(shells.get(idx));
const ourShell = self.blob.constSlice()[offset .. offset + len]; const ourShell = self.blob.constSlice()[fullOffset .. fullOffset + len];
try self.indices.put(ourShell, idx); try self.indices.put(ourShell, idx);
self.index.set(idx, ShellIndex{ self.index.set(idx, ShellIndex{
.offset = offset >> 2, .offset = @intCast(u10, fullOffset >> 2),
.len = len, .len = len,
}); });
offset += len;
// Padd padding to make offset divisible by 4. // Padd padding to make offset divisible by 4.
const padding = (offset + 3) & ~@intCast(u10, 3); const padding = (fullOffset + 3) & ~@intCast(u12, 3);
offset += padding; fullOffset += len + padding;
try self.blob.appendNTimes(0, padding); try self.blob.appendNTimes(0, padding);
idx += 1; idx += 1;
} }
@ -170,15 +169,15 @@ test "basic shellpopcon" {
try testing.expectEqual(sections.getIndex(bash).?, 2); try testing.expectEqual(sections.getIndex(bash).?, 2);
try testing.expectEqual(sections.getIndex(nobody), null); try testing.expectEqual(sections.getIndex(nobody), null);
const longIndex = sections.getIndex(long).?; const idx = sections.getIndex(zsh).?;
const start = sections.index.get(longIndex).offset << 2; const start = sections.index.get(idx).offset << 2;
const end = start + sections.index.get(longIndex).len; const end = start + sections.index.get(idx).len;
const gotLong = sections.blob.constSlice()[start..end]; const got = sections.blob.constSlice()[start..end];
const stderr = std.io.getStdErr().writer(); const stderr = std.io.getStdErr().writer();
try stderr.print("\n", .{}); try stderr.print("\n", .{});
try stderr.print("gotLong: {s}\n", .{gotLong}); try stderr.print("gotLong: {s}\n", .{got});
try stderr.print(" long: {s}\n", .{long}); try stderr.print(" long: {s}\n", .{zsh});
try testing.expectEqual(gotLong, long); try testing.expectEqual(got, zsh);
} }