Skip to content

Commit

Permalink
Fix #408: Parser can blow up if no [preset00] header (#450)
Browse files Browse the repository at this point in the history
* Fix: Parser can blow up if no [preset00] header

* fix no debug mode

* Remove assert that is blowing up because it looks like this is sort of handled.

* Only do rdft() on powers of 2 samples

* Only do rdft() on powers of 2 samples

* Revert changes to PCM.cpp sampling, needs more thoughtful fixing
  • Loading branch information
revmischa authored Feb 12, 2021
1 parent 67a1999 commit 8913b06
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 33 deletions.
Binary file removed bad_presets.tar
Binary file not shown.
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
AC_INIT([projectM], [3.1.9], [[email protected]], [projectM], [https://github.com/projectM-visualizer/projectm/])
AC_INIT([projectM], [3.1.10], [[email protected]], [projectM], [https://github.com/projectM-visualizer/projectm/])
AM_INIT_AUTOMAKE([-Wall -Werror foreign subdir-objects tar-pax])

AX_IS_RELEASE([git-directory])
AX_CHECK_ENABLE_DEBUG([no], [DEBUG])

m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
LT_INIT

Expand Down
8 changes: 8 additions & 0 deletions src/libprojectM/FileScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ std::string FileScanner::extensionMatches(std::string &filename) {
return {};
}

bool FileScanner::isValidFilename(std::string &filename) {
if (filename.find("__MACOSX") != std::string::npos) return false;
return true;
}

// generic implementation using dirent
void FileScanner::scanGeneric(ScanCallback cb, const char *currentDir) {
Expand All @@ -93,6 +97,8 @@ void FileScanner::scanGeneric(ScanCallback cb, const char *currentDir) {
// Convert char * to friendly string
std::string filename(dir_entry->d_name);

// Some sanity checks
if (! isValidFilename(filename)) continue;
if (filename.length() > 0 && filename[0] == '.')
continue;

Expand Down Expand Up @@ -165,6 +171,8 @@ void FileScanner::scanPosix(ScanCallback cb) {
path = std::string(node->fts_path);
name = std::string(node->fts_name);

if (!isValidFilename(path) || !isValidFilename(name)) break;

// check extension
nameMatched = extensionMatches(name);
if (! nameMatched.empty())
Expand Down
25 changes: 13 additions & 12 deletions src/libprojectM/FileScanner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,24 +25,25 @@ extern "C"
#include "dirent.h"
#endif

typedef std::function<void (std::string &path, std::string &name)> ScanCallback;
typedef std::function<void(std::string &path, std::string &name)> ScanCallback;

class FileScanner {
class FileScanner
{
public:
FileScanner();
FileScanner(std::vector<std::string> &rootDirs, std::vector<std::string> &extensions);
FileScanner();
FileScanner(std::vector<std::string> &rootDirs, std::vector<std::string> &extensions);

void scan(ScanCallback cb);
std::string extensionMatches(std::string &filename);
void scan(ScanCallback cb);
std::string extensionMatches(std::string &filename);

private:
std::vector<std::string> _rootDirs;
std::vector<std::string> _extensions;
std::vector<std::string> _rootDirs;
std::vector<std::string> _extensions;

void scanGeneric(ScanCallback cb, const char *dir);
void scanPosix(ScanCallback cb);
void handleDirectoryError(std::string dir);
void scanGeneric(ScanCallback cb, const char *dir);
void scanPosix(ScanCallback cb);
void handleDirectoryError(std::string dir);
bool isValidFilename(std::string &filename);
};


#endif /* FileScanner_hpp */
31 changes: 16 additions & 15 deletions src/libprojectM/MilkdropPresetFactory/MilkdropPreset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,25 +459,26 @@ int MilkdropPreset::readIn(std::istream & fs) {
presetOutputs().compositeShader.programSource.clear();
presetOutputs().warpShader.programSource.clear();

/* Parse any comments */
if (Parser::parse_top_comment(fs) < 0)
/* Parse any comments (aka "[preset00]") */
/* We don't do anything with this info so it's okay if it's missing */
if (Parser::parse_top_comment(fs) == PROJECTM_SUCCESS)
{
/* Parse the preset name and a left bracket */
char tmp_name[MAX_TOKEN_SIZE];

if (Parser::parse_preset_name(fs, tmp_name) < 0)
{
std::cerr << "[Preset::readIn] loading of preset name failed" << std::endl;
fs.seekg(0);
}
/// @note We ignore the preset name because [preset00] is just not so useful
} else {
// no comment found. whatever
if (MILKDROP_PRESET_DEBUG)
std::cerr << "[Preset::readIn] no left bracket found..." << std::endl;
return PROJECTM_FAILURE;
}

/* Parse the preset name and a left bracket */
char tmp_name[MAX_TOKEN_SIZE];

if (Parser::parse_preset_name(fs, tmp_name) < 0)
{
std::cerr << "[Preset::readIn] loading of preset name failed" << std::endl;
return PROJECTM_ERROR;
std::cerr << "[Preset::readIn] no left bracket found..." << std::endl;
fs.seekg(0);
}

/// @note We ignore the preset name because [preset00] is just not so useful

// Loop through each line in file, trying to successfully parse the file.
// If a line does not parse correctly, keep trucking along to next line.
int retval;
Expand Down
7 changes: 6 additions & 1 deletion src/libprojectM/MilkdropPresetFactory/MilkdropPreset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@
#include <cassert>
#include <map>

#define MILKDROP_PRESET_DEBUG 0 /* 0 for no debugging, 1 for normal, 2 for insane */
#ifdef DEBUG
/* 0 for no debugging, 1 for normal, 2 for insane */
#define MILKDROP_PRESET_DEBUG 1
#else
#define MILKDROP_PRESET_DEBUG 0
#endif

#include "CustomShape.hpp"
#include "CustomWave.hpp"
Expand Down
15 changes: 11 additions & 4 deletions src/libprojectM/MilkdropPresetFactory/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ token_t Parser::parseToken(std::istream & fs, char * string)

last_token_size++;
/* If the string line buffer is full, quit */
if (string_line_buffer_index == (STRING_LINE_SIZE - 1))
return tStringBufferFilled;
if (string_line_buffer_index == (STRING_LINE_SIZE - 1)) {
if (PARSE_DEBUG) {
string_line_buffer[string_line_buffer_index++] = '\0';
std::cout << "ERROR String line buffer full. Buffer: " << string_line_buffer << std::endl;
}
return tStringBufferFilled;
}

/* Otherwise add this character to the string line buffer */
string_line_buffer[string_line_buffer_index++] = tolower(c);
Expand Down Expand Up @@ -119,6 +124,7 @@ token_t Parser::parseToken(std::istream & fs, char * string)
c = EOF;
else
c = fs.get();
std::cout << "parsing comment, c=" << c << std::endl;
if (c == EOF)
{
line_mode = UNSET_LINE_MODE;
Expand Down Expand Up @@ -301,8 +307,9 @@ int Parser::parse_top_comment(std::istream & fs)
/* Process tokens until left bracket is found */
while ((token = parseToken(fs, string)) != tLBr)
{
if (token == tEOF)
return PROJECTM_PARSE_ERROR;
if (token == tEOF || token == tStringBufferFilled)
// filled up the buffer and didn't find a '['
return PROJECTM_PARSE_ERROR;
}

/* Done, return success */
Expand Down

0 comments on commit 8913b06

Please sign in to comment.