# Buggy code
def decode_methods_request(offset, data):
    """
    Try to decodes a METHOD request
    @param int offset: the offset to start in the data
    @param str data: the serialised data to decode from
    @return: Tuple (offset, None) on failure, else (new_offset, MethodRequest)
    @rtype: (int, None|MethodRequest)
    """
    # Check if we have enough bytes
    if len(data) - offset < 2:
        return offset, None

    (version, number_of_methods) = struct.unpack_from("!BB", data, offset)

    offset += 2

    methods = set([])
    for i in range(number_of_methods):
        (method,) = struct.unpack_from("!B", data, offset)
        methods.add(method)
        offset += 1

    return offset, MethodRequest(version, methods)


# Traceback (struct.error)
Aug 23 12:41:27 LSW-95 twistd[26310]: 2018-08-23T12:41:27+0200 [Socks5Connection,9,<FILTERED>] Unhandled Error
Aug 23 12:41:27 LSW-95 twistd[26310]: Traceback (most recent call last):
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/python/log.py", line 103, in callWithLogger
Aug 23 12:41:27 LSW-95 twistd[26310]: return callWithContext({"system": lp}, func, *args, **kw)
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/python/log.py", line 86, in callWithContext
Aug 23 12:41:27 LSW-95 twistd[26310]: return context.call({ILogContext: newCtx}, func, *args, **kw)
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 122, in callWithContext
Aug 23 12:41:27 LSW-95 twistd[26310]: return self.currentContext().callWithContext(ctx, func, *args, **kw)
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/python/context.py", line 85, in callWithContext
Aug 23 12:41:27 LSW-95 twistd[26310]: return func(*args,**kw)
Aug 23 12:41:27 LSW-95 twistd[26310]: --- <exception caught here> ---
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/internet/posixbase.py", line 614, in _doReadOrWrite
Aug 23 12:41:27 LSW-95 twistd[26310]: why = selectable.doRead()
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 205, in doRead
Aug 23 12:41:27 LSW-95 twistd[26310]: return self._dataReceived(data)
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/usr/local/lib/python2.7/dist-packages/twisted/internet/tcp.py", line 211, in _dataReceived
Aug 23 12:41:27 LSW-95 twistd[26310]: rval = self.protocol.dataReceived(data)
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/opt/tribler/Tribler/Core/Socks5/connection.py", line 43, in dataReceived
Aug 23 12:41:27 LSW-95 twistd[26310]: if not self._try_handshake():
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/opt/tribler/Tribler/Core/Socks5/connection.py", line 60, in _try_handshake
Aug 23 12:41:27 LSW-95 twistd[26310]: offset, request = conversion.decode_methods_request(0, self.buffer)
Aug 23 12:41:27 LSW-95 twistd[26310]: File "/opt/tribler/Tribler/Core/Socks5/conversion.py", line 105, in decode_methods_request
Aug 23 12:41:27 LSW-95 twistd[26310]: method, = struct.unpack_from("!B", data, offset)
Aug 23 12:41:27 LSW-95 twistd[26310]: struct.error: unpack_from requires a buffer of at least 1 bytes

# Fixed code
def decode_methods_request(offset, data):
    """
    Try to decodes a METHOD request
    @param int offset: the offset to start in the data
    @param str data: the serialised data to decode from
    @return: Tuple (offset, None) on failure, else (new_offset, MethodRequest)
    @rtype: (int, None|MethodRequest)
    """
    # Check if we have enough bytes
    if len(data) - offset < 2:
        return offset, None

    (version, number_of_methods) = struct.unpack_from("!BB", data, offset)

    offset += 2

    # Check whether there are enough bytes for the number of methods
    if len(data) - offset < number_of_methods:
        return offset, None

    methods = set([])
    for i in range(number_of_methods):
        (method,) = struct.unpack_from("!B", data, offset)
        methods.add(method)
        offset += 1

    return offset, MethodRequest(version, methods)