@@ -810,9 +810,11 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
810810 List <RRset > additionalSectionSets = getSectionRRsets (Section .ADDITIONAL );
811811 List <RRset > authoritySectionSets = getSectionRRsets (Section .AUTHORITY );
812812
813- List <RRset > cleanedAnswerSection = new ArrayList <>();
814- List <RRset > cleanedAuthoritySection = new ArrayList <>();
815- List <RRset > cleanedAdditionalSection = new ArrayList <>();
813+ @ SuppressWarnings ("unchecked" )
814+ List <RRset >[] cleanedSection = new ArrayList [4 ];
815+ cleanedSection [Section .ANSWER ] = new ArrayList <>();
816+ cleanedSection [Section .AUTHORITY ] = new ArrayList <>();
817+ cleanedSection [Section .ADDITIONAL ] = new ArrayList <>();
816818 boolean hadNsInAuthority = false ;
817819
818820 // For the ANSWER section, remove all "irrelevant" records and add synthesized CNAMEs from
@@ -843,7 +845,7 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
843845 // If DNAME was queried, don't attempt to synthesize CNAME
844846 if (query .getQuestion ().getType () != Type .DNAME ) {
845847 // The DNAME is valid, accept it
846- cleanedAnswerSection .add (rrset );
848+ cleanedSection [ Section . ANSWER ] .add (rrset );
847849
848850 // Check if the next rrset is correct CNAME, otherwise synthesize a CNAME
849851 RRset nextRRSet = answerSectionSets .size () >= i + 2 ? answerSectionSets .get (i + 1 ) : null ;
@@ -863,7 +865,7 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
863865
864866 // Add a synthesized CNAME; TTL=0 to avoid caching
865867 Name dnameTarget = sname .fromDNAME (dname );
866- cleanedAnswerSection .add (
868+ cleanedSection [ Section . ANSWER ] .add (
867869 new RRset (new CNAMERecord (sname , dname .getDClass (), 0 , dnameTarget )));
868870 sname = dnameTarget ;
869871
@@ -872,7 +874,7 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
872874 for (i ++; i < answerSectionSets .size (); i ++) {
873875 rrset = answerSectionSets .get (i );
874876 if (rrset .getName ().equals (oldSname )) {
875- cleanedAnswerSection .add (rrset );
877+ cleanedSection [ Section . ANSWER ] .add (rrset );
876878 } else {
877879 break ;
878880 }
@@ -943,14 +945,14 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
943945 }
944946
945947 sname = ((CNAMERecord ) rrset .first ()).getTarget ();
946- cleanedAnswerSection .add (rrset );
948+ cleanedSection [ Section . ANSWER ] .add (rrset );
947949
948950 // In CNAME ANY response, can have data after CNAME
949951 if (query .getQuestion ().getType () == Type .ANY ) {
950952 for (i ++; i < answerSectionSets .size (); i ++) {
951953 rrset = answerSectionSets .get (i );
952954 if (rrset .getName ().equals (oldSname )) {
953- cleanedAnswerSection .add (rrset );
955+ cleanedSection [ Section . ANSWER ] .add (rrset );
954956 } else {
955957 break ;
956958 }
@@ -973,9 +975,9 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
973975 }
974976
975977 // Mark the additional names from relevant RRset as OK
976- cleanedAnswerSection .add (rrset );
978+ cleanedSection [ Section . ANSWER ] .add (rrset );
977979 if (sname .equals (rrset .getName ())) {
978- addAdditionalRRset (rrset , additionalSectionSets , cleanedAdditionalSection );
980+ addAdditionalRRset (rrset , additionalSectionSets , cleanedSection [ Section . ADDITIONAL ] );
979981 }
980982 }
981983
@@ -1045,15 +1047,25 @@ public Message normalize(Message query, boolean throwOnIrrelevantRecord)
10451047 }
10461048 }
10471049
1048- cleanedAuthoritySection .add (rrset );
1049- addAdditionalRRset (rrset , additionalSectionSets , cleanedAdditionalSection );
1050+ cleanedSection [ Section . AUTHORITY ] .add (rrset );
1051+ addAdditionalRRset (rrset , additionalSectionSets , cleanedSection [ Section . ADDITIONAL ] );
10501052 }
10511053
10521054 Message cleanedMessage = new Message (this .getHeader ());
10531055 cleanedMessage .sections [Section .QUESTION ] = this .sections [Section .QUESTION ];
1054- cleanedMessage .sections [Section .ANSWER ] = rrsetListToRecords (cleanedAnswerSection );
1055- cleanedMessage .sections [Section .AUTHORITY ] = rrsetListToRecords (cleanedAuthoritySection );
1056- cleanedMessage .sections [Section .ADDITIONAL ] = rrsetListToRecords (cleanedAdditionalSection );
1056+ for (int section : new int [] {Section .ANSWER , Section .AUTHORITY , Section .ADDITIONAL }) {
1057+ cleanedMessage .sections [section ] = rrsetListToRecords (cleanedSection [section ]);
1058+
1059+ // Fixup counts in the header
1060+ cleanedMessage
1061+ .getHeader ()
1062+ .setCount (
1063+ section ,
1064+ cleanedMessage .sections [section ] == null
1065+ ? 0
1066+ : cleanedMessage .sections [section ].size ());
1067+ }
1068+
10571069 return cleanedMessage ;
10581070 }
10591071
0 commit comments