Commit 7ba66cab authored by Mitsuru Kariya's avatar Mitsuru Kariya Committed by Daniel P. Berrangé
Browse files

Fix virDomainInfo.state type from int to byte



The C struct defines state as unsigned char (1 byte), but the JNA
mapping used int (4 bytes). This causes two problems:

- On big-endian systems, the 1-byte state value is stored at the
  beginning of the field, but reading it as a 4-byte int interprets
  it as the most significant byte, producing a completely wrong
  value (e.g. state=1 would be read as 0x01000000).
- On little-endian systems, the state byte is correctly placed in
  the least significant position, but the remaining 3 padding bytes
  are also read as part of the int. These padding bytes may contain
  garbage, corrupting the value. In practice this was masked by
  libvirt zero-initializing the struct, but it is not guaranteed by
  the C specification.

Changed to byte to match the C type, and added a bitmask (& 0xFF)
when converting to the DomainState enum index to handle the
unsigned-to-signed conversion correctly.

Signed-off-by: default avatarMitsuru Kariya <mitsuru.kariya@nttdata.com>
parent cee8d5b0
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ public class DomainInfo {
        maxMem = info.maxMem.longValue();
        memory = info.memory.longValue();
        nrVirtCpu = info.nrVirtCpu;
        state = DomainState.values()[info.state];
        state = DomainState.values()[info.state & 0xFF];
    }

    @Override
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import com.sun.jna.Structure;
 * JNA mapping for the virDomainInfo structure
 */
public class virDomainInfo extends Structure {
    public int state;
    public byte state;
    public NativeLong maxMem;
    public NativeLong memory;
    public short nrVirtCpu;