Skip to content

Commit 036a4d1

Browse files
committed
api: use ParseInt and add int32 bounds check
Replaces `strconv.Atoi` with `strconv.ParseInt` to ensure proper `int32` handling and adds a bounds check to prevent out-of-range integer values. All error conditions result in `ErrKeyNameNotFound` being returned. Signed-off-by: Ryan Johnson <[email protected]>
1 parent 63177e0 commit 036a4d1

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

object/custom_fields_manager.go

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
/*
2-
Copyright (c) 2015 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package object
186

197
import (
208
"context"
219
"errors"
10+
"math"
2211
"strconv"
2312

2413
"github.com/vmware/govmomi/vim25"
@@ -127,9 +116,12 @@ func (m CustomFieldsManager) FindKey(ctx context.Context, name string) (int32, e
127116
}
128117
}
129118

130-
k, err := strconv.Atoi(name)
131-
if err == nil {
132-
// assume literal int key
119+
k, err := strconv.ParseInt(name, 10, 32)
120+
if err != nil {
121+
return -1, ErrKeyNameNotFound
122+
}
123+
124+
if k >= math.MinInt32 && k <= math.MaxInt32 {
133125
return int32(k), nil
134126
}
135127

0 commit comments

Comments
 (0)