~sircmpwn/hare-dev

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

[PATCH harec v2] Print paths relative to cwd on errors

Details
Message ID
<20231125210932.257394-1-max@mxsr.de>
DKIM signature
missing
Download raw message
Patch: +51 -9
Signed-off-by: Max Schillinger <max@mxsr.de>
---
* Prevent out-of-bounds access.
* Rename 'dir' parameter to 'filepath'.

 include/util.h |  2 ++
 src/main.c     | 11 ++---------
 src/util.c     | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 51 insertions(+), 9 deletions(-)

diff --git a/include/util.h b/include/util.h
index 42127c4..b29cc3e 100644
--- a/include/util.h
+++ b/include/util.h
@@ -28,6 +28,8 @@ char *xstrdup(const char *s);

char *gen_name(int *id, const char *fmt);

const char *relpath(const char *dir);

void errline(struct location loc);

#endif
diff --git a/src/main.c b/src/main.c
index 313e462..62d2667 100644
--- a/src/main.c
+++ b/src/main.c
@@ -70,7 +70,6 @@ main(int argc, char *argv[])
{
	const char *output = NULL, *typedefs = NULL;
	const char *target = DEFAULT_TARGET;
	const char *modpath = NULL;
	const char *mainsym = "main";
	bool is_test = false;
	struct unit unit = {0};
@@ -91,7 +90,6 @@ main(int argc, char *argv[])
			usage(argv[0]);
			return EXIT_SUCCESS;
		case 'M':
			modpath = optarg;
			break;
		case 'm':
			mainsym = optarg;
@@ -144,13 +142,8 @@ main(int argc, char *argv[])
	memcpy((char **)sources + 1, argv + optind, sizeof(char **) * nsources);
	sources[0] = "<unknown>";

	if (modpath) {
		size_t modlen = strlen(modpath);
		for (size_t i = 1; i <= nsources; i++) {
			if (strncmp(sources[i], modpath, modlen) == 0) {
				sources[i] += modlen;
			}
		}
	for (size_t i = 1; i <= nsources; i++) {
		sources[i] = relpath(sources[i]);
	}

	for (size_t i = 0; i < nsources; ++i) {
diff --git a/src/util.c b/src/util.c
index c3691bb..8c13f4e 100644
--- a/src/util.c
+++ b/src/util.c
@@ -1,3 +1,4 @@
#include <limits.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <stdbool.h>
@@ -113,6 +114,52 @@ gen_name(int *id, const char *fmt)
	return str;
}

const char *
relpath(const char *filepath)
{
	if (filepath[0] != '/') {
		return filepath;
	}
	char cwd[PATH_MAX];
	if (getcwd(cwd, sizeof(cwd)) == NULL) {
		return filepath;
	}
	size_t cwdlen = strlen(cwd);
	if (strncmp(filepath, cwd, cwdlen) == 0) {
		return (strlen(filepath) > cwdlen) ? filepath + cwdlen + 1 : filepath;
	}
	int common_prefix = -1;
	for (int i = 0; filepath[i] != '\0' && filepath[i] == cwd[i]; i++) {
		if (filepath[i] == '/') {
			common_prefix = i;
		}
	}
	if (common_prefix == -1) {
		return filepath;
	}
	int dirs_up = 1;
	for (int i = common_prefix + 1; cwd[i] != '\0'; i++) {
		if (cwd[i] == '/') {
			dirs_up++;
		}
	}
	const char *unique_part = filepath + common_prefix + 1;
	if (*unique_part == '\0') {
		return filepath;
	}
	char dir_up[] = "../";
	size_t l_dir_up = strlen(dir_up);
	size_t l_relpath = dirs_up * l_dir_up + strlen(unique_part) + 1;
	char *relfilepath = xcalloc(1, l_relpath);
	int i = 0;
	for (int d = 0; d < dirs_up; d++) {
		strcpy(relfilepath + i, dir_up);
		i += l_dir_up;
	}
	strcpy(relfilepath + i, unique_part);
	return relfilepath;
}

void
errline(struct location loc)
{
-- 
2.43.0

[harec/patches] build success

builds.sr.ht <builds@sr.ht>
Details
Message ID
<CX879G29C07Q.3CNH5CB25NCH8@cirno2>
In-Reply-To
<20231125210932.257394-1-max@mxsr.de> (view parent)
DKIM signature
missing
Download raw message
harec/patches: SUCCESS in 58s

[Print paths relative to cwd on errors][0] v2 from [Max Schillinger][1]

[0]: https://lists.sr.ht/~sircmpwn/hare-dev/patches/47009
[1]: max@mxsr.de

✓ #1100955 SUCCESS harec/patches/freebsd.yml https://builds.sr.ht/~sircmpwn/job/1100955
✓ #1100956 SUCCESS harec/patches/netbsd.yml  https://builds.sr.ht/~sircmpwn/job/1100956
✓ #1100954 SUCCESS harec/patches/alpine.yml  https://builds.sr.ht/~sircmpwn/job/1100954
Reply to thread Export thread (mbox)