Skip to content

Is handle_char thread-safe? #54

@phkeese

Description

@phkeese

Hello,

I noticed that handle_char uses a static buffer to buffer words, which sounds like it could cause issues when running two instances on two different threads:

static void handle_char(zf_ctx *ctx, char c)
{
	// Static buffer, shared between threads.
	static char buf[32];
	static size_t len = 0;

	if(ctx->input_state == ZF_INPUT_PASS_CHAR) {

		ctx->input_state = ZF_INPUT_INTERPRET;
		run(ctx, &c);

	} else if(c != '\0' && !isspace(c)) {

		if(len < sizeof(buf)-1) {
 			// Buffer access, not synchronized.
			buf[len++] = c;
			buf[len] = '\0';
		}

	} else {

		if(len > 0) {
			len = 0;
			handle_word(ctx, buf);
		}
	}
}

I can't test if this actually breaks in the real world right now, but this sounds like a potential race condition under true parallelism?

Best,
Philipp

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions