Uploaded image for project: 'JSword'
  1. JSword
  2. JS-170

GenBookBackend.contains(key) throws NPE if key not found

    Details

    • Type: Bug
    • Status: Closed (View Workflow)
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.6
    • Fix Version/s: None
    • Component/s: o.c.jsword.book.sword
    • Labels:
      None
    • Environment:

      All

      Description

      I think there is a bug in GenBookBackend.contains(Key).
      The find method will return null if the key is not in the book
      TreeNode node = find(key);
      which means the next line will throw a NullPointerException
      byte[] userData = node.getUserData();

      Here is the whole method:
      @Override
      public boolean contains(Key key) {
      checkActive();

      try

      { DataPolice.setKey(key); TreeNode node = find(key); byte[] userData = node.getUserData(); // Some entries may be empty. return userData.length == 8; }

      catch (IOException e)

      { return false; }

      finally

      { DataPolice.setKey(null); }

      }

        Attachments

          Activity

          Hide
          mjdenham Martin Denham added a comment -

          A simple fix is to add a simple null check as below:

          public boolean contains(Key key) {
          checkActive();

          try {
          DataPolice.setKey(key);
          TreeNode node = find(key);

          if (node==null)

          { return false; }

          byte[] userData = node.getUserData();

          // Some entries may be empty.
          return userData.length == 8;
          } catch (IOException e)

          { return false; }

          finally

          { DataPolice.setKey(null); }

          }

          Show
          mjdenham Martin Denham added a comment - A simple fix is to add a simple null check as below: public boolean contains(Key key) { checkActive(); try { DataPolice.setKey(key); TreeNode node = find(key); if (node==null) { return false; } byte[] userData = node.getUserData(); // Some entries may be empty. return userData.length == 8; } catch (IOException e) { return false; } finally { DataPolice.setKey(null); } }
          Hide
          dmsmith DM Smith added a comment -

          I just checked in a fix for this. Let me know if it works for you.

          Show
          dmsmith DM Smith added a comment - I just checked in a fix for this. Let me know if it works for you.
          Hide
          mjdenham Martin Denham added a comment -

          I can't actually get the whole code because there are other changes and I am trying to stabilise prior to a release but I looked at it and the fix is exactly the same as a version which works. So yes the fix is good. Thanks.

          Show
          mjdenham Martin Denham added a comment - I can't actually get the whole code because there are other changes and I am trying to stabilise prior to a release but I looked at it and the fix is exactly the same as a version which works. So yes the fix is good. Thanks.

            People

            • Assignee:
              dmsmith DM Smith
              Reporter:
              mjdenham Martin Denham
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: