Skip to content

Commit 2f2b8d2

Browse files
committed
Bound file-exists checks to MAX_PATH
1 parent 322738f commit 2f2b8d2

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/adam/model/std_factories/std_model.py

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,36 +23,43 @@ def urdf_remove_sensors_tags(xml_string):
2323
return modified_xml_string
2424

2525

26-
def get_xml_string(path: str):
27-
isPath = False
26+
def get_xml_string(path: str | pathlib.Path):
27+
28+
isPath = isinstance(path, pathlib.Path)
2829
isUrdf = False
30+
31+
# Extract the maximum path length for the current OS
32+
try:
33+
from ctypes.wintypes import MAX_PATH
34+
except ValueError:
35+
MAX_PATH = os.pathconf("/", "PC_PATH_MAX")
36+
2937
# Checking if it is a path or an urdf
30-
if type(path) is not (pathlib.Path):
31-
if os.path.exists(path):
38+
if not isPath:
39+
if len(path) <= MAX_PATH and os.path.exists(path):
3240
path = pathlib.Path(path)
3341
isPath = True
3442
else:
3543
root = ET.fromstring(path)
36-
robot_el = None
44+
3745
for elem in root.iter():
3846
if elem.tag == "robot":
3947
xml_string = path
4048
isUrdf = True
41-
elif path.exists():
42-
isPath = True
49+
break
50+
51+
if isPath:
52+
if not path.is_file():
53+
raise FileExistsError(path)
54+
55+
with path.open() as xml_file:
56+
xml_string = xml_file.read()
4357

44-
if not (isPath) and not (isUrdf):
58+
if not isPath and not isUrdf:
4559
raise ValueError(
4660
f"Invalid urdf string: {path}. It is neither a path nor a urdf string"
4761
)
4862

49-
if isPath:
50-
if not (path.exists()):
51-
raise FileExistsError(path)
52-
path = pathlib.Path(path)
53-
with open(path, "r") as xml_file:
54-
xml_string = xml_file.read()
55-
xml_file.close()
5663
return xml_string
5764

5865

0 commit comments

Comments
 (0)