~pixelherodev/cyclopropatriene-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
2

[PATCH v3] make import relative to the file importing instead of cwd()

jacob gw
Details
Message ID
<20210207202321.16425-1-jacoblevgw@gmail.com>
DKIM signature
pass
Download raw message
Patch: +38 -16
---
 lexer.c    | 27 ++++++++++++++-------------
 test/1.zig |  2 +-
 test/3.zig |  3 +--
 util.c     | 20 ++++++++++++++++++++
 util.h     |  2 ++
 5 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/lexer.c b/lexer.c
index d253e44..df575b7 100644
--- a/lexer.c
+++ b/lexer.c
@@ -56,10 +56,11 @@ static void lex_string(void);
static void
lex_import(void)
{
	int i, done;
	unsigned char buf[2];
	char *child;
	char* new_path;
	size_t new_path_len;
	skip_whitespace();

	if(ctx.buf[ctx.index] != '(')
		FATAL("Invalid @import",0);
	ctx.token.tag = ZYG_TK_LPAREN;
@@ -72,22 +73,22 @@ lex_import(void)
	if(ctx.token.len >= PATH_MAX)
		FATAL("import string limit is %d bytes, found %d", PATH_MAX, ctx.token.len);
	buf[0] = 0;

	new_path = file_rel_dir(ctx.path, ctx.token.ptr, ctx.token.len);
	new_path_len = strlen(new_path);

	if(ctx.subprocess){
		if(write(ctx.out, buf, 1) != 1)
			FATAL("Failed to write import message type", 0);
		buf[0] = ctx.token.len & 0xFF;
		buf[1] = (ctx.token.len >> 8) & 0xFF;
		buf[0] = new_path_len & 0xFF;
		buf[1] = (new_path_len >> 8) & 0xFF;
		if(write(ctx.out, buf, 2) != 2)
			FATAL("Failed to write import path size (%d)", ctx.token.len);
		if(write(ctx.out, ctx.token.ptr, ctx.token.len) != ctx.token.len)
			FATAL("Failed to write import path size (%d)", new_path_len);
		if(write(ctx.out, new_path, new_path_len) != new_path_len)
			FATAL("Failed to write import path",0);
	} else {
		child = malloc(ctx.token.len + 1);
		if(child == NULL)
			FATAL("Out of memory", 0);
		strncpy(child, ctx.token.ptr, ctx.token.len);
		child[ctx.token.len] = 0;
		list_add(ctx.children, child);
		free(new_path);
	} else{
		list_add(ctx.children, new_path);
	}
}

diff --git a/test/1.zig b/test/1.zig
index f329062..21d0a58 100644
--- a/test/1.zig
+++ b/test/1.zig
@@ -1,4 +1,4 @@
const a = @import("test/2.zig");
const a = @import("2.zig");

pub 		export const start2 = fn () noreturn{

diff --git a/test/3.zig b/test/3.zig
index cc8206e..7576d07 100644
--- a/test/3.zig
+++ b/test/3.zig
@@ -1,2 +1 @@
const a = @import("test/2.zig");

const a = @import("./2.zig");
diff --git a/util.c b/util.c
index d127e5b..34aad7c 100644
--- a/util.c
+++ b/util.c
@@ -177,3 +177,23 @@ file_len(FILE *file, const char *const path)
		FATAL("Seek failed on '%s'", path);
	return (size_t)len;
}

// returns an allocated string of dirname(orig) ++ new
char*
file_rel_dir(const char *const orig, const char *const new, size_t new_len) 
{
	int i;
	int last_slash_pos;
	char *out;
	for(i = 0; orig[i] != 0; i += 1)
		if(orig[i] == '/')
			last_slash_pos = i + 1;
	
	out = malloc(last_slash_pos + strlen(new));
	if(out == NULL)
		FATAL("Out of memory", 0);
	strncpy(out, orig, last_slash_pos);
	strncpy(out + last_slash_pos, new, new_len);
	out[new_len + last_slash_pos + 1] = 0;
	return out;
}
diff --git a/util.h b/util.h
index b892929..bb320fb 100644
--- a/util.h
+++ b/util.h
@@ -3,6 +3,8 @@ void error(const char *fmt, ...);
char *h_path_resolve(char *unknown);
char* file_read(const char *const path, size_t *size);
size_t file_len(FILE *file, const char *const path);
char *file_rel_dir(const char *const orig, const char *const new, size_t new_len);


#define _STR(x) #x
#define STR(x) _STR(x)
-- 
2.29.2
Details
Message ID
<856e932d-70dc-4454-a021-cfdd14155eed@pixelhero.dev>
In-Reply-To
<20210207202321.16425-1-jacoblevgw@gmail.com> (view parent)
DKIM signature
pass
Download raw message
Note to self for tomorrow when i review this properly: children list in 
single processed queue is flat heaped list and should be freed as such.
Details
Message ID
<3fd08e0f-17b6-47e8-9373-92daa287faaa@pixelhero.dev>
In-Reply-To
<20210207202321.16425-1-jacoblevgw@gmail.com> (view parent)
DKIM signature
pass
Download raw message
+ new_path = file_rel_dir(ctx.path, ctx.token.ptr, ctx.token.len);
+ new_path_len = strlen(new_path);

Maybe make file_rel_dir take a pointer to len and store it if nonnull, to 
avoid recalculating an already known length.

+file_rel_dir(const char *const orig, const char *const new, size_t 
new_len)
+ out = malloc(last_slash_pos + strlen(new));

strlen(new) is illegal here, since new is not required to be null 
terminated. Furthermore, we have the length as new_len.

Also, this does not allocate space for a null terminator, but you add one 
later - this is out of bounds.

+ out[new_len + last_slash_pos + 1] = 0;

See? :)

Also, there should not be a +1 here. If the first part is one character 
and the second part is one character, this sets index three, not two, for 
instance.

Aside from that I think it is good.
Reply to thread Export thread (mbox)