tabfile: remember to check UTF-8, glibize

This commit is contained in:
Přemysl Eric Janouch 2021-10-06 22:10:26 +02:00
parent 6c364dc997
commit 3881725904
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 11 additions and 9 deletions

View File

@ -31,7 +31,7 @@
#include "utils.h" #include "utils.h"
static gboolean static gboolean
set_data_error (GError **error, const char *message) set_data_error (GError **error, const gchar *message)
{ {
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, message); g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA, message);
return FALSE; return FALSE;
@ -40,11 +40,11 @@ set_data_error (GError **error, const char *message)
static const gchar escapes[256] = { ['n'] = '\n', ['t'] = '\t', ['\\'] = '\\' }; static const gchar escapes[256] = { ['n'] = '\n', ['t'] = '\t', ['\\'] = '\\' };
static gboolean static gboolean
inplace_unescape (char *line, GError **error) inplace_unescape (gchar *line, GError **error)
{ {
gboolean escape = FALSE; gboolean escape = FALSE;
char *dest = line; gchar *dest = line;
for (char *src = line; *src; src++) for (gchar *src = line; *src; src++)
{ {
if (escape) if (escape)
{ {
@ -65,12 +65,14 @@ inplace_unescape (char *line, GError **error)
} }
static gboolean static gboolean
import_line (Generator *generator, char *line, size_t len, GError **error) import_line (Generator *generator, gchar *line, gsize len, GError **error)
{ {
if (!len) if (!len)
return TRUE; return TRUE;
if (!g_utf8_validate_len (line, len, NULL))
return set_data_error (error, "not valid UTF-8");
char *separator = strchr (line, '\t'); gchar *separator = strchr (line, '\t');
if (!separator) if (!separator)
return set_data_error (error, "keyword separator not found"); return set_data_error (error, "keyword separator not found");
@ -79,7 +81,7 @@ import_line (Generator *generator, char *line, size_t len, GError **error)
// The index wouldn't be sorted correctly with our method // The index wouldn't be sorted correctly with our method
return set_data_error (error, "escapes not allowed in keywords"); return set_data_error (error, "escapes not allowed in keywords");
char *newline = strpbrk (separator, "\r\n"); gchar *newline = strpbrk (separator, "\r\n");
if (newline) if (newline)
*newline = 0; *newline = 0;
@ -95,8 +97,8 @@ import_line (Generator *generator, char *line, size_t len, GError **error)
static gboolean static gboolean
transform (FILE *fsorted, Generator *generator, GError **error) transform (FILE *fsorted, Generator *generator, GError **error)
{ {
char *line = NULL; gchar *line = NULL;
size_t size = 0, ln = 1; gsize size = 0, ln = 1;
for (ssize_t read; (read = getline (&line, &size, fsorted)) >= 0; ln++) for (ssize_t read; (read = getline (&line, &size, fsorted)) >= 0; ln++)
if (!import_line (generator, line, read, error)) if (!import_line (generator, line, read, error))
break; break;