xpathleak.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/python
  2. import sys, libxml2
  3. libxml2.debugMemory(True)
  4. expect="""--> Invalid expression
  5. --> xmlXPathEval: evaluation failed
  6. --> Invalid expression
  7. --> xmlXPathEval: evaluation failed
  8. --> Invalid expression
  9. --> xmlXPathEval: evaluation failed
  10. --> Invalid expression
  11. --> xmlXPathEval: evaluation failed
  12. --> Invalid expression
  13. --> xmlXPathEval: evaluation failed
  14. --> Invalid expression
  15. --> xmlXPathEval: evaluation failed
  16. """
  17. err=""
  18. def callback(ctx, str):
  19. global err
  20. err = err + "%s %s" % (ctx, str)
  21. libxml2.registerErrorHandler(callback, "-->")
  22. doc = libxml2.parseDoc("<fish/>")
  23. ctxt = doc.xpathNewContext()
  24. ctxt.setContextNode(doc)
  25. for expr in (":false()","bad:()","bad(:)",":bad(:)","bad:(:)","bad:bad(:)"):
  26. try:
  27. ctxt.xpathEval(expr)
  28. except libxml2.xpathError, e:
  29. pass
  30. else:
  31. print "Unexpectedly legal expression:", expr
  32. ctxt.xpathFreeContext()
  33. doc.freeDoc()
  34. if err != expect:
  35. print "error"
  36. print "received %s" %(err)
  37. print "expected %s" %(expect)
  38. sys.exit(1)
  39. libxml2.cleanupParser()
  40. leakedbytes = libxml2.debugMemory(True)
  41. if leakedbytes == 0:
  42. print "OK"
  43. else:
  44. print "Memory leak", leakedbytes, "bytes"
  45. # drop file to .memdump file in cwd, but won't work if not compiled in
  46. libxml2.dumpMemory()