more AI-enabled cheap wins

This commit is contained in:
NunoSempere 2025-11-24 12:53:19 -03:00
parent 56617754f5
commit f368cbdbcd
6 changed files with 50 additions and 41 deletions

View File

@ -1,6 +1,6 @@
# C compiler # C compiler
CC=gcc # alternatives: tcc, clang, zig cc CC=gcc # alternatives: tcc, clang, zig cc
WARNINGS=-Wall WARNINGS=-Wall -Wextra -Wpedantic -Wshadow -Wformat=2 -Wno-unused-parameter
OPTIMIZED_SOME=-O3 OPTIMIZED_SOME=-O3
OPTIMIZED_MORE=-Ofast -march=native -funit-at-a-time -flto # binary will not be compatible with other computers, but may be much faster OPTIMIZED_MORE=-Ofast -march=native -funit-at-a-time -flto # binary will not be compatible with other computers, but may be much faster
DEBUG=-g DEBUG=-g

View File

@ -66,8 +66,9 @@ int libre_redirect(const char* uri, char* output)
fprintf(stderr, "Unreachable state\n"); fprintf(stderr, "Unreachable state\n");
} }
} }
// Use snprintf instead of strcpy for safety // Use strncpy with explicit null termination for safety
snprintf(output, len_output, "%s", uri); strncpy(output, uri, len_output - 1);
output[len_output - 1] = '\0';
} }
int utm_check = str_destructively_omit_from(output, "?utm"); int utm_check = str_destructively_omit_from(output, "?utm");

View File

@ -1,3 +1,4 @@
#include <glib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -5,27 +6,29 @@
void read_readability_js(char* string) void read_readability_js(char* string)
{ {
FILE* fp = fopen("/opt/rosenrot/readability.js", "r"); gchar* file_contents = NULL;
if (!fp) { // fp is NULL, fopen failed gsize length = 0;
fprintf(stderr, "Failed to open file\n"); GError* error = NULL;
if (!g_file_get_contents("/opt/rosenrot/readability.js", &file_contents, &length, &error)) {
fprintf(stderr, "Failed to open file: %s\n", error ? error->message : "unknown error");
fprintf(stderr, "Consider running $ sudo make runtime_files\n"); fprintf(stderr, "Consider running $ sudo make runtime_files\n");
string = NULL; if (error) g_error_free(error);
string[0] = '\0';
return; return;
} }
int i = 0;
int c; if (length > READABILITY_N) {
while ((c = fgetc(fp)) != EOF) { fprintf(stderr, "readability.js file is too large (%zu bytes, max %d)\n", length, READABILITY_N);
if (i >= READABILITY_N) { fprintf(stderr, "Consider increasing READABILITY_N or running recompute_READABILITY_N.sh\n");
fprintf(stderr, "readability.js file is too large (exceeds %d bytes)\n", READABILITY_N); g_free(file_contents);
fprintf(stderr, "Consider increasing READABILITY_N or running recompute_READABILITY_N.sh\n"); string[0] = '\0';
fclose(fp); return;
string[0] = '\0';
return;
}
string[i++] = c;
} }
string[i] = '\0';
fclose(fp); memcpy(string, file_contents, length);
string[length] = '\0';
g_free(file_contents);
} }
/* /*

View File

@ -62,8 +62,9 @@ int shortcut_expand(const char* uri, char* output)
fprintf(stderr, "Unreachable state\n"); fprintf(stderr, "Unreachable state\n");
} }
} }
// Use snprintf instead of strcpy for safety // Use strncpy with explicit null termination for safety
snprintf(output, len_output, "%s", uri); strncpy(output, uri, len_output - 1);
output[len_output - 1] = '\0';
} }
if (DEBUG) printf("No match found\n\n"); if (DEBUG) printf("No match found\n\n");
return 0; return 0;

View File

@ -49,8 +49,9 @@ int str_replace_start(const char* string, const char* target, const char* replac
return 2; // success return 2; // success
} else { } else {
if (DEBUG) printf("Did not find match.\n"); if (DEBUG) printf("Did not find match.\n");
// Use snprintf instead of strcpy for safety // Use strncpy with explicit null termination for safety
snprintf(output, l4, "%s", string); strncpy(output, string, l4 - 1);
output[l4 - 1] = '\0';
} }
return 0; return 0;

View File

@ -1,3 +1,4 @@
#include <glib.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -5,24 +6,26 @@
void read_style_js(char* string) void read_style_js(char* string)
{ {
FILE* fp = fopen("/opt/rosenrot/style.js", "r"); gchar* file_contents = NULL;
if (!fp) { // fp is NULL, fopen failed gsize length = 0;
fprintf(stderr, "Failed to open file\n"); GError* error = NULL;
string = NULL;
if (!g_file_get_contents("/opt/rosenrot/style.js", &file_contents, &length, &error)) {
fprintf(stderr, "Failed to open file: %s\n", error ? error->message : "unknown error");
if (error) g_error_free(error);
string[0] = '\0';
return; return;
} }
int i = 0;
int c; if (length > STYLE_N) {
while ((c = fgetc(fp)) != EOF) { fprintf(stderr, "style.js file is too large (%zu bytes, max %d)\n", length, STYLE_N);
if (i >= STYLE_N) { fprintf(stderr, "Consider increasing STYLE_N or running recompute_STYLE_N.sh\n");
fprintf(stderr, "style.js file is too large (exceeds %d bytes)\n", STYLE_N); g_free(file_contents);
fprintf(stderr, "Consider increasing STYLE_N or running recompute_STYLE_N.sh\n"); string[0] = '\0';
fclose(fp); return;
string[0] = '\0';
return;
}
string[i++] = c;
} }
string[i] = '\0';
fclose(fp); memcpy(string, file_contents, length);
string[length] = '\0';
g_free(file_contents);
} }