# Buggy code
def visit_target(self, node):
    # postpone the labels until after the sectioning command
    parindex = node.parent.index(node)
    try:
        try:
            next = node.parent[parindex + 1]
        except IndexError:
            # last node in parent, look at next after parent
            # (for section of equal level)
            next = node.parent.parent[node.parent.parent.index(node.parent)]
        if isinstance(next, nodes.section):
            if node.get("refid"):
                self.next_section_ids.add(node["refid"])
            self.next_section_ids.update(node["ids"])
            return
    except IndexError:
        pass
    if "refuri" in node:
        return
    if node.get("refid"):
        self.add_anchor(node["refid"], node)
    for id in node["ids"]:
        self.add_anchor(id, node)


# Traceback (IndexError)
# Sphinx version: 1.4.1
# Python version: 3.4.3 (CPython)
# Docutils version: 0.12 release
# Jinja2 version: 2.7.3
# Last messages:
#   none found
#   pickling environment...
#   done
#   checking consistency...
#   done
#   processing bug.texi...
#   index
#
#   resolving references...
#   writing...
# Loaded extensions:
#   alabaster (0.7.8) from /usr/local/lib/python3.4/dist-packages/alabaster/__init__.py
Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/sphinx/writers/texinfo.py", line 643, in visit_target
next = node.parent[parindex+1]
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 569, in __getitem__
return self.children[key]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.4/dist-packages/sphinx/cmdline.py", line 244, in main
app.build(opts.force_all, filenames)
File "/usr/local/lib/python3.4/dist-packages/sphinx/application.py", line 287, in build
self.builder.build_update()
File "/usr/local/lib/python3.4/dist-packages/sphinx/builders/__init__.py", line 246, in build_update
self.build(['__all__'], to_build)
File "/usr/local/lib/python3.4/dist-packages/sphinx/builders/__init__.py", line 322, in build
self.write(docnames, list(updated_docnames), method)
File "/usr/local/lib/python3.4/dist-packages/sphinx/builders/texinfo.py", line 163, in write
docwriter.write(doctree, destination)
File "/usr/local/lib/python3.4/dist-packages/docutils/writers/__init__.py", line 80, in write
self.translate()
File "/usr/local/lib/python3.4/dist-packages/sphinx/writers/texinfo.py", line 130, in translate
self.document.walkabout(visitor)
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 174, in walkabout
if child.walkabout(visitor):
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 174, in walkabout
if child.walkabout(visitor):
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 166, in walkabout
visitor.dispatch_visit(self)
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 1882, in dispatch_visit
return method(node)
File "/usr/local/lib/python3.4/dist-packages/sphinx/writers/texinfo.py", line 835, in visit_footnote_reference
footnode.walkabout(self)
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 174, in walkabout
if child.walkabout(visitor):
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 166, in walkabout
visitor.dispatch_visit(self)
File "/usr/local/lib/python3.4/dist-packages/docutils/nodes.py", line 1882, in dispatch_visit
return method(node)
File "/usr/local/lib/python3.4/dist-packages/sphinx/writers/texinfo.py", line 647, in visit_target
next = node.parent.parent[node.parent.parent.index(node.parent)]
AttributeError: 'NoneType' object has no attribute 'index'

# Fixed code
def visit_target(self, node):
    # postpone the labels until after the sectioning command
    parindex = node.parent.index(node)
    try:
        try:
            next = node.parent[parindex + 1]
        except IndexError:
            # last node in parent, look at next after parent
            # (for section of equal level)
            next = node.parent.parent[node.parent.parent.index(node.parent)]
        if isinstance(next, nodes.section):
            if node.get("refid"):
                self.next_section_ids.add(node["refid"])
            self.next_section_ids.update(node["ids"])
            return
    except (IndexError, AttributeError):
        pass
    if "refuri" in node:
        return
    if node.get("refid"):
        self.add_anchor(node["refid"], node)
    for id in node["ids"]:
        self.add_anchor(id, node)