tracker issue : CF-3820049

select a category, or use search below
(searches all categories and all time range)
Title:

Nested cfloops with less records than outer loop cause “array index out of range” error

| View in Tracker

Status/Resolution/Reason: Closed/Fixed/

Reporter/Name(from Bugbase): Sam Daams / Sam Daams (Sam I Am)

Created: 09/08/2014

Components: Language

Versions: 10.0

Failure Type: Non Functioning

Found In Build/Fixed In Build: Final /

Priority/Frequency: Normal / All users will encounter

Locale/System: English / Platforms All

Vote Count: 6

Listed in the version 2016.0.0.297996 Issues Fixed doc
Verification notes: verified_fixed on August 25, 2019 using build 2016.0.01.298513
Problem Description:
When nesting cfloops, a grouped cfloop with an inner loop outputting less records than the outer cfloop will throw error "Array index out of range: xx", with xx being the recordcount of the second query.

Steps to Reproduce:
<cfscript>
selectContent = queryNew("h2,id,content", "varchar,integer,varchar", [
    ["one", 1, "content.1"],
    ["two", 2, "content.2"],
    ["three", 3, "content.3"],
    ["four", 4, "content.4"],
    ["five", 5, "content.5"],
    ["six", 6, "content.6"],
    ["seven", 7, "content.7"]
]);

selectPricing = queryNew("groupCol,description,price", "varchar,varchar,varchar", [
    ["groupCol.1", "description.1", "1.11"],
    ["groupCol.2", "description.2", "2.22"],
    ["groupCol.2", "description.3", "3.33"],
    ["groupCol.3", "description.4", "4.44"],
    ["groupCol.3", "description.5", "5.55"],
    ["groupCol.3", "description.6", "6.66"]
]);
</cfscript>
<cfloop query="selectContent">
    <section>
        #selectContent.h2#
        <cfif selectContent.id eq 3>
            <cfloop query="selectPricing" group="groupCol">
                <table class="pricing">
                <thead>
                    <tr>
                        <th>#description#</th>
                        <th>Price</th>
                    </tr>
                </thead>
                <tbody>
                    <cfloop>
                    <tr>
                        <td>#selectPricing.description#</td>
                        <td>#selectPricing.price#</td>
                    </tr>
                    </cfloop>
                </tbody>
                </table>
            </cfloop>
        </cfif>
        #selectContent.content#
    </section>
</cfloop>

Actual Result:
Array index out of range: 6

Expected Result:
Query output as it should.

Any Workarounds:
* replace inner cfloop group with cfoutput group.
* stick a cfbreak on that third cfloop if the currentRow matches the recordcount.

----------------------------- Additional Watson Details -----------------------------

Watson Bug ID:	3820049

External Customer Info:
External Company:  
External Customer Name: Sam I Am
External Customer Email:  
External Test Config: My Hardware and Environment details: Coldfusion 10 on Mac 10.9.4

Attachments:

Comments:

See http://stackoverflow.com/questions/25719936/nested-cfloops-with-less-records-than-outer-loop-cause-array-index-out-of-range
Comment by External U.
10973 | September 08, 2014 06:58:10 AM GMT
This is a big deal. People use nested cfloops a lot, for grouping output, and there will be many situations where the nested groups have fewer records than the parent!
Vote by External U.
10978 | September 08, 2014 07:51:32 AM GMT
I agree with Brian... this is pretty fundamental. Should be be easy to fix though... simply use the code in <cfoutput>, which works fine. I am kinda perplexed that that's not what you did in the first place: factor out the code from one place, then call it in from both places that need it! -- Adam
Vote by External U.
10979 | September 08, 2014 08:35:02 AM GMT
+1 - Definitely needs to be fixed. "You all wanna be looking very intently at your own belly buttons. I see a head start to rise, violence is going to ensue. Probably guessed we mean to be thieving here but what we're after is not yours. So, let's have no undue fussing. " - Capt. Mal Reynolds, Serenity
Vote by External U.
10980 | September 08, 2014 09:58:01 AM GMT
This is still a problem in CF11 Update 3 pre-release 1. Ran into it yesterday.
Comment by External U.
10974 | November 19, 2014 02:11:21 PM GMT
+1 - Definitely needs to be fixed.
Vote by External U.
10981 | November 19, 2014 02:11:37 PM GMT
This also throws the error: <cfscript> outer = queryNew("foo", "", [["a"]]); inner = queryNew("bar", "", [["one"]]); </cfscript> <cfloop query="outer"> <cfloop query="inner" group="bar"> <cfloop> </cfloop> </cfloop> </cfloop> Leave a space between <cfloop> </cfloop>. Thanks!, -Aaron
Comment by External U.
10975 | November 24, 2014 03:34:22 AM GMT
+1 - sounds similar to CF-3740141, and both need fixed
Vote by External U.
10982 | November 24, 2014 03:36:06 AM GMT
fixed in the next major version release of CF.
Comment by Piyush K.
10976 | December 16, 2014 06:37:39 AM GMT
This is still an issue in CF10 Update 17 10,0,17,295085
Comment by External U.
10977 | September 22, 2015 03:03:22 PM GMT
this is still not fixed in 10,0,17,295085
Vote by External U.
10983 | September 22, 2015 03:03:56 PM GMT
Hi Adobe, I've verified this is fixed in CF2016 Update 1 (build 2016.0.01.298513). Thanks!, -Aaron
Comment by Aaron N.
31195 | August 25, 2019 04:23:48 AM GMT