gophercloud icon indicating copy to clipboard operation
gophercloud copied to clipboard

Fix panic in ExtractIntoStructPtr

Open bobuhiro11 opened this issue 2 years ago • 6 comments

Passing nil as an argument to 'ExtractIntoStructPtr' causes "panic: runtime error: invalid memory address or nil pointer dereference". This is an invalid argument and should be corrected to return an error. For reference, standard package functions such as 'json.Unmarshal' return an error instead of panic.

Fixes #2872

bobuhiro11 avatar Jan 19 '24 07:01 bobuhiro11

Coverage Status

coverage: 77.875% (+0.04%) from 77.832% when pulling 571d3f585aa2baa39408ec51e9274940d00e8596 on bobuhiro11:fix_extract into d0c1d7e079aced9455238c7148da2ca6555d1ac5 on gophercloud:master.

coveralls avatar Jan 19 '24 09:01 coveralls

The comment to the function you are modifying specifically mentions that it's "for internal use only". However I realise that since it's exported, it's integral part of the API surface.

I think your change request has merit and it's correctly written. Can you please do the same for ExtractIntoSlicePtr just below?

pierreprinetti avatar Jan 19 '24 09:01 pierreprinetti

I have a last-minute doubt about this PR. see https://github.com/gophercloud/gophercloud/issues/2872#issuecomment-1900099365

pierreprinetti avatar Jan 19 '24 10:01 pierreprinetti

@pierreprinetti @kayrus Thanks for your review!

I think your change request has merit and it's correctly written. Can you please do the same for ExtractIntoSlicePtr just below?

Of course. I added the same change for ExtractIntoSlicePtr.

thanks for the PR. I think it's also worth adding a check, whether interface is not a pointer. And an error message should be more specific, this should help during the development.

I appied your suggestions. reflect.ValueOf(to).IsNil() was needed for err5.

bobuhiro11 avatar Jan 19 '24 13:01 bobuhiro11

@pierreprinetti @kayrus Could you please take a look at it again?

bobuhiro11 avatar Feb 19 '24 05:02 bobuhiro11

@kayrus are you fine with the changes you requested?

pierreprinetti avatar Jul 19 '24 07:07 pierreprinetti