@@ -66,39 +66,35 @@ func NewHandle(rules any, keyObfuscatorRegex string, valueObfuscatorRegex string
6666 diagnosticsWafObj := new (bindings.WafObject )
6767 defer wafLib .WafObjectFree (diagnosticsWafObj )
6868
69- cHandle := wafLib .WafInit (obj , config , diagnosticsWafObj )
70- // Upon failure, the WAF may have produced some diagnostics to help signal what went wrong...
71- var (
72- diags = new (Diagnostics )
73- diagsErr error
74- )
75- if ! diagnosticsWafObj .IsInvalid () {
76- diags , diagsErr = decodeDiagnostics (diagnosticsWafObj )
69+ unsafe .KeepAlive (encoder .cgoRefs )
70+
71+ return newHandle (wafLib .WafInit (obj , config , diagnosticsWafObj ), diagnosticsWafObj )
72+ }
73+
74+ // newHandle creates a new Handle from a C handle (nullable) and a diagnostics object.
75+ // and it handles the multiple ways a WAF initialization can fail.
76+ func newHandle (cHandle bindings.WafHandle , diagnosticsWafObj * bindings.WafObject ) (* Handle , error ) {
77+ diags , diagsErr := decodeDiagnostics (diagnosticsWafObj )
78+ if cHandle == 0 && diagsErr != nil { // WAF Failed initialization and we manage to decode the diagnostics, return the diagnostics error
79+ if err := diags .TopLevelError (); err != nil {
80+ return nil , fmt .Errorf ("could not instantiate the WAF: %w" , err )
81+ }
7782 }
7883
7984 if cHandle == 0 {
8085 // WAF Failed initialization, report the best possible error...
81- if diags != nil && diagsErr == nil {
82- // We were able to parse out some diagnostics from the WAF!
83- err = diags .TopLevelError ()
84- if err != nil {
85- return nil , fmt .Errorf ("could not instantiate the WAF: %w" , err )
86- }
87- }
8886 return nil , errors .New ("could not instantiate the WAF" )
8987 }
9088
91- // The WAF successfully initialized at this stage...
89+ // The WAF successfully initialized at this stage but if the diagnostics decoding failed, we still need to cleanup
9290 if diagsErr != nil {
9391 wafLib .WafDestroy (cHandle )
9492 return nil , fmt .Errorf ("could not decode the WAF diagnostics: %w" , diagsErr )
9593 }
9694
97- unsafe .KeepAlive (encoder .cgoRefs )
98-
9995 handle := & Handle {
10096 cHandle : cHandle ,
101- diagnostics : * diags ,
97+ diagnostics : diags ,
10298 }
10399
104100 handle .refCounter .Store (1 ) // We count the handle itself in the counter
@@ -170,25 +166,9 @@ func (handle *Handle) Update(newRules any) (*Handle, error) {
170166 }
171167
172168 diagnosticsWafObj := new (bindings.WafObject )
173-
174- cHandle := wafLib .WafUpdate (handle .cHandle , obj , diagnosticsWafObj )
175- unsafe .KeepAlive (encoder .cgoRefs )
176- if cHandle == 0 {
177- return nil , errors .New ("could not update the WAF instance" )
178- }
179-
180169 defer wafLib .WafObjectFree (diagnosticsWafObj )
181170
182- if err != nil { // Something is very wrong
183- return nil , fmt .Errorf ("could not decode the WAF ruleset errors: %w" , err )
184- }
185-
186- newHandle := & Handle {
187- cHandle : cHandle ,
188- }
189-
190- newHandle .refCounter .Store (1 ) // We count the handle itself in the counter
191- return newHandle , nil
171+ return newHandle (wafLib .WafUpdate (handle .cHandle , obj , diagnosticsWafObj ), diagnosticsWafObj )
192172}
193173
194174// Close puts the handle in termination state, when all the contexts are closed the handle will be destroyed
0 commit comments