Skip to content

GitHub Actions Validator — C source

Check a workflow YAML before you push: on-shape, jobs, runs-on, steps with run or uses, and the needs graph including cycle detection — every issue reported at its path. Runs locally in your browser.

This is the C implementation — the same logic the interactive tool runs, in a shareable, citable form.

/* gh-actions-validator — GitHub Actions workflow structural validator. C11
 * port (canonical TS: src/lib/gh-actions-validator.ts; Go twin:
 * cli/gh-actions-validator). Trimmed port: a minimal YAML-subset parser
 * (block maps/sequences with `- key: value` items, plain/quoted scalars —
 * no flow style, anchors or block scalars; inline lists like needs: [a, b]
 * stay literal scalars) plus terse rule checks: root shape, on/jobs
 * presence, jobs non-empty, runs-on, steps run-xor-uses, needs known +
 * acyclic. Standard library only; never crashes on bad input. Single-shot
 * validator: nodes are arena-style, freed nowhere (exit reclaims). */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef enum { N_STR, N_SEQ, N_MAP } NType; /* a NULL Node* is YAML null */
typedef struct Node Node;
typedef struct { char *key; Node *val; } Entry;
struct Node { NType t; char *s; Node **it; size_t ni; Entry *en; size_t ne; };
typedef struct { char *id; int color; } Clr; /* DFS mark: 1 visiting, 2 done */
typedef struct { char *path, *msg; } Issue;
typedef struct { int valid; Issue *v; size_t n; char *error; } GhResult;

static char *ds(const char *s, size_t n) { char *p = malloc(n + 1); memcpy(p, s, n); p[n] = '\0'; return p; }
static char *dsc(const char *s) { return ds(s, strlen(s)); }
static Node *nnew(NType t) { Node *n = calloc(1, sizeof *n); n->t = t; return n; } static Node *nstr(char *s) { Node *n = nnew(N_STR); n->s = s; return n; }
static void seq_push(Node *s, Node *v) { s->it = realloc(s->it, (s->ni + 1) * sizeof *s->it); s->it[s->ni++] = v; }
static void map_put(Node *m, char *k, Node *v) { m->en = realloc(m->en, (m->ne + 1) * sizeof *m->en); m->en[m->ne++] = (Entry){k, v}; }
static Node *mget(Node *m, const char *k) { if (!m || m->t != N_MAP) return NULL; for (size_t i = 0; i < m->ne; i++) if (!strcmp(m->en[i].key, k)) return m->en[i].val; return NULL; }
static int mhas(Node *m, const char *k) { if (!m || m->t != N_MAP) return 0; for (size_t i = 0; i < m->ne; i++) if (!strcmp(m->en[i].key, k)) return 1; return 0; }
static int is_str(Node *n) { return n && n->t == N_STR; } static int is_seq(Node *n) { return n && n->t == N_SEQ; }
/* Trim a copy of s[0..n) minus its space/tab margins. */
static char *trimds(const char *s, size_t n) { while (n && (s[0] == ' ' || s[0] == '\t')) { s++; n--; } while (n && (s[n-1] == ' ' || s[n-1] == '\t')) n--; return ds(s, n); }
/* Strip matching quotes; single-quote style un-doubles ''. */
static char *unquote(const char *s) {
    size_t n = strlen(s);
    if (n < 2 || s[0] != s[n-1] || (s[0] != '\'' && s[0] != '"')) return dsc(s);
    char *out = malloc(n - 1); size_t w = 0;
    for (size_t i = 1; i + 1 < n; i++) { out[w++] = s[i]; if (s[0] == '\'' && s[i] == '\'' && i + 2 < n && s[i+1] == '\'') i++; }
    return out[w] = '\0', out;
}
/* Cut a line at an unquoted # that starts the line or follows whitespace. */
static void strip_hash(char *s) { char q = 0;
    for (size_t i = 0; s[i]; i++) { if (q) { if (s[i] == q) q = 0; } else if (s[i] == '\'' || s[i] == '"') q = s[i];
        else if (s[i] == '#' && (i == 0 || s[i-1] == ' ' || s[i-1] == '\t')) { s[i] = '\0'; return; } } }
/* Split "key: rest" at the first unquoted colon ending the line or followed by space/tab. */
static void split_key(const char *t, char **key, const char **rest) {
    size_t i, n = strlen(t); char q = 0;
    for (i = 0; i < n; i++) { if (q) { if (t[i] == q) q = 0; } else if (t[i] == '\'' || t[i] == '"') q = t[i];
        else if (t[i] == ':' && (i + 1 == n || t[i+1] == ' ' || t[i+1] == '\t')) break; }
    if (i == n) { *key = unquote(trimds(t, n)); *rest = t + n; return; }
    *key = unquote(trimds(t, i));
    size_t j = i + 1; while (t[j] == ' ' || t[j] == '\t') j++;
    *rest = t + j;
}
typedef struct { size_t ind; char *text; } L;
typedef struct { L *v; size_t n, i; } Ctx;
static int seqmark(const char *t) { return !strcmp(t, "-") || !strncmp(t, "- ", 2); }

