Bit Manipulation - UTF-8 Validation
Consider the following buggy UTF-8 validation code snippet. Which line contains the subtle bug that can cause invalid UTF-8 sequences to be accepted as valid?
def validUtf8(data):
remaining = 0
for byte in data:
if remaining == 0:
if (byte >> 5) == 0b110:
remaining = 1
elif (byte >> 4) == 0b1110:
remaining = 2
elif (byte >> 3) == 0b11110:
remaining = 3
elif (byte >> 7) == 0:
remaining = 0
else:
return False
else:
# Bug: missing check for continuation byte pattern
remaining -= 1
return remaining == 0
