arduino-libraries / SD

SD Library for Arduino
http://arduino.cc/
GNU General Public License v3.0
179 stars 155 forks source link

WARNING: taking address of packed member of 'directoryEntry' may result in an unaligned pointer value #157

Open brightproject opened 1 month ago

brightproject commented 1 month ago

My platformio.ini

[platformio] description = BNO055 default_envs = genericSTM32F411CE

[env:genericSTM32F401CE] platform = ststm32

[env:genericSTM32F411CE] platform = ststm32 platform_packages = platformio/framework-arduinoststm32@^4.20801.240802

board = blackpill_f411ce framework = arduino

monitor_speed = 230400 monitor_port = COM6

; debug_tool = stlink ; upload_protocol = dfu ; upload_protocol = stlink

lib_deps = adafruit/Adafruit BNO055@^1.6.3

; change MCU frequency board_build.f_cpu = 100000000L

When compiling code with library

name=SD version=1.3.0

I received warnings:

SdFile.cpp:1175:17: warning: taking address of packed member of 'directoryEntry' may result in an unaligned pointer value [-Waddress-of-packed-member]
 1175 |       dateTime_(&d->lastWriteDate, &d->lastWriteTime);

Replaced these functions

https://github.com/arduino-libraries/SD/blob/a3866205cf9af4eea3e10dcdc5ac2c713f293337/src/utility/SdFile.cpp#L530

with

  // set timestamps
if (dateTime_) {
    // call user function using temporary variables for safety
    uint16_t creationDate = p->creationDate;
    uint16_t creationTime = p->creationTime;
    dateTime_(&creationDate, &creationTime);

   // After getting new values ​​from dateTime_, you can assign them back if needed.
    p->creationDate = creationDate;
    p->creationTime = creationTime;
} else {
    // use default date/time
    p->creationDate = FAT_DEFAULT_DATE;
    p->creationTime = FAT_DEFAULT_TIME;
}

p->lastAccessDate = p->creationDate;
p->lastWriteDate = p->creationDate;
p->lastWriteTime = p->creationTime;

and

https://github.com/arduino-libraries/SD/blob/a3866205cf9af4eea3e10dcdc5ac2c713f293337/src/utility/SdFile.cpp#L1154

with

// set modify time if user supplied a callback date/time function
if (dateTime_) {
// Create temporary variables to safely call dateTime_
uint16_t lastWriteDate = d->lastWriteDate;
uint16_t lastWriteTime = d->lastWriteTime;

// Call the function with temporary variables
dateTime_(&lastWriteDate, &lastWriteTime);

// If necessary, save updated values ​​back to the structure
d->lastWriteDate = lastWriteDate;
d->lastWriteTime = lastWriteTime;

// Update lastAccessDate based on lastWriteDate d->lastAccessDate = d->lastWriteDate;
}

After these changes, yellow warnings during compilation disappeared.