BLee15
Occasional Contributor
6 years agoHAL code of Character Buffer for VGA Display (altera_up_avalon_video_character_buffer_with_dma) contains code that modifies string literal
altera_up_avalon_video_character_buffer_with_dma.c contains following code:
void alt_up_char_buffer_init(alt_up_char_buffer_dev *char_buffer) {
char * name;
name = (char *) char_buffer->dev.name;
for ( ; (*name) != '\0'; name++) {
if (strcmp(name, "_avalon_char_buffer_slave") == 0) {
(*name) = '\0';
break;
}
}
return;
}The type of dev.name is const char* , which (normally) contains string literal.
This code does type conversion from const char* to char*, and then modifies contents of the string.
Therefore, this code is modifying contents of string literal, which causes undefined behavior.
I wrote the following code:
alt_up_char_buffer_dev* char_buffer = alt_up_char_buffer_open_dev(VIDEO_CHARACTER_BUFFER_AVALON_CHAR_BUFFER_SLAVE_NAME);When I compile this code with -O0, the return value was NULL.
When I compile this code with -O2, the return value was pointer of (correct) device structure.
Version: Quartus Prime 18.1.1 Build 646 04/11/2019 SJ Lite Edition