-
Notifications
You must be signed in to change notification settings - Fork 50
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
clean up optionsNameThumbnail() #248
clean up optionsNameThumbnail() #248
Conversation
2ebf513
to
bd6e281
Compare
Force-push because I had an extra |
I don't really like the existing function either, but the improvement here is very marginal. It's still directly messing with pointer arithmetic which are error prone. I honestly had no clue that a screenshot tool would have so much string manipulation going on! If I were to write scrot from scratch then I definitely would've opted for using sized-strings, with "higher level" string manipulation routines. I also wouldn't have created a massive web of allocated pointers, each of which needs individual managing, which is error prone (evident by the amount of leaks we've discovered lately). Instead the strings would go into an arena so that they can be managed in groups. But in any-case, we're not writing scrot from scratch so we're far past that point :)
But if this still bothers you, then you can use the stream functions from #250, they're guarded by assertions so mistakes are less likely to go unnoticed. Something like this (pseudo-code): Stream ret = {0};
streamReserve(newNameLen);
streamMem(name, ..);
streamMem(thumbSuffix, sizeof(thumbSuffix) - 1);
if (ext) streamMem(ext, extLen);
streamChar('\0');
return ret.buf; Also, I'd probably use |
This version has a slightly better algorithm (less
It does, however, do a lot of working around the poor design of C's string.h. If you want, I can change it to this version which doesn't: char *optionsNameThumbnail(const char *name)
{
char *ext = strrchr(name, '.');
int basenameLen = ext ? ext-name : INT_MAX;
char *ret;
if (asprintf(&ret, "%.*s-thumb%s", basenameLen, name, ext ? ext : "") == -1)
err(1, "asprintf");
return ret;
} Although I'm not sure how widely adopted I considered Memory management in scrot isn't complicated, there's just a lot of bad code that needs fixing, but all we need to do is look for the last use of a variable and |
Unfortunate, most other BSDs have it. Drop that idea then. |
One more thing I realized (just mentioning it so I don't forget about it later, doesn't need to be fixed in this PR) is that this function probably won't work reliably for cases like |
That's similar to #228. I'd like to make the code This would open the path for some mitigations that restrict the filesystem view like chroot, unveil, and landlock, but I'm not sure of how feasible those are because the output directory could contain |
bd6e281
to
598428c
Compare
Should be ready for review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't test (yet) but looks OK.
e5772c8
to
7c09147
Compare
Thanks. |
Sorry but this function has been bugging me ever since #220. I knew it could be better but I failed back then.
I've tested it with these commands:
src/scrot --thumb 50 test.png src/scrot --thumb 50 test src/scrot --thumb 50 te.st.png
As you can see, all the right files were created: