Uploaded image for project: 'XWiki Platform'
  1. XWiki Platform
  2. XWIKI-22036

Call "hasNext" before call "next" in XWikiDocumentArchive

    XMLWordPrintable

Details

    • Bug
    • Resolution: Cannot Reproduce
    • Minor
    • None
    • 16.2.0
    • Old Core
    • Unknown
    • N/A
    • N/A
    • Awaiting Committer feedback

    Description

      If it cannot be guaranteed that the Iterator will successfully access the next element using the "next" method, it is necessary to use the "hasNext" method to check before attempting to access the next element. 

      1. 

      If the number of nodes is an odd number, the second "it.next()" may cause NoSuchElementExceptions.

      // xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/doc/XWikiDocumentArchive.java
      public void setArchive(String text) throws XWikiException {
      ...
      Collection nodes = archive.getNodes(getWikiReference(), getId());
      for (Iterator it = nodes.iterator(); it.hasNext();) {
      XWikiRCSNodeInfo nodeInfo = (XWikiRCSNodeInfo) it.next();
      XWikiRCSNodeContent nodeContent = (XWikiRCSNodeContent) it.next();
      updateNode(nodeInfo);
      this.updatedNodeInfos.add(nodeInfo);
      
      
      

      2. 

      I couldn't find any logic that ensures the 'tables' variable is not empty. If there is no such logic in place, it is recommended to add proper validation to ensure that 'tables' is not empty before accessing it.

       

      // xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/internal/store/hibernate/query/HqlQueryUtils.java
      private static String getTableName(Table table, Map<String, String> tables)
      { 
      String tableName = tables.values().iterator().next();
      
      
      

      Attachments

        Activity

          People

            vmassol Vincent Massol
            xiqinger xiqinger
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: