Skip to content

Commit

Permalink
Fixed string bug in icon search code
Browse files Browse the repository at this point in the history
  • Loading branch information
jmanc3 committed Feb 14, 2022
1 parent 9898a5e commit cf17aa7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 27 deletions.
67 changes: 40 additions & 27 deletions lib/icons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <zconf.h>
#include <sys/mman.h>
#include <pango/pangocairo.h>
#include <math.h>

#ifdef TRACY_ENABLE

Expand Down Expand Up @@ -350,7 +351,7 @@ void check_icon_cache() {
char buf[1024];
if ((fp = fopen(icon_cache_path.data(), "rb"))) {
fread(buf, 1, 10, fp);
std::string version = std::string(buf, strlen(buf));
std::string version = std::string(buf, std::max(strlen(buf), (unsigned long) 0));
if (!version.empty()) {
auto cache_version_on_disk = std::stoi(version);
if (cache_version_on_disk != cache_version) {
Expand Down Expand Up @@ -378,7 +379,7 @@ void check_icon_cache() {
c->root->when_paint = paint_warning;
first_message = "Icon cache version file out of date";
second_message = "We are re-caching all icons, so this may take a while. Subsequent launches won't do this, so they'll be faster.";
third_message = "We'll start WinBar when we are done!";
third_message = "We'll start WinBar when we are done! (Feel free to close this window)";
client_show(temp_app, c);
app_main(temp_app);
app_clean(temp_app);
Expand Down Expand Up @@ -409,7 +410,7 @@ void check_icon_cache() {
c->root->when_paint = paint_warning;
first_message = "First time launching WinBar";
second_message = "We are caching all icons so this may take a while. Subsequent launches will be faster.";
third_message = "We'll start WinBar when we are done!";
third_message = "We'll start WinBar when we are done! (Feel free to close this window)";
client_show(temp_app, c);
app_main(temp_app);
app_clean(temp_app);
Expand All @@ -425,6 +426,17 @@ void check_icon_cache() {
last_time_cached_checked = get_current_time_in_ms();
}

int has_extension(const char *szFileName, const char *szExt) {
int i = 0;
if ('\0' != *szFileName) {
for (i = strlen(szFileName) - 1; i > 0; --i) {
if ('.' == szFileName[i])
break;
}
}
return (0 == strcmp(szFileName + i, szExt));
}

void search_icons(std::vector<IconTarget> &targets) {
#ifdef TRACY_ENABLE
ZoneScoped;
Expand All @@ -449,47 +461,48 @@ void search_icons(std::vector<IconTarget> &targets) {
strcpy(buffer, icon_cache_data + index_into_file);
long len = strlen(buffer);
index_into_file += len + 1;
std::string pre_path = std::string(buffer, len);
std::string pre_path = std::string(buffer, std::max(len, (long) 0));

strcpy(buffer, icon_cache_data + index_into_file);
len = strlen(buffer);
index_into_file += len + 1;
std::string size = std::string(buffer, len);
std::string size = std::string(buffer, std::max(len, (long) 0));

strcpy(buffer, icon_cache_data + index_into_file);
len = strlen(buffer);
index_into_file += len + 1;
std::string scale = std::string(buffer, len);
std::string scale = std::string(buffer, std::max(len, (long) 0));

strcpy(buffer, icon_cache_data + index_into_file);
len = strlen(buffer);
index_into_file += len + 1;
std::string theme = std::string(buffer, len);
std::string theme = std::string(buffer, std::max(len, (long) 0));

while (NOT_DONE && icon_cache_data[index_into_file] != '\n') {
strcpy(buffer, icon_cache_data + index_into_file);
len = strlen(buffer);
std::string name_without_extension = std::string(buffer, len - 4);
int extension = 0;

if (strncmp(buffer + len - 4, ".svg", 4) == 0) {
extension = 2;
} else if (strncmp(buffer + len - 4, ".png", 4) == 0) {
extension = 1;
} else if (strncmp(buffer + len - 4, ".xpm", 4) == 0) {
extension = 3;
}
std::string name_without_extension = std::string(buffer, std::max(len - 4, (long) 0));
if (!name_without_extension.empty()) {
int extension = 0;
if (strncmp(buffer + len - 4, ".svg", 4) == 0) {
extension = 2;
} else if (strncmp(buffer + len - 4, ".png", 4) == 0) {
extension = 1;
} else if (strncmp(buffer + len - 4, ".xpm", 4) == 0) {
extension = 3;
}

for (int i = 0; i < targets.size(); i++) {
if (strcmp(name_without_extension.data(), targets[i].name.data()) == 0) {
targets[i].indexes_of_results.emplace_back(
pre_path,
std::stoi(size),
std::stoi(scale),
theme,
std::string(buffer, len),
extension
);
for (int i = 0; i < targets.size(); i++) {
if (strcmp(name_without_extension.data(), targets[i].name.data()) == 0) {
targets[i].indexes_of_results.emplace_back(
pre_path,
std::stoi(size),
std::stoi(scale),
theme,
std::string(buffer, len),
extension
);
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ bool restart = false;
void check_config_version();

int main() {
// char buf[102];
// buf[0] = '\0';
// int len = strlen(buf);
// std::string test = std::string(buf, -1);

global = new globals;

// Open connection to app
Expand Down

0 comments on commit cf17aa7

Please sign in to comment.