Open the context
Choose the project folder and name the files or behavior involved.
Decode a DNS name from a byte packet of at most 65535 bytes and return raw labels with the following offset. Test roots, truncation and a 255-byte expanded-name limit, rejecting invalid label tags, cycles and pointers that do not point backward.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
def dns_name(data, offset):
if type(data) is not bytes or len(data) > 65535 or type(offset) is not int or not 0 <= offset < len(data):
raise ValueError("dns.input")
labels, seen, pos, end, size = [], set(), offset, None, 1
while True:
if pos in seen or pos >= len(data):
raise ValueError("dns.pointer")
seen.add(pos)
count = data[pos]
if count & 192 == 192:
if pos+1 >= len(data):
raise ValueError("dns.truncated")
target = ((count & 63) << 8) | data[pos+1]
if target >= pos:
raise ValueError("dns.forward")
if end is None:
end = pos+2
pos = target
continue
if count & 192 or pos+1+count > len(data):
raise ValueError("dns.label")
pos += 1
if count == 0:
return labels, pos if end is None else end
size += count+1
if size > 255:
raise ValueError("dns.length")
labels.append(data[pos:pos+count])
pos += count
The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.