definition-variables

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
alex4
Level 1
Level 1
5 sign-ins First reply posted First question asked

hello

used PSoc 4.2.

i defined a uint8 XXX variable.

in another file, by mistake,  this variable defined as: extern uint32 XXX;

there was no warning. in MAP file the XXX is located as 1 byte. but we have a memory override of another variable that  located in the near address.

please advice if there is a way to check such cases automatically (by compiler....)

thanks in advance

Alexander

0 Likes
1 Solution
Charles_Lai
Moderator
Moderator
Moderator
500 replies posted 250 solutions authored 250 sign-ins

Hi,

You got no warning due to the implicit type conversion of XXX and it's a secure type conversion (uint8 -> uint32). It's a legit and designed manner so no warning is given by default.

In your case, it's equivalent to:

 

uint8 XXX = 1;
uint32 YYY = 2222222;
*(uint32*)&XXX = YYY;

 

The above example gives no warning, too. But it violates the allocation of XXX and memory overlapping happens.

This behavior is recognized as user-responsible so you can't expect an auto-check by the build system.

Best regards

View solution in original post

0 Likes
4 Replies
Charles_Lai
Moderator
Moderator
Moderator
500 replies posted 250 solutions authored 250 sign-ins

Hi,

You got no warning due to the implicit type conversion of XXX and it's a secure type conversion (uint8 -> uint32). It's a legit and designed manner so no warning is given by default.

In your case, it's equivalent to:

 

uint8 XXX = 1;
uint32 YYY = 2222222;
*(uint32*)&XXX = YYY;

 

The above example gives no warning, too. But it violates the allocation of XXX and memory overlapping happens.

This behavior is recognized as user-responsible so you can't expect an auto-check by the build system.

Best regards

0 Likes

thank you for clarification

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I also tried your case, and reproduced the same problem.

IMHO, Linker has the only chance to detect this problem and (I think that) should flag a warning.
But as far as I tried, I could not find a linker flag to warn linking the variable with different size(s).

Since GCC has millions of runtime arguments, may be there is/are some flag to take care of this,

but I could not afford to search all. (I'm sorry)

 

Although I understand that this is not what you expected,
I tried to have a header file which defines all "external".
Then the compiler could report error and stopped.

In the followings, if I uncomment the definition of 

extern uint32_t XXX ;

in sub.h,  the compiler can generate error.

If I keep it commented, the project compiles OK,

but as you wrote, crashes when the line of assignment to XXX is executed. >_<

 

sub.h

#ifndef _SUB_H_
#define _SUB_H_
#include "project.h"

// extern uint32_t XXX ;
    
void sub_function(int32_t a) ;

#endif

sub.c

#include "project.h"
#include "sub.h"

extern uint32_t XXX ;

void sub_function(int32_t val)
{
    XXX = val ;
}

main.c

#include "project.h"
#include "stdio.h"
#include "sub.h"

uint8_t WWW = 12 ;
uint8_t XXX = 0xCD ;
uint8_t YYY = 34 ;
uint8_t ZZZ = 56 ;

char str[128] ;

void print_values(void)
{
    snprintf(str, 128, "%d, %d, %d, %d\n\r",
        WWW, XXX, YYY, ZZZ) ;
    UART_UartPutString(str) ;
}

int main(void)
{
    uint32_t i ;
    
    CyGlobalIntEnable; /* Enable global interrupts. */
    UART_Start() ;
    
    UART_UartPutString("\x1b[2J\x1b[;H") ;
    print_values() ;

    for(;;)
    {
        for (i = 0 ; i < 0x100 ; i++ ) {
            sub_function(i) ;
            print_values() ;
            CyDelay(500) ;
        }
    }
}

moto

0 Likes
alex4
Level 1
Level 1
5 sign-ins First reply posted First question asked

thank you

0 Likes