49
49
#include <sysexits.h> /* EX_USAGE, EX_OSERR, EX_OSFILE */
50
50
#include <errno.h> /* errno, error_t, EACCES,
51
51
ENAMETOOLONG, ENOENT, ENOTDIR,
52
EEXIST, ECHILD, EPERM, ENOMEM,
52
ENOMEM, EEXIST, ECHILD, EPERM,
53
53
EAGAIN, EINTR, ENOBUFS, EADDRINUSE,
54
54
ECONNREFUSED, ECONNRESET,
55
55
ETOOMANYREFS, EMSGSIZE, EBADF,
73
73
ARGP_ERR_UNKNOWN, ARGP_KEY_ARGS,
74
74
struct argp, argp_parse(),
76
#include <stdint.h> /* SIZE_MAX */
76
77
#include <unistd.h> /* uid_t, gid_t, close(), pipe2(),
77
78
fork(), _exit(), dup2(),
78
79
STDOUT_FILENO, setresgid(),
95
96
IN_EXCL_UNLINK, IN_ONLYDIR,
96
97
struct inotify_event */
97
98
#include <fnmatch.h> /* fnmatch(), FNM_FILE_NAME */
98
#include <stdio.h> /* asprintf(), FILE, fopen(),
99
getline(), sscanf(), feof(),
100
ferror(), fclose(), stderr,
101
rename(), fdopen(), fprintf(),
99
#include <stdio.h> /* asprintf(), FILE, stderr, fopen(),
100
fclose(), getline(), sscanf(),
101
feof(), ferror(), rename(),
102
fdopen(), fprintf(), fscanf() */
103
103
#include <glib.h> /* GKeyFile, g_key_file_free(), g_key_file_new(),
104
104
GError, g_key_file_load_from_file(),
105
105
G_KEY_FILE_NONE, TRUE, G_FILE_ERROR_NOENT,
652
652
__attribute__((nonnull, warn_unused_result))
653
653
bool add_to_queue(task_queue *const queue, const task_context task){
654
if((queue->length + 1) > (SIZE_MAX / sizeof(task_context))){
656
error(0, ENOMEM, "Failed to allocate %" PRIuMAX
657
" tasks for queue->tasks", (uintmax_t)(queue->length + 1));
654
661
const size_t needed_size = sizeof(task_context)*(queue->length + 1);
655
662
if(needed_size > (queue->allocated)){
656
663
task_context *const new_tasks = realloc(queue->tasks,
1884
1891
g_assert_true(queue->tasks[0].func == dummy_func);
1894
static void test_add_to_queue_overflow(__attribute__((unused))
1895
test_fixture *fixture,
1896
__attribute__((unused))
1897
gconstpointer user_data){
1898
__attribute__((cleanup(cleanup_queue)))
1899
task_queue *queue = create_queue();
1900
g_assert_nonnull(queue);
1901
g_assert_true(queue->length == 0);
1902
queue->length = SIZE_MAX / sizeof(task_context); /* fake max size */
1904
FILE *real_stderr = stderr;
1905
FILE *devnull = fopen("/dev/null", "we");
1906
g_assert_nonnull(devnull);
1908
const bool ret = add_to_queue(queue,
1909
(task_context){ .func=dummy_func });
1910
g_assert_true(errno == ENOMEM);
1911
g_assert_false(ret);
1912
stderr = real_stderr;
1913
g_assert_cmpint(fclose(devnull), ==, 0);
1914
queue->length = 0; /* Restore real size */
1887
1917
static void dummy_func(__attribute__((unused))
1888
1918
const task_context task,
1889
1919
__attribute__((unused))
7862
7892
test_add("/parse_arguments/mixed", test_parse_arguments_mixed);
7863
7893
test_add("/queue/create", test_create_queue);
7864
7894
test_add("/queue/add", test_add_to_queue);
7895
test_add("/queue/add/overflow", test_add_to_queue_overflow);
7865
7896
test_add("/queue/has_question/empty",
7866
7897
test_queue_has_question_empty);
7867
7898
test_add("/queue/has_question/false",
8092
8123
g_option_context_set_help_enabled(context, FALSE);
8093
8124
g_option_context_set_ignore_unknown_options(context, TRUE);
8095
gboolean run_tests = FALSE;
8126
gboolean should_run_tests = FALSE;
8096
8127
GOptionEntry entries[] = {
8097
8128
{ "test", 0, 0, G_OPTION_ARG_NONE,
8098
&run_tests, "Run tests", NULL },
8129
&should_run_tests, "Run tests", NULL },
8101
8132
g_option_context_add_main_entries(context, entries, NULL);