You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@trafficcontrol.apache.org by GitBox <gi...@apache.org> on 2021/05/13 16:15:54 UTC

[GitHub] [trafficcontrol] zrhoffman commented on a change in pull request #5844: Circumvent copylocks `go vet` check using unsafe Go

zrhoffman commented on a change in pull request #5844:
URL: https://github.com/apache/trafficcontrol/pull/5844#discussion_r631931430



##########
File path: grove/remap/remap.go
##########
@@ -497,7 +498,11 @@ func makeTo(tosJSON []RemapRuleToJSON, rule remapdata.RemapRule, baseTransport *
 				return nil, fmt.Errorf("error parsing to %v proxy_url: %v", to.URL, toJSON.ProxyURL)
 			}
 			to.ProxyURL = proxyURL
-			newTransport := *baseTransport
+			newTransport := http.Transport{}
+			const transportSize = unsafe.Sizeof(*baseTransport)
+			// Copy baseTransport to newTransport without failing the copylocks `go vet` check. See
+			// b7953cc239 for the reasoning behind copying the Transport.
+			copy((*[transportSize]byte)(unsafe.Pointer(&newTransport))[:], (*[transportSize]byte)(unsafe.Pointer(baseTransport))[:])

Review comment:
       We could do
   
   ```go
   newTransport := *baseTransport.Clone()
   ```
   but it would skip unexported fields like locks. We could use `reflect` to copy and set the unexported fields, but that would also involve using `unsafe`.
   
   As for silencing the check on this line, Go offers no such option. From [go/issues#17058](https://github.com/golang/go/issues/17058#issuecomment-259012764):
   
   > I am adamant that source code not be annotated to silence vet. That will reduce the options available but that is my position.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org