Fix implicit memory aliasing in for loop

Signed-off-by: Evan Lezar <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2023-08-25 17:50:43 +02:00
parent 1b16b341dd
commit 709e27bf4b
11 changed files with 14 additions and 0 deletions

View File

@ -34,6 +34,7 @@ var _ oci.SpecModifier = (*fromCDISpec)(nil)
// Modify applies the mofiications defined by the raw CDI spec to the incomming OCI spec.
func (m fromCDISpec) Modify(spec *specs.Spec) error {
for _, device := range m.cdiSpec.Devices {
device := device
cdiDevice := cdi.Device{
Device: &device,
}

View File

@ -49,6 +49,7 @@ func (m nvidiaContainerRuntimeHookRemover) Modify(spec *specs.Spec) error {
var newPrestart []specs.Hook
for _, hook := range spec.Hooks.Prestart {
hook := hook
if isNVIDIAContainerRuntimeHook(&hook) {
m.logger.Debugf("Removing hook %v", hook)
continue

View File

@ -48,6 +48,7 @@ func (m stableRuntimeModifier) Modify(spec *specs.Spec) error {
// If an NVIDIA Container Runtime Hook already exists, we don't make any modifications to the spec.
if spec.Hooks != nil {
for _, hook := range spec.Hooks.Prestart {
hook := hook
if isNVIDIAContainerRuntimeHook(&hook) {
m.logger.Infof("Existing nvidia prestart hook (%v) found in OCI spec", hook.Path)
return nil

View File

@ -150,6 +150,8 @@ func TestAddHookModifier(t *testing.T) {
}
for _, tc := range testCases {
tc := tc
logHook.Reset()
t.Run(tc.description, func(t *testing.T) {

View File

@ -70,6 +70,7 @@ func TestNewMountSpecFromLine(t *testing.T) {
for i, tc := range testCases {
t.Run(fmt.Sprintf("test case %d", i), func(t *testing.T) {
tc := tc
target, err := NewMountSpecFromLine(tc.line)
if tc.expectedError != nil {
require.Error(t, err)

View File

@ -39,6 +39,7 @@ func (d dedupe) Transform(spec *specs.Spec) error {
}
var updatedDevices []specs.Device
for _, device := range spec.Devices {
device := device
if err := d.transformEdits(&device.ContainerEdits); err != nil {
return err
}

View File

@ -111,6 +111,7 @@ func mergeDeviceSpecs(deviceSpecs []specs.Device, mergedDeviceName string) (*spe
mergedEdits := edits.NewContainerEdits()
for _, d := range deviceSpecs {
d := d
edit := cdi.ContainerEdits{
ContainerEdits: &d.ContainerEdits,
}

View File

@ -39,6 +39,7 @@ func (r remove) Transform(spec *specs.Spec) error {
}
for _, device := range spec.Devices {
device := device
if err := r.transformEdits(&device.ContainerEdits); err != nil {
return fmt.Errorf("failed to remove edits from device %q: %w", device.Name, err)
}

View File

@ -54,6 +54,7 @@ func (t rootTransformer) Transform(spec *specs.Spec) error {
}
for _, d := range spec.Devices {
d := d
if err := t.applyToEdits(&d.ContainerEdits); err != nil {
return fmt.Errorf("failed to apply root transform to device %s: %w", d.Name, err)
}

View File

@ -44,6 +44,7 @@ func (d sorter) Transform(spec *specs.Spec) error {
}
var updatedDevices []specs.Device
for _, device := range spec.Devices {
device := device
if err := d.transformEdits(&device.ContainerEdits); err != nil {
return err
}

View File

@ -235,6 +235,8 @@ func TestUpdateConfig(t *testing.T) {
}
for i, tc := range testCases {
tc := tc
o := &options{
Options: container.Options{
RuntimeName: tc.runtimeName,
@ -361,6 +363,7 @@ func TestRevertConfig(t *testing.T) {
}
for i, tc := range testCases {
tc := tc
o := &options{}
err := o.RevertConfig(&tc.config)