Add a bunch of overflow checks near malloc()s
[nci.git] / nci-create-assignment-group.c
blob09ddb82e0cf9f68b871f052e04795e35c3c1bdd0
1 /*
2 * Copyright (c) 2016, S. Gilles <sgilles@math.umd.edu>
4 * Permission to use, copy, modify, and/or distribute this software
5 * for any purpose with or without fee is hereby granted, provided
6 * that the above copyright notice and this permission notice appear
7 * in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
13 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
14 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
15 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <errno.h>
19 #include <locale.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <unistd.h>
25 #include <curl/curl.h>
26 #include <yajl_parse.h>
28 #include "macros.h"
29 #include "util.h"
31 int main(int argc, char **argv)
33 int ret = EINVAL;
34 char *url_base = 0;
35 char *auth_token = 0;
36 char *course_id = 0;
37 char *name = 0;
38 char *weight_arg = 0;
39 double weight_d = 0;
40 char *weight = 0;
41 char *drop_lowest_arg = 0;
42 long long drop_lowest_ll = 0;
43 char *drop_lowest = 0;
44 char *drop_highest_arg = 0;
45 long long drop_highest_ll = 0;
46 char *drop_highest = 0;
47 size_t len = 0;
48 char *built_uri = 0;
49 struct curl_httppost *post = 0;
50 char *new_id = 0;
51 int opt = 0;
53 setlocale(LC_ALL, "");
55 while ((opt = getopt(argc, argv, "c:n:w:d:D:")) != -1) {
56 switch (opt) {
57 case 'c':
58 course_id = optarg;
59 break;
60 case 'n':
61 name = optarg;
62 break;
63 case 'w':
64 weight_arg = optarg;
65 break;
66 case 'd':
67 drop_lowest_arg = optarg;
68 break;
69 case 'D':
70 drop_highest_arg = optarg;
71 break;
72 default:
73 break;
77 if (!course_id) {
78 ret = EINVAL;
79 fprintf(stderr, "course-id is mandatory\n");
80 goto cleanup;
83 if (!name) {
84 ret = EINVAL;
85 fprintf(stderr, "name is mandatory\n");
86 goto cleanup;
89 if (weight_arg) {
90 weight_d = strtod(weight_arg, 0);
91 len = snprintf(0, 0, "%lf", weight_d);
93 if (len + 1 < len) {
94 ret = errno = EOVERFLOW;
95 perror(L(""));
96 goto cleanup;
99 if (!(weight = malloc(len + 1))) {
100 ret = errno;
101 perror(L("malloc"));
102 goto cleanup;
105 sprintf(weight, "%lf", weight_d);
108 if (drop_lowest_arg) {
109 drop_lowest_ll = strtoll(drop_lowest_arg, 0, 0);
110 len = snprintf(0, 0, "%lld", drop_lowest_ll);
112 if (len + 1 < len) {
113 ret = errno = EOVERFLOW;
114 perror(L(""));
115 goto cleanup;
118 if (!(drop_lowest = malloc(len + 1))) {
119 ret = errno;
120 perror(L("malloc"));
121 goto cleanup;
124 sprintf(drop_lowest, "%lld", drop_lowest_ll);
127 if (drop_highest_arg) {
128 drop_highest_ll = strtoll(drop_highest_arg, 0, 0);
129 len = snprintf(0, 0, "%lld", drop_highest_ll);
131 if (len + 1 < len) {
132 ret = errno = EOVERFLOW;
133 perror(L(""));
134 goto cleanup;
137 if (!(drop_highest = malloc(len + 1))) {
138 ret = errno;
139 perror(L("malloc"));
140 goto cleanup;
143 sprintf(drop_highest, "%lld", drop_highest_ll);
146 curl_global_init(CURL_GLOBAL_DEFAULT);
148 if (!(url_base = get_url_base())) {
149 ret = ENOENT;
151 /* Error should have already been printed */
152 goto cleanup;
155 if (!(auth_token = get_auth_token())) {
156 ret = ENOENT;
158 /* Error should have already been printed */
159 goto cleanup;
162 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignment_groups", url_base,
163 course_id);
165 if (len + 1 < len) {
166 ret = errno = EOVERFLOW;
167 perror(L(""));
168 goto cleanup;
171 if (!(built_uri = malloc(len + 1))) {
172 ret = errno;
173 perror(L("malloc"));
174 goto cleanup;
177 sprintf(built_uri, "%s/api/v1/courses/%s/assignment_groups", url_base,
178 course_id);
180 if (!(post = make_agroup_post(name, weight, drop_lowest,
181 drop_highest))) {
182 /* XXX: pass back proper error code from make_agroup_post() */
183 goto cleanup;
186 if ((ret = send_and_id_scan(built_uri, auth_token, post, "POST",
187 &new_id))) {
188 goto cleanup;
191 if (new_id) {
192 printf("%s\n", new_id);
195 ret = 0;
196 cleanup:
197 curl_formfree(post);
198 free(new_id);
199 free(built_uri);
200 free(drop_highest);
201 free(drop_lowest);
202 free(weight);
203 free(url_base);
204 free(auth_token);
205 curl_global_cleanup();
207 return ret;