2016-03-21 43 views
0

我在一些安全關鍵汽車模塊中運行代碼。下面是代碼的一個粗略估計:丟失揮發性限定

在下面的代碼是一個模塊的一部分 - 「主模塊」,擁有該易失性可變/數組「x_ast」

Main Module.c 

//The structure x contains the major data that needs to be stored in case of a crash event. i.e. a real car crash 
// x contains data such a vehicle speed, environment data, sensor data, CAN related data,etc. Basically x itself has lot of structures inside it. 


typedef struct x x_tst; 
volatile x_tst x_ast[5]; 
//x_ast is being used in realtime and background functions, considering proper interrupt disabling and enabling. 

在下面的代碼是模塊的一部分 - 「DependentModule」,可以共享緩衝區「x_ast」。

DependentModule.c 

extern volatile x_tst x_ast[5]; 


//x_ast is owned by a separate module 
//Now I need to share this buffer "x_ast" to a different module that will inturn fill data in it for some conditions. Say for a particular condition that is activated, the buffer "x_ast" will be shared across to a module "Conditional Data Record". 
//The base address of first indexed buffer "x_ast[1]" is provided to "Conditional Data Record" for access. 
//The main module will not access the buffer that is shared, once the "Conditional Data Record" is activated/requested. 

// Below is a mechanism to share the buffer "x_ast" to module "Conditional Data Record". 

// This API will be called by module - "Conditional Data Record" once it is activated. It is ensured that this takes place only during intialization of whole system, and no other time. 

boolean GetConditionalBuffer(uint8 **PtrToBuffer) 
{ 
    boolean BufferShared = FALSE; 
    void* Temp_ptr; 
    *PtrToBuffer = NULL; 

// if module "Conditional Data Record" is activated? then share the buffer "x_ast", else do not share the buffer. 

if(Conditional Data Record == activated) { 
    Temp_ptr = (x_tst*)&x_ast[1]; 
    *PtrToBuffer = Temp_ptr; 
    BufferShared = TRUE; 
} 

return BufferShared; 

} 



Referring to the line of code: 
Temp_ptr = (x_tst*)&x_ast[1]; 

的代碼(Tempptr =(x_tst *)& x_ast [1])的上面的行拋出一個警告「消息(7:0312)。危險指針鑄造導致揮發性資格的損失 「 上述警告是強制性警告,因此有必要解決它。

我知道我將一個volatile變量的地址賦值給一個void指針,導致volatile限定的丟失。 我嘗試了不同的方法試圖解決警告,但無法得到確鑿的方法。

有沒有什麼辦法,我可以修改代碼並刪除這個警告,或者可以繞過這個警告。

+1

而不是使用'volatile void ** PtrToBuffer'函數。另一種選擇是在不同的緩衝區中完成您的工作,並根據需要將其複製到易失性緩衝區中。後者風險較小。請記住,您將需要鎖定緩衝區以防止您自己併發訪問以及其中一個後臺功能。 –

+0

在「條件數據記錄」模塊中處理併發條件。確保避免比賽條件。 –

+0

如果你的意思是該函數鎖定了緩衝區(並且稍後你將要解鎖它),那麼使該函數採用'volatile void ** PtrToBuffer'。 (不易揮發uint8_t)。拋出volatile和write會導致未定義的行爲,即使你鎖定了緩衝區。您需要使用賦值運算符一次讀取和寫入緩衝區1的值。 –

回答

1

如果您將值分配給易失性對象或在不使用易失性限定指針的情況下讀取易失性對象的值,則會出現未定義的行爲。

編譯器必須以比非易失性對象更嚴格的方式處理易失性對象(這意味着將非易失性對象當作易失性對象處理很好,將易失性對象視爲非易失性對象可以有壞的後果)。

將易失性對象的地址投射到非易失性指針使您面臨嚴重風險。不要這樣做。無論誰使用void *指針都會引發未定義的行爲。例如,僅使用memcpy複製該易失性數組是未定義的行爲。任何事情,通常都是壞事,都可能發生。

聲明一個功能

boolean GetConditionalBuffer(volatile x_tst **PtrToBuffer) 

因爲揮發性x_tst *是它存儲到PtrToBuffer。爲什麼你會丟棄類型信息?我實際上將其更改爲

volatile x_tst* GetConditionalBuffer (void); 

這使得它更容易開發人員的大腦,使功能更容易使用。

+0

這裏的另一個依賴是模塊「Conditional Data Record」將逐字節訪問共享緩衝區「x_ast」。即模塊「條件數據記錄」需要一個大小爲7KB的緩衝器用於其操作。 x_ast [1] ... x_ast [5]:總大小約爲8KB。因此,模塊「條件數據記錄」不會將x緩存作爲x_ast使用,而只是將其用作8KB的數組 –