static Ctx build_ctx(const char *raw) {
    Ctx c = {0};
    for (const char *p = raw; p && *p; ) {
        const char *e = strchr(p, '\n');
        char *line = ds(p, e ? (size_t)(e - p) : strlen(p));
        size_t len = strcspn(line, "\r");
        p = e ? e + 1 : NULL;
        if (line[len]) line[len] = '\0'; /* drop trailing CR */
        strip_hash(line);
        char *t = line + strspn(line, " \t");
        size_t tl = strlen(t); while (tl && (t[tl-1] == ' ' || t[tl-1] == '\t')) t[--tl] = '\0';
        if (!tl || !strcmp(t, "---") || !strcmp(t, "...")) continue;
        c.v = realloc(c.v, (c.n + 1) * sizeof *c.v); c.v[c.n++] = (L){(size_t)(t - line), t};
    }
    return c;
}

static Node *parse_block(Ctx *c);
static Node *parse_seq(Ctx *c, size_t ind);
/* Value for a key (or "- key") consumed at the cursor: inline scalar, child block, child seq or null. */
static Node *childval(Ctx *c, size_t ind, const char *rest) {
    if (*rest) { Node *v = nstr(unquote(rest)); c->i++; return v; }
    if (c->i + 1 < c->n && c->v[c->i+1].ind > ind) { c->i++; return parse_block(c); }
    if (c->i + 1 < c->n && c->v[c->i+1].ind == ind && seqmark(c->v[c->i+1].text)) { c->i++; return parse_seq(c, ind); }
    return c->i++, NULL;
}
static void pair(Ctx *c, size_t ind, Node *m) { /* one "key: value" line of a mapping at ind */
    char *k; const char *r; split_key(c->v[c->i].text, &k, &r);
    map_put(m, k, childval(c, ind, r));
}
static Node *parse_map(Ctx *c, size_t ind) { Node *m = nnew(N_MAP); while (c->i < c->n && c->v[c->i].ind == ind && !seqmark(c->v[c->i].text)) pair(c, ind, m); return m; }
static Node *parse_seq(Ctx *c, size_t ind) {
    Node *s = nnew(N_SEQ);
    while (c->i < c->n && c->v[c->i].ind == ind && seqmark(c->v[c->i].text)) {
        const char *t = c->v[c->i].text, *rest = t + (t[1] == ' ' || t[1] == '\t' ? 2 : 1);
        while (*rest == ' ' || *rest == '\t') rest++;
        size_t rc = ind + (size_t)(rest - t); /* column the item text starts at */
        Node *v;
        if (!*rest) v = childval(c, ind, rest);
        else {
            char *k; const char *r; split_key(rest, &k, &r);
            if (*r || rest[strlen(rest)-1] == ':') { /* inline map item: "- key: value" (+ pairs at rc) */
                v = nnew(N_MAP); map_put(v, k, childval(c, rc, r));
                while (c->i < c->n && c->v[c->i].ind == rc && !seqmark(c->v[c->i].text)) pair(c, rc, v);
            } else { v = nstr(unquote(rest)); c->i++; }
        }
        seq_push(s, v);
    }
    return s;
}
static Node *parse_block(Ctx *c) { L l = c->v[c->i]; return seqmark(l.text) ? parse_seq(c, l.ind) : parse_map(c, l.ind); }

static void iss(Issue **v, size_t *n, const char *path, const char *msg) { *v = realloc(*v, (*n + 1) * sizeof **v); (*v)[(*n)++] = (Issue){dsc(path), dsc(msg)}; }
static void issf(Issue **v, size_t *n, const char *pathfmt, const char *msg, ...) {
    char path[576]; va_list ap; va_start(ap, msg); vsnprintf(path, sizeof path, pathfmt, ap); va_end(ap);
    iss(v, n, path, msg);
}

/* needs-graph cycle detection: DFS from every job over declared, known edges. */
static Clr *cl = NULL; static size_t cln = 0; /* DFS colors; the validator is single-shot */
static void visit(Node *jobs, const char *id, char **stack, size_t sn, Issue **v, size_t *n) {
    size_t ci = cln; for (size_t k = 0; k < cln; k++) if (!strcmp(cl[k].id, id)) { ci = k; break; }
    if (ci < cln && cl[ci].color == 2) return;
    if (ci < cln) { /* color 1 = id already sits on the current path — cycle */
        size_t from = 0; while (from < sn && strcmp(stack[from], id)) from++;
        char msg[1024]; size_t len = (size_t)snprintf(msg, sizeof msg, "needs cycle: ");
        for (size_t k = from; k < sn; k++) { int w = snprintf(msg + len, sizeof msg - len, "%s -> ", stack[k]); if ((size_t)w >= sizeof msg - len) break; len += (size_t)w; }
        iss(v, n, "$.jobs", (snprintf(msg + len, sizeof msg - len, "%s", id), msg)); return;
    }
    cl = realloc(cl, (cln + 1) * sizeof *cl); cl[cln++] = (Clr){dsc(id), 1}; stack[sn] = (char *)id;
    Node *needs = mget(mget(jobs, id), "needs");
    if (is_str(needs) && mhas(jobs, needs->s)) visit(jobs, needs->s, stack, sn + 1, v, n);
    else if (is_seq(needs)) for (size_t k = 0; k < needs->ni; k++) if (is_str(needs->it[k]) && mhas(jobs, needs->it[k]->s)) visit(jobs, needs->it[k]->s, stack, sn + 1, v, n);
    cl[cln - 1].color = 2;
}

