Home > Software design >  Why does this char comparison cause errors?
Why does this char comparison cause errors?

Time:11-14

In this function I am trying to check whether a char array contains a broadcast MAC address. I compare each array element with 0xFF to do this.

static inline bool recibir_trama_l2_en_interface(interface_t *interface, cab_ethernet_t *cab_ethernet) {
    char *mac = MAC_IF(interface);
    if(IF_EN_MODO_L3(interface)) {
        char *mac_destino = cab_ethernet->mac_destino.dir_mac;
        mostrar_dir_mac(&interface->prop_intf->dir_mac);
        mostrar_dir_mac(&cab_ethernet->mac_destino);
        bool bandera_prueba = true;
        bandera_prueba = mac_destino[0] == 0xFF;            

        if(mac_destino[0] == 0xFF && mac_destino[1] == 0xFF && mac_destino[2] == 0xFF && mac_destino[3] == 0xFF && mac_destino[4] == 0xFF && mac_destino[5] == 0xFF) {
            printf("Esto está pasando.\n");
        }
        printf("AABBNINKD.\n");
        if(mac_destino[0] == 0xFF) {
            printf("Esto sí pasa.\n");
        }    
    }
    return false;
}

These are the structs that I am working with.

typedef struct cab_ethernet_ {
    dir_mac_t mac_destino;
    dir_mac_t mac_origen;
    short tipo;
    char payload[TAM_MAX_PAYLOAD];
    unsigned int FCS;
} cab_ethernet_t;

typedef struct dir_mac_ {
    char dir_mac[TAM_DIR_MAC];
} dir_mac_t;

The debugger shows me that the content of mac_destino[0] is 0xFF. But you can also see that after the comparison, bandera_prueba is set to false. enter image description here

Another thing that is happenning is that apparently the program is skipping these instructions.

if(mac_destino[0] == 0xFF && mac_destino[1] == 0xFF && mac_destino[2] == 0xFF && mac_destino[3] == 0xFF && mac_destino[4] == 0xFF && mac_destino[5] == 0xFF) {
    printf("Esto está pasando.\n");
}

if(mac_destino[0] == 0xFF) {
    printf("Esto sí pasa.\n");
}

I mean, the debugger jumps from line 78 to line 83 to line 89. enter image description here Is there something wrong with this kind of comparison that is causing these errors?

CodePudding user response:

The value of the constant 0xFF is 255. In your C implementation, char is signed and can only have the values −128 to 127. mac_destino[0] is a char. Therefore mac_destino[0] == 0xFF can never be true. Stepping through the code in the debugger appears to skip lines because the compiler has optimized the program to omit the impossible parts.

To fix this, change the type used to unsigned char.

Preferably, change the element type of dir_mac in struct dir_mac_ to unsigned char and the type of mac_destino to unsigned char *. If you cannot do that, change the definition of mac_destino from char *mac_destino = cab_ethernet->mac_destino.dir_mac; to unsigned char *mac_destino = (unsigned char *) cab_ethernet->mac_destino.dir_mac;.

If you cannot do that, you can insert a conversion into each comparison, as by changing mac_destino[0] == 0xFF to (unsigned char) mac_destino[0] == 0xFF.

  • Related