@@ -1733,11 +1733,9 @@ func tomlAddToPath(path []string, tail string) []string {
17331733 return result
17341734}
17351735
1736- const maxManifestDepth = 500
1737-
17381736// tomlRenderValue returns a rendered value as string, with proper indenting
17391737func tomlRenderValue (i * interpreter , val value , sindent string , indexedPath []string , inline bool , cindent string , depth int ) (string , error ) {
1740- if depth > maxManifestDepth {
1738+ if depth <= 0 {
17411739 return "" , i .Error ("max manifest depth exceeded, possible infinite recursion" )
17421740 }
17431741 switch v := val .(type ) {
@@ -1781,7 +1779,7 @@ func tomlRenderValue(i *interpreter, val value, sindent string, indexedPath []st
17811779 }
17821780
17831781 res = res + newIndent
1784- value , err := tomlRenderValue (i , thunkValue , sindent , childIndexedPath , true , "" , depth + 1 )
1782+ value , err := tomlRenderValue (i , thunkValue , sindent , childIndexedPath , true , "" , depth - 1 )
17851783 if err != nil {
17861784 return "" , err
17871785 }
@@ -1812,7 +1810,7 @@ func tomlRenderValue(i *interpreter, val value, sindent string, indexedPath []st
18121810
18131811 childIndexedPath := tomlAddToPath (indexedPath , fieldName )
18141812
1815- value , err := tomlRenderValue (i , fieldValue , sindent , childIndexedPath , true , "" , depth + 1 )
1813+ value , err := tomlRenderValue (i , fieldValue , sindent , childIndexedPath , true , "" , depth - 1 )
18161814 if err != nil {
18171815 return "" , err
18181816 }
@@ -1831,7 +1829,7 @@ func tomlRenderValue(i *interpreter, val value, sindent string, indexedPath []st
18311829}
18321830
18331831func tomlRenderTableArray (i * interpreter , v * valueArray , sindent string , path []string , indexedPath []string , cindent string , depth int ) (string , error ) {
1834- if depth > maxManifestDepth {
1832+ if depth <= 0 {
18351833 return "" , i .Error ("max manifest depth exceeded, possible infinite recursion" )
18361834 }
18371835
@@ -1866,7 +1864,7 @@ func tomlRenderTableArray(i *interpreter, v *valueArray, sindent string, path []
18661864 childIndexedPath := tomlAddToPath (indexedPath , strconv .FormatInt (int64 (j ), 10 ))
18671865
18681866 // render the table and add it to result
1869- table , err := tomlTableInternal (i , tv , sindent , path , childIndexedPath , cindent + sindent , depth + 1 )
1867+ table , err := tomlTableInternal (i , tv , sindent , path , childIndexedPath , cindent + sindent , depth - 1 )
18701868 if err != nil {
18711869 return "" , err
18721870 }
@@ -1883,7 +1881,7 @@ func tomlRenderTableArray(i *interpreter, v *valueArray, sindent string, path []
18831881}
18841882
18851883func tomlRenderTable (i * interpreter , v * valueObject , sindent string , path []string , indexedPath []string , cindent string , depth int ) (string , error ) {
1886- if depth > maxManifestDepth {
1884+ if depth <= 0 {
18871885 return "" , i .Error ("max manifest depth exceeded, possible infinite recursion" )
18881886 }
18891887 res := cindent + "["
@@ -1898,7 +1896,7 @@ func tomlRenderTable(i *interpreter, v *valueObject, sindent string, path []stri
18981896 res = res + "\n "
18991897 }
19001898
1901- table , err := tomlTableInternal (i , v , sindent , path , indexedPath , cindent + sindent , depth + 1 )
1899+ table , err := tomlTableInternal (i , v , sindent , path , indexedPath , cindent + sindent , depth - 1 )
19021900 if err != nil {
19031901 return "" , err
19041902 }
@@ -1908,7 +1906,7 @@ func tomlRenderTable(i *interpreter, v *valueObject, sindent string, path []stri
19081906}
19091907
19101908func tomlTableInternal (i * interpreter , v * valueObject , sindent string , path []string , indexedPath []string , cindent string , depth int ) (string , error ) {
1911- if depth > maxManifestDepth {
1909+ if depth <= 0 {
19121910 return "" , i .Error ("max manifest depth exceeded, possible infinite recursion" )
19131911 }
19141912 resFields := []string {}
@@ -1937,13 +1935,13 @@ func tomlTableInternal(i *interpreter, v *valueObject, sindent string, path []st
19371935
19381936 switch fv := fieldValue .(type ) {
19391937 case * valueObject :
1940- section , err := tomlRenderTable (i , fv , sindent , childPath , childIndexedPath , cindent , depth + 1 )
1938+ section , err := tomlRenderTable (i , fv , sindent , childPath , childIndexedPath , cindent , depth - 1 )
19411939 if err != nil {
19421940 return "" , err
19431941 }
19441942 resSections = append (resSections , section )
19451943 case * valueArray :
1946- section , err := tomlRenderTableArray (i , fv , sindent , childPath , childIndexedPath , cindent , depth + 1 )
1944+ section , err := tomlRenderTableArray (i , fv , sindent , childPath , childIndexedPath , cindent , depth - 1 )
19471945 if err != nil {
19481946 return "" , err
19491947 }
@@ -1954,7 +1952,7 @@ func tomlTableInternal(i *interpreter, v *valueObject, sindent string, path []st
19541952 } else {
19551953 // render as value and append to result fields
19561954
1957- renderedValue , err := tomlRenderValue (i , fieldValue , sindent , childIndexedPath , false , "" , depth + 1 )
1955+ renderedValue , err := tomlRenderValue (i , fieldValue , sindent , childIndexedPath , false , "" , depth - 1 )
19581956 if err != nil {
19591957 return "" , err
19601958 }
@@ -1982,7 +1980,7 @@ func builtinManifestTomlEx(i *interpreter, arguments []value) (value, error) {
19821980
19831981 switch v := val .(type ) {
19841982 case * valueObject :
1985- res , err := tomlTableInternal (i , v , sindent , []string {}, []string {}, "" , 0 )
1983+ res , err := tomlTableInternal (i , v , sindent , []string {}, []string {}, "" , i . stack . limit )
19861984 if err != nil {
19871985 return nil , err
19881986 }
@@ -2022,7 +2020,7 @@ func builtinManifestJSONEx(i *interpreter, arguments []value) (value, error) {
20222020
20232021 var aux func (ov value , path []string , cindent string , depth int ) (string , error )
20242022 aux = func (ov value , path []string , cindent string , depth int ) (string , error ) {
2025- if depth > maxManifestDepth {
2023+ if depth <= 0 {
20262024 return "" , i .Error ("max manifest depth exceeded, possible infinite recursion" )
20272025 }
20282026 if ov == nil {
@@ -2057,7 +2055,7 @@ func builtinManifestJSONEx(i *interpreter, arguments []value) (value, error) {
20572055 }
20582056
20592057 newPath := append (path , strconv .FormatInt (int64 (aI ), 10 ))
2060- s , err := aux (cTv , newPath , newIndent , depth + 1 )
2058+ s , err := aux (cTv , newPath , newIndent , depth - 1 )
20612059 if err != nil {
20622060 return "" , err
20632061 }
@@ -2085,7 +2083,7 @@ func builtinManifestJSONEx(i *interpreter, arguments []value) (value, error) {
20852083 }
20862084
20872085 newPath := append (path , fieldName )
2088- mvs , err := aux (fieldValue , newPath , newIndent , depth + 1 )
2086+ mvs , err := aux (fieldValue , newPath , newIndent , depth - 1 )
20892087 if err != nil {
20902088 return "" , err
20912089 }
@@ -2101,7 +2099,7 @@ func builtinManifestJSONEx(i *interpreter, arguments []value) (value, error) {
21012099 }
21022100 }
21032101
2104- finalString , err := aux (val , path , "" , 0 )
2102+ finalString , err := aux (val , path , "" , i . stack . limit )
21052103 if err != nil {
21062104 return nil , err
21072105 }
@@ -2191,7 +2189,7 @@ func builtinManifestYamlDoc(i *interpreter, arguments []value) (value, error) {
21912189
21922190 var aux func (ov value , buf * bytes.Buffer , cindent string , depth int ) error
21932191 aux = func (ov value , buf * bytes.Buffer , cindent string , depth int ) error {
2194- if depth > maxManifestDepth {
2192+ if depth <= 0 {
21952193 return i .Error ("max manifest depth exceeded, possible infinite recursion" )
21962194 }
21972195 switch v := ov .(type ) {
@@ -2251,7 +2249,7 @@ func builtinManifestYamlDoc(i *interpreter, arguments []value) (value, error) {
22512249 cindent = cindent + yamlIndent
22522250 }
22532251
2254- if err := aux (thunkValue , buf , cindent , depth + 1 ); err != nil {
2252+ if err := aux (thunkValue , buf , cindent , depth - 1 ); err != nil {
22552253 return err
22562254 }
22572255 cindent = prevIndent
@@ -2301,7 +2299,7 @@ func builtinManifestYamlDoc(i *interpreter, arguments []value) (value, error) {
23012299 } else {
23022300 buf .WriteByte (' ' )
23032301 }
2304- if err := aux (fieldValue , buf , cindent , depth + 1 ); err != nil {
2302+ if err := aux (fieldValue , buf , cindent , depth - 1 ); err != nil {
23052303 return err
23062304 }
23072305 cindent = prevIndent
@@ -2310,7 +2308,7 @@ func builtinManifestYamlDoc(i *interpreter, arguments []value) (value, error) {
23102308 return nil
23112309 }
23122310
2313- if err := aux (val , & buf , "" , 0 ); err != nil {
2311+ if err := aux (val , & buf , "" , i . stack . limit ); err != nil {
23142312 return nil , err
23152313 }
23162314
0 commit comments