Decode + validate. Returns NULL on success, else a static teaching error. */
| 9375 | |
| 9376 | /* Decode + validate. Returns NULL on success, else a static teaching error. */ |
| 9377 | static const char *trace_cursor_decode(const char *token, const char *current_generation, |
| 9378 | uint64_t expected_qhash, trace_cursor_t *out) { |
| 9379 | memset(out, 0, sizeof(*out)); |
| 9380 | if (!token || strncmp(token, "c1.", 3) != 0) { |
| 9381 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9382 | } |
| 9383 | const char *p = token + 3; |
| 9384 | if ((*p != 'o' && *p != 'i') || p[1] != '.') { |
| 9385 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9386 | } |
| 9387 | out->leg = *p; |
| 9388 | p += 2; /* leg + '.' */ |
| 9389 | const char *gen_end = strchr(p, '.'); |
| 9390 | if (!gen_end || gen_end == p || (size_t)(gen_end - p) >= sizeof(out->generation)) { |
| 9391 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9392 | } |
| 9393 | memcpy(out->generation, p, (size_t)(gen_end - p)); |
| 9394 | out->generation[gen_end - p] = '\0'; |
| 9395 | const char *hash_start = gen_end + 1; |
| 9396 | const char *hash_end = strchr(hash_start, '.'); |
| 9397 | if (!hash_end || hash_end - hash_start != 16) { |
| 9398 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9399 | } |
| 9400 | for (const char *digit = hash_start; digit < hash_end; digit++) { |
| 9401 | if (!isxdigit((unsigned char)*digit)) { |
| 9402 | return "invalid_cursor: unrecognized token — re-run the original query without " |
| 9403 | "'cursor'"; |
| 9404 | } |
| 9405 | } |
| 9406 | errno = 0; |
| 9407 | char *parsed_end = NULL; |
| 9408 | unsigned long long qh = strtoull(hash_start, &parsed_end, 16); |
| 9409 | if (errno == ERANGE || parsed_end != hash_end) { |
| 9410 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9411 | } |
| 9412 | const char *hop_start = hash_end + 1; |
| 9413 | const char *hop_end = strchr(hop_start, '.'); |
| 9414 | errno = 0; |
| 9415 | long parsed_hop = hop_end ? strtol(hop_start, &parsed_end, 10) : -1; |
| 9416 | if (!hop_end || hop_end == hop_start || errno == ERANGE || parsed_end != hop_end || |
| 9417 | parsed_hop < 1 || parsed_hop > INT_MAX) { |
| 9418 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9419 | } |
| 9420 | const char *node_start = hop_end + 1; |
| 9421 | errno = 0; |
| 9422 | long long nid = strtoll(node_start, &parsed_end, 10); |
| 9423 | if (node_start == parsed_end || errno == ERANGE || *parsed_end != '\0' || nid <= 0) { |
| 9424 | return "invalid_cursor: unrecognized token — re-run the original query without 'cursor'"; |
| 9425 | } |
| 9426 | out->hop = (int)parsed_hop; |
| 9427 | out->qhash = qh; |
| 9428 | out->node_id = nid; |
| 9429 | if (out->qhash != expected_qhash) { |
| 9430 | return "cursor_params_mismatch: this cursor was issued for different arguments — " |
| 9431 | "pass the cursor back with ALL other arguments identical"; |
| 9432 | } |
| 9433 | if (strcmp(out->generation, current_generation) != 0) { |
| 9434 | return "stale_cursor: the project was reindexed since this cursor was issued — " |
no outgoing calls
no test coverage detected