Skip to content

Commit d8ec470

Browse files
authored
Throw an error for non-ASCII xattr names (#657)
Throws an error for non-ASCII xattr names on read instead of crashing at force-unwrapping.
1 parent 7578850 commit d8ec470

1 file changed

Lines changed: 6 additions & 1 deletion

File tree

Sources/ContainerizationEXT4/EXT4+Xattrs.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,9 @@ extension EXT4 {
267267
continue
268268
}
269269
let rawName = buffer[i..<endIndex]
270-
let name = String(bytes: rawName, encoding: .ascii)!
270+
guard let name = String(bytes: rawName, encoding: .ascii) else {
271+
throw Error.nonAsciiXattrName
272+
}
271273
let valueStart = Int(xattrEntry.valueOffset) + offset
272274
let valueEnd = Int(xattrEntry.valueOffset) + Int(xattrEntry.valueSize) + offset
273275
let value = [UInt8](buffer[valueStart..<valueEnd])
@@ -290,6 +292,7 @@ extension EXT4 {
290292
case insufficientSpace(_ inode: Int)
291293
case malformedXattrBuffer
292294
case convertAsciiString(_ s: String)
295+
case nonAsciiXattrName
293296
case missingXAttrHeader
294297

295298
public var description: String {
@@ -300,6 +303,8 @@ extension EXT4 {
300303
return "malformed extended attribute buffer"
301304
case .convertAsciiString(let s):
302305
return "cannot convert string \(s) to a list of ASCII characters"
306+
case .nonAsciiXattrName:
307+
return "extended attribute name contains non-ASCII bytes"
303308
case .missingXAttrHeader:
304309
return "missing header for extended attribute entry"
305310
}

0 commit comments

Comments
 (0)