Add a bunch of overflow checks near malloc()s
[nci.git] / nci-edit-assignment-group.c
blob7e170395e5d5efcfca18d1e10b5f2b775c76708b
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 *group_id = 0;
38 char *name = 0;
39 char *weight_arg = 0;
40 double weight_d = 0;
41 char *weight = 0;
42 char *drop_lowest_arg = 0;
43 long long drop_lowest_ll = 0;
44 char *drop_lowest = 0;
45 char *drop_highest_arg = 0;
46 long long drop_highest_ll = 0;
47 char *drop_highest = 0;
48 size_t len = 0;
49 char *built_uri = 0;
50 struct curl_httppost *post = 0;
51 int opt = 0;
53 setlocale(LC_ALL, "");
55 while ((opt = getopt(argc, argv, "g:c:n:w:d:D:")) != -1) {
56 switch (opt) {
57 case 'c':
58 course_id = optarg;
59 break;
60 case 'g':
61 group_id = optarg;
62 break;
63 case 'n':
64 name = optarg;
65 break;
66 case 'w':
67 weight_arg = optarg;
68 break;
69 case 'd':
70 drop_lowest_arg = optarg;
71 break;
72 case 'D':
73 drop_highest_arg = optarg;
74 break;
75 default:
76 break;
80 if (!course_id) {
81 ret = EINVAL;
82 fprintf(stderr, "course-id is mandatory\n");
83 goto cleanup;
86 if (!group_id) {
87 ret = EINVAL;
88 fprintf(stderr, "group-id is mandatory\n");
89 goto cleanup;
92 if (weight_arg) {
93 weight_d = strtod(weight_arg, 0);
94 len = snprintf(0, 0, "%lf", weight_d);
96 if (len + 1 < len) {
97 ret = errno = EOVERFLOW;
98 perror(L(""));
99 goto cleanup;
102 if (!(weight = malloc(len + 1))) {
103 ret = errno;
104 perror(L("malloc"));
105 goto cleanup;
108 sprintf(weight, "%lf", weight_d);
111 if (drop_lowest_arg) {
112 drop_lowest_ll = strtoll(drop_lowest_arg, 0, 0);
113 len = snprintf(0, 0, "%lld", drop_lowest_ll);
115 if (len + 1 < len) {
116 ret = errno = EOVERFLOW;
117 perror(L(""));
118 goto cleanup;
121 if (!(drop_lowest = malloc(len + 1))) {
122 ret = errno;
123 perror(L("malloc"));
124 goto cleanup;
127 sprintf(drop_lowest, "%lld", drop_lowest_ll);
130 if (drop_highest_arg) {
131 drop_highest_ll = strtoll(drop_highest_arg, 0, 0);
132 len = snprintf(0, 0, "%lld", drop_highest_ll);
134 if (len + 1 < len) {
135 ret = errno = EOVERFLOW;
136 perror(L(""));
137 goto cleanup;
140 if (!(drop_highest = malloc(len + 1))) {
141 ret = errno;
142 perror(L("malloc"));
143 goto cleanup;
146 sprintf(drop_highest, "%lld", drop_highest_ll);
149 curl_global_init(CURL_GLOBAL_DEFAULT);
151 if (!(url_base = get_url_base())) {
152 ret = ENOENT;
154 /* Error should have already been printed */
155 goto cleanup;
158 if (!(auth_token = get_auth_token())) {
159 ret = ENOENT;
161 /* Error should have already been printed */
162 goto cleanup;
165 len = snprintf(0, 0, "%s/api/v1/courses/%s/assignment_groups/%s",
166 url_base, course_id, group_id);
168 if (len + 1 < len) {
169 ret = errno = EOVERFLOW;
170 perror(L(""));
171 goto cleanup;
174 if (!(built_uri = malloc(len + 1))) {
175 ret = errno;
176 perror(L("malloc"));
177 goto cleanup;
180 sprintf(built_uri, "%s/api/v1/courses/%s/assignment_groups/%s",
181 url_base, course_id, group_id);
183 if (!(post = make_agroup_post(name, weight, drop_lowest,
184 drop_highest))) {
185 /* XXX: pass back proper error code from make_agroup_post() */
186 goto cleanup;
189 if ((ret = send_and_id_scan(built_uri, auth_token, post, "PUT", 0))) {
190 goto cleanup;
193 ret = 0;
194 cleanup:
195 curl_formfree(post);
196 free(built_uri);
197 free(drop_highest);
198 free(drop_lowest);
199 free(weight);
200 free(url_base);
201 free(auth_token);
202 curl_global_cleanup();
204 return ret;