Skip to content

ext2spice.c mergeAttr() request for review #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
dlmiles opened this issue Feb 18, 2025 · 2 comments
Open

ext2spice.c mergeAttr() request for review #381

dlmiles opened this issue Feb 18, 2025 · 2 comments

Comments

@dlmiles
Copy link
Contributor

dlmiles commented Feb 18, 2025

t = (char *) strcat(*a1, *a2);

void
mergeAttr(a1, a2)
    char **a1, **a2;
{
    if (*a1 == NULL)
	*a1 = *a2;
    else
    {
	char *t;
	int l1 = strlen(*a1);
	int l2 = strlen(*a2);
	t = (char *) mallocMagic((unsigned int)((l1 + l2) + 1));
	t = (char *) strcat(*a1, *a2);  // PLEASE REVIEW THIS AREA
	freeMagic(*a1);
	*a1 = t;
    }
}

Consider:

	char *t;
	int l1 = strlen(*a1);
	int l2 = strlen(*a2);
	t = (char *) mallocMagic((unsigned int)((l1 + l2) + 1));
#if 0
 	t = (char *) strcat(*a1, *a2);
#else
	strcpy(t, *a1);
	strcat(t, *a2);
#endif
	freeMagic(*a1);
	*a1 = t;
@dlmiles
Copy link
Contributor Author

dlmiles commented Feb 19, 2025

Please also check this macro version of mergeAttr() of the same

https://github.com/RTimothyEdwards/magic/blob/master/ext2sim/ext2sim.c#L1758

Looks unused, maybe safer to delete ?

@RTimothyEdwards
Copy link
Owner

I agree the original version doesn't make any sense; the strcat() function merges string a2 into string a1 which does not necessarily have room to hold it, and the returns a pointer to string a1, causing the allocated memory t to be orphaned. I suggest fixing this as you have done above.

Likewise, I can find no place that calls mergeAttr() in ext2sim so I agree with your assessment of just removing it altogether.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants