~sircmpwn/harec

ba8d4ca7e44b923c955e78624dcb379d59193616 — Sebastian 1 year, 2 months ago 60a26e3
mod: use identifier_unparse_static

There were actually a few problems with the old code:

- env's size was PATH_MAX + 1, even though PATH_MAX already includes the
  NUL terminator in its definition

- PATH_MAX is non-portable. From limits.h(0p):

  > A definition of one of the symbolic constants in the following list
  > shall be omitted from the <limits.h> header on specific
  > implementations where the corresponding value is equal to or greater
  > than the stated minimum, but where the value can vary depending on
  > the file to which it is applied. The actual value supported for a
  > specific pathname shall be provided by the pathconf() function.

- Write errors weren't reported; the output was just silently truncated.
  This is because fmemopen is buffered by default, so the calls to
  xfprintf in write_ident don't report errors. Errors are only reported
  in fclose (when the stream is flushed), but no error checks were
  performed there.

  Because of the addition of IDENT_MAX, and the added check in parse, it
  shouldn't be possible for an error to occur (or, as of this commit,
  for the buffer to be too small). However, I added assertions anyway,
  since there's not really a reason not to.

- write_ident was unnecessary: identifier_unparse_static already exists.
  write_ident was also externally linked despite not appearing in any
  header files.

Signed-off-by: Sebastian <sebastian@sebsite.pw>
1 files changed, 7 insertions(+), 14 deletions(-)

M src/mod.c
M src/mod.c => src/mod.c +7 -14
@@ 14,15 14,9 @@
#include "type_store.h"
#include "util.h"

void
write_ident(FILE *out, const struct identifier *ident)
{
	if (ident->ns) {
		write_ident(out, ident->ns);
		xfprintf(out, "::");
	};
	xfprintf(out, "%s", ident->name);
}
// unfortunately necessary since this is used in an array declaration, and we
// don't want a VLA
#define strlen_HARE_TD_ (sizeof("HARE_TD_") - 1)

struct scope *
module_resolve(struct modcache *cache[],


@@ 41,11 35,10 @@ module_resolve(struct modcache *cache[],
	struct lexer lexer = {0};
	struct ast_unit aunit = {0};

	char env[PATH_MAX+1];
	FILE *envf = fmemopen(&env, sizeof(env), "w");
	xfprintf(envf, "HARE_TD_");
	write_ident(envf, ident);
	fclose(envf);
	// env = "HARE_TD_foo::bar::baz"
	char env[strlen_HARE_TD_ + IDENT_BUFSIZ];
	memcpy(env, "HARE_TD_", strlen_HARE_TD_);
	identifier_unparse_static(ident, &env[strlen_HARE_TD_]);

	char *path = getenv(env);
	if (!path) {