@@ -82,33 +82,42 @@ private void generatePciXml(StringBuilder gpuBuilder) {
8282 // Parse the bus address into domain, bus, slot, function. Two input formats are accepted:
8383 // - "dddd:bb:ss.f" full PCI address with domain (e.g. 0000:00:02.0)
8484 // - "bb:ss.f" legacy short BDF; domain defaults to 0000
85- String domain = "0x0000" ;
86- String bus = "0x00" ;
87- String slot = "0x00" ;
88- String function = "0x0" ;
85+ // Each segment is parsed as a hex integer and formatted with fixed widths
86+ // (domain: 4 hex digits, bus/slot: 2 hex digits, function: 1 hex digit) to
87+ // produce canonical libvirt XML values regardless of input casing or padding.
88+ int domainVal = 0 , busVal = 0 , slotVal = 0 , funcVal = 0 ;
8989
9090 if (busAddress != null && !busAddress .isEmpty ()) {
9191 String [] parts = busAddress .split (":" );
9292 String slotFunction = null ;
93- if (parts .length == 3 ) {
94- domain = "0x" + parts [0 ];
95- bus = "0x" + parts [1 ];
96- slotFunction = parts [2 ];
97- } else if (parts .length == 2 ) {
98- bus = "0x" + parts [0 ];
99- slotFunction = parts [1 ];
100- }
101- if (slotFunction != null ) {
102- String [] slotFunctionParts = slotFunction .split ("\\ ." );
103- if (slotFunctionParts .length > 0 ) {
104- slot = "0x" + slotFunctionParts [0 ];
105- if (slotFunctionParts .length > 1 ) {
106- function = "0x" + slotFunctionParts [1 ].trim ();
93+ try {
94+ if (parts .length == 3 ) {
95+ domainVal = Integer .parseInt (parts [0 ], 16 );
96+ busVal = Integer .parseInt (parts [1 ], 16 );
97+ slotFunction = parts [2 ];
98+ } else if (parts .length == 2 ) {
99+ busVal = Integer .parseInt (parts [0 ], 16 );
100+ slotFunction = parts [1 ];
101+ }
102+ if (slotFunction != null ) {
103+ String [] slotFunctionParts = slotFunction .split ("\\ ." );
104+ if (slotFunctionParts .length > 0 ) {
105+ slotVal = Integer .parseInt (slotFunctionParts [0 ], 16 );
106+ if (slotFunctionParts .length > 1 ) {
107+ funcVal = Integer .parseInt (slotFunctionParts [1 ].trim (), 16 );
108+ }
107109 }
108110 }
111+ } catch (NumberFormatException e ) {
112+ // leave all values at 0 (safe defaults)
109113 }
110114 }
111115
116+ String domain = String .format ("0x%04x" , domainVal );
117+ String bus = String .format ("0x%02x" , busVal );
118+ String slot = String .format ("0x%02x" , slotVal );
119+ String function = String .format ("0x%x" , funcVal );
120+
112121 gpuBuilder .append (" <address domain='" ).append (domain ).append ("' bus='" ).append (bus ).append ("' slot='" )
113122 .append (slot ).append ("' function='" ).append (function .trim ()).append ("'/>\n " );
114123 gpuBuilder .append (" </source>\n " );
0 commit comments