static void check(Node *root, Issue **v, size_t *n) {
    if (!root || root->t != N_MAP) { iss(v, n, "$", "workflow root must be a mapping"); return; }
    if (!mhas(root, "jobs")) iss(v, n, "$", "missing required key \"jobs\"");
    if (!mhas(root, "on")) iss(v, n, "$", "missing required key \"on\"");
    Node *jobs = mget(root, "jobs");
    if (!jobs || jobs->t != N_MAP) { if (mhas(root, "jobs")) iss(v, n, "$.jobs", "jobs must be a mapping"); return; }
    if (!jobs->ne) iss(v, n, "$.jobs", "jobs must not be empty");
    char **stack = malloc((jobs->ne + 1) * sizeof *stack);
    for (size_t j = 0; j < jobs->ne; j++) {
        char at[512], msg[1024];
        snprintf(at, sizeof at, "$.jobs.%s", jobs->en[j].key);
        Node *job = jobs->en[j].val;
        if (!job || job->t != N_MAP) { iss(v, n, at, "job must be a mapping"); continue; }
        int reusable = is_str(mget(job, "uses"));
        Node *ro = mget(job, "runs-on"), *steps = mget(job, "steps"), *needs = mget(job, "needs");
        if (!reusable && !is_str(ro) && !is_seq(ro)) issf(v, n, "%s.runs-on", "job needs runs-on (or a reusable-workflow uses)", at);
        if (!reusable && !mhas(job, "steps")) issf(v, n, "%s.steps", "job needs steps", at);
        if (is_seq(steps)) {
            if (!steps->ni) issf(v, n, "%s.steps", "steps must not be empty", at);
            for (size_t k = 0; k < steps->ni; k++) {
                Node *st = steps->it[k];
                if (!st || st->t != N_MAP) { issf(v, n, "%s.steps[%d]", "step must be a mapping", at, (int)k); continue; }
                int hr = is_str(mget(st, "run")), hu = is_str(mget(st, "uses"));
                if (!hr && !hu) issf(v, n, "%s.steps[%d]", "step needs run or uses", at, (int)k);
                if (hr && hu) issf(v, n, "%s.steps[%d]", "step has both run and uses - pick one", at, (int)k);
            }
        } else if (mhas(job, "steps")) issf(v, n, "%s.steps", "steps must be a list", at);
        if (mhas(job, "needs")) {
            if (is_str(needs)) {
                if (!mhas(jobs, needs->s)) issf(v, n, "%s.needs[0]", (snprintf(msg, sizeof msg, "unknown job \"%s\"", needs->s), msg), at);
            } else if (is_seq(needs)) for (size_t k = 0; k < needs->ni; k++) {
                if (!is_str(needs->it[k]) || mhas(jobs, needs->it[k]->s)) continue;
                issf(v, n, "%s.needs[%d]", (snprintf(msg, sizeof msg, "unknown job \"%s\"", needs->it[k]->s), msg), at, (int)k);
            } else issf(v, n, "%s.needs", "needs must be a string or a list", at);
        }
    }
    for (size_t j = 0; j < jobs->ne; j++) visit(jobs, jobs->en[j].key, stack, 0, v, n);
    free(stack);
}

GhResult validate_gh_actions(const char *raw) {
    GhResult r = {0};
    if (strlen(raw) == strspn(raw, " \t\r\n")) { r.error = dsc("A workflow document is required"); return r; }
    Ctx c = build_ctx(raw);
    Node *root = c.n ? parse_block(&c) : NULL;
    check(root, &r.v, &r.n);
    return r.valid = r.n == 0, r;
}
/* Example: validate_gh_actions("on: push\njobs:\n  a:\n    needs: b\n    runs-on: ubuntu-latest\n    steps:\n      - run: make\n  b:\n    needs: a\n    runs-on: ubuntu-latest\n    steps:\n      - run: make") -> issues[0] = { "$.jobs", "needs cycle: a -> b -> a" } */

Also available in 13 other languages

Every CosmoDev tool ships its pure logic in TypeScript (web) and Go (CLI), with authored implementations in a dozen-plus languages — the same contract, ported. Compare all languages side by side →