Skip to content

Commit 594b7f1

Browse files
committed
job-info: validate keys array on lookup
Problem: Throughout lookup code there are checks if the values within the keys array are strings. It would be simpler if this check was only done once. Check that the keys array is an array and contains only strings within the primary lookup callback.
1 parent cbe957d commit 594b7f1

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

src/modules/job-info/lookup.c

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,7 @@ static int lookup_keys (struct lookup_ctx *l)
131131
}
132132

133133
json_array_foreach (l->keys, index, key) {
134-
const char *keystr;
135-
if (!(keystr = json_string_value (key))) {
136-
errno = EINVAL;
137-
goto error;
138-
}
139-
if (lookup_key (l, fall, keystr) < 0)
134+
if (lookup_key (l, fall, json_string_value (key)) < 0)
140135
goto error;
141136
}
142137

@@ -197,14 +192,9 @@ static void info_lookup_continuation (flux_future_t *fall, void *arg)
197192

198193
json_array_foreach (l->keys, index, key) {
199194
flux_future_t *f;
200-
const char *keystr;
195+
const char *keystr = json_string_value (key); /* validated earlier */
201196
json_t *str = NULL;
202197

203-
if (!(keystr = json_string_value (key))) {
204-
errno = EINVAL;
205-
goto error;
206-
}
207-
208198
if (!(f = flux_future_get_child (fall, keystr))) {
209199
flux_log_error (ctx->h, "%s: flux_future_get_child", __FUNCTION__);
210200
goto error;
@@ -279,12 +269,7 @@ static int check_keys_for_eventlog (struct lookup_ctx *l)
279269
}
280270

281271
json_array_foreach (l->keys, index, key) {
282-
const char *keystr;
283-
if (!(keystr = json_string_value (key))) {
284-
errno = EINVAL;
285-
return -1;
286-
}
287-
if (streq (keystr, "eventlog"))
272+
if (streq (json_string_value (key), "eventlog"))
288273
return 0;
289274
}
290275

@@ -297,6 +282,8 @@ void lookup_cb (flux_t *h, flux_msg_handler_t *mh,
297282
{
298283
struct info_ctx *ctx = arg;
299284
struct lookup_ctx *l = NULL;
285+
size_t index;
286+
json_t *key;
300287
json_t *keys;
301288
flux_jobid_t id;
302289
uint32_t rolemask;
@@ -318,6 +305,19 @@ void lookup_cb (flux_t *h, flux_msg_handler_t *mh,
318305
goto error;
319306
}
320307

308+
/* validate keys is an array and all fields are strings */
309+
if (!json_is_array (keys)) {
310+
errno = EPROTO;
311+
goto error;
312+
}
313+
314+
json_array_foreach (keys, index, key) {
315+
if (!json_is_string (key)) {
316+
errno = EPROTO;
317+
goto error;
318+
}
319+
}
320+
321321
if (!(l = lookup_ctx_create (ctx, msg, id, keys, flags)))
322322
goto error;
323323

0 commit comments

Comments
 (0)