Fix longFilename NULL pointer dereference

This commit is contained in:
Jason Smith 2020-11-12 00:18:17 -08:00
parent 37b56e09a3
commit 1e88c9f673

View file

@ -1125,41 +1125,43 @@ int8_t SdBaseFile::readDir(dir_t* dir, char* longFilename) {
// Return if normal file or subdirectory // Return if normal file or subdirectory
if (DIR_IS_FILE_OR_SUBDIR(dir)) { if (DIR_IS_FILE_OR_SUBDIR(dir)) {
#if ENABLED(UTF_FILENAME_SUPPORT) #if ENABLED(UTF_FILENAME_SUPPORT)
// Convert filename from utf-16 to utf-8 as Marlin expects if (longFilename) {
#if LONG_FILENAME_CHARSIZE > 2 // Convert filename from utf-16 to utf-8 as Marlin expects
// Add warning for developers for currently not supported 3-byte cases (Conversion series of 2-byte #if LONG_FILENAME_CHARSIZE > 2
// codepoints to 3-byte in-place will break the rest of filename) // Add warning for developers for currently not supported 3-byte cases (Conversion series of 2-byte
#error "Currently filename re-encoding is done in-place. It may break the remaining chars to use 3-byte codepoints." // codepoints to 3-byte in-place will break the rest of filename)
#endif #error "Currently filename re-encoding is done in-place. It may break the remaining chars to use 3-byte codepoints."
uint16_t currentPos = 0; #endif
LOOP_L_N(i, (LONG_FILENAME_LENGTH / 2)) { uint16_t currentPos = 0;
uint16_t idx = i * 2; // This is fixed as FAT LFN always contain UTF-16LE encoding
uint16_t utf16_ch = longFilename[idx] | (longFilename[idx + 1] << 8); LOOP_L_N(i, (LONG_FILENAME_LENGTH / 2)) {
if (0xD800 == (utf16_ch & 0xF800)) // Surrogate pair - encode as '_' uint16_t idx = i * 2; // This is fixed as FAT LFN always contain UTF-16LE encoding
longFilename[currentPos++] = '_';
else if (0 == (utf16_ch & 0xFF80)) // Encode as 1-byte utf-8 char uint16_t utf16_ch = longFilename[idx] | (longFilename[idx + 1] << 8);
longFilename[currentPos++] = utf16_ch & 0x007F; if (0xD800 == (utf16_ch & 0xF800)) // Surrogate pair - encode as '_'
else if (0 == (utf16_ch & 0xF800)) { // Encode as 2-byte utf-8 char
longFilename[currentPos++] = 0xC0 | ((utf16_ch >> 6) & 0x1F);
longFilename[currentPos++] = 0x80 | (utf16_ch & 0x3F);
}
else {
#if LONG_FILENAME_CHARSIZE > 2 // Encode as 3-byte utf-8 char
longFilename[currentPos++] = 0xE0 | ((utf16_ch >> 12) & 0x0F);
longFilename[currentPos++] = 0xC0 | ((utf16_ch >> 6) & 0x3F);
longFilename[currentPos++] = 0xC0 | (utf16_ch & 0x3F);
#else // Encode as '_'
longFilename[currentPos++] = '_'; longFilename[currentPos++] = '_';
#endif else if (0 == (utf16_ch & 0xFF80)) // Encode as 1-byte utf-8 char
} longFilename[currentPos++] = utf16_ch & 0x007F;
else if (0 == (utf16_ch & 0xF800)) { // Encode as 2-byte utf-8 char
longFilename[currentPos++] = 0xC0 | ((utf16_ch >> 6) & 0x1F);
longFilename[currentPos++] = 0x80 | (utf16_ch & 0x3F);
}
else {
#if LONG_FILENAME_CHARSIZE > 2 // Encode as 3-byte utf-8 char
longFilename[currentPos++] = 0xE0 | ((utf16_ch >> 12) & 0x0F);
longFilename[currentPos++] = 0xC0 | ((utf16_ch >> 6) & 0x3F);
longFilename[currentPos++] = 0xC0 | (utf16_ch & 0x3F);
#else // Encode as '_'
longFilename[currentPos++] = '_';
#endif
}
if (0 == utf16_ch) break; // End of filename if (0 == utf16_ch) break; // End of filename
}
n = currentPos;
} }
return currentPos;
#else
return n;
#endif #endif
return n;
} }
} }
} }