detect-buffer-noassert.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Tries to detect buffer read / write calls that use noAssert set to true
  3. * @author Adam Baldwin
  4. */
  5. //------------------------------------------------------------------------------
  6. // Rule Definition
  7. //------------------------------------------------------------------------------
  8. var names = [];
  9. module.exports = function(context) {
  10. "use strict";
  11. var read = [
  12. "readUInt8",
  13. "readUInt16LE",
  14. "readUInt16BE",
  15. "readUInt32LE",
  16. "readUInt32BE",
  17. "readInt8",
  18. "readInt16LE",
  19. "readInt16BE",
  20. "readInt32LE",
  21. "readInt32BE",
  22. "readFloatLE",
  23. "readFloatBE",
  24. "readDoubleL",
  25. "readDoubleBE"
  26. ];
  27. var write = [
  28. "writeUInt8",
  29. "writeUInt16LE",
  30. "writeUInt16BE",
  31. "writeUInt32LE",
  32. "writeUInt32BE",
  33. "writeInt8",
  34. "writeInt16LE",
  35. "writeInt16BE",
  36. "writeInt32LE",
  37. "writeInt32BE",
  38. "writeFloatLE",
  39. "writeFloatBE",
  40. "writeDoubleLE",
  41. "writeDoubleBE"
  42. ];
  43. return {
  44. "MemberExpression": function (node) {
  45. var index;
  46. if (read.indexOf(node.property.name) !== -1) {
  47. index = 1;
  48. } else if (write.indexOf(node.property.name) !== -1) {
  49. index = 2;
  50. }
  51. if (index && node.parent && node.parent.arguments && node.parent.arguments[index] && node.parent.arguments[index].value) {
  52. var token = context.getTokens(node)[0];
  53. return context.report(node, 'Found Buffer.' + node.property.name + ' with noAssert flag set true');
  54. }
  55. }
  56. };
  57. };