Skip to content
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

fix romimg bundled on docker containers #673

Merged
merged 4 commits into from
Nov 11, 2024
Merged

Conversation

israpps
Copy link
Contributor

@israpps israpps commented Nov 6, 2024

instead of relying on strlen, go straight away with max bufsize. before this change the romimg from docker would segfault while local build works like a charm

New feature, -c and -a commands will have new variants, -C and -A, these will manipulate the filename written to ROMFS removing extension and converting to uppercase

So for example

romimg -C IOPRP.IMG $PS2SDK/iop/irx/secrman.irx

Will register secrman.irx as SECRMAN on the image, this seems to be needed when the IOPRP is replacing startup IOP modules

#else
char* UserName;
#endif
char LocalhostName[32] = "\0", cwd[128] = "\0", UserName[32] = "\0";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still think that 128 is too short for computer usage. You should use MAX_PATH instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess there is no cross platform definition of MAX_PATH. linux seems to use PATH_MAX

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry PATH_MAX or whatever is called, I don't remember right now the POSIX solution for that hehe

tools/romimg/src/romimg.c Outdated Show resolved Hide resolved
tools/romimg/src/romimg.c Outdated Show resolved Hide resolved
@israpps israpps force-pushed the romimg-fix branch 2 times, most recently from 5029ea8 to 699e6f2 Compare November 6, 2024 13:34
instead of relying on strlen, go straight away with max bufsize
@israpps israpps marked this pull request as draft November 6, 2024 13:59
@israpps israpps marked this pull request as ready for review November 6, 2024 14:44
@israpps israpps force-pushed the romimg-fix branch 2 times, most recently from 7c8a04d to 2edbeef Compare November 6, 2024 15:00
@israpps
Copy link
Contributor Author

israpps commented Nov 6, 2024

@fjtrujy any idea with this compiler warning? not sure what to do to get rid of it.

My local build (WSL) does not complain of this

@uyjulian
Copy link
Member

uyjulian commented Nov 6, 2024

Add or remove one from either buffer or length

@israpps
Copy link
Contributor Author

israpps commented Nov 6, 2024

Add or remove one from either buffer or length

will take a look. buffer is 11 and i'm reading 10 already

@israpps israpps force-pushed the romimg-fix branch 9 times, most recently from 29f4ef8 to ce264c6 Compare November 6, 2024 17:14
@uyjulian
Copy link
Member

uyjulian commented Nov 6, 2024

The buffer is being truncated.

That is, the destination buffer is smaller than the source buffer. So increase the destination buffer size or decrease the source buffer size

@israpps
Copy link
Contributor Author

israpps commented Nov 6, 2024

im kinda lost now

/usr/include/x86_64-linux-gnu/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’ output may be truncated copying 9 bytes from a string of length 9 [-Werror=stringop-truncation]

@fjtrujy
Copy link
Member

fjtrujy commented Nov 7, 2024

im kinda lost now

/usr/include/x86_64-linux-gnu/bits/string_fortified.h:95:10: error: ‘__builtin_strncpy’ output may be truncated copying 9 bytes from a string of length 9 [-Werror=stringop-truncation]

The error is quite easy, you do something like:

strncpy(file->RomDir.name, fname, sizeof(file->RomDir.name) - 1);

This -1 at the end is the root of the issue.
It should be directly:

strncpy(file->RomDir.name, fname, sizeof(file->RomDir.name));

And you also have it in other places as:

strncpy(tbuf, fname, sizeof(tbuf) - 1);

I think that in general the way of using strncpy should be:

strncpy(a, b, sizeof(a));


#include "platform.h"
#include "romimg.h"
#include "SonyRX.h"

#define BUFCHK(X) (X[0] == '\0') ? "" : X
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think this is really needed?
I think it makes no sense at all, the behavior should be the same, no matter if the first character is \0

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it remained from older approach. will deal with it now

I realised I placed this recipe into the C makefile, leaving C++ and IOP C without this recipe
@israpps israpps force-pushed the romimg-fix branch 2 times, most recently from d8ce025 to b575b96 Compare November 11, 2024 03:18
@israpps
Copy link
Contributor Author

israpps commented Nov 11, 2024

fixed. seems to work as expected!

@israpps israpps requested a review from fjtrujy November 11, 2024 03:20
@fjtrujy
Copy link
Member

fjtrujy commented Nov 11, 2024

Finally 😄

CommentLength = 31 + strlen(filename) + strlen(UserName) + strlen(LocalhostName) + strlen(cwd);
ROMImg->comment = (char *)malloc(CommentLength);
sprintf(ROMImg->comment, "%08x,conffile,%s,%s@%s/%s", ROMImg->date, filename, (UserName[0] =='\0')?"":UserName, LocalhostName, cwd);
CommentLength = IMAGE_COMMENT_BASESIZE + strlen(filename) + sizeof(LocalhostName) + sizeof(UserName) + MAX_PATH;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use sizeof at the end


#if defined(_WIN32) || defined(WIN32)
#define PATHSEP '\\'
#else
#define PATHSEP '/'
#endif
#ifndef MAX_PATH
#define MAX_PATH 4096
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PATH_MAX is available in all the OS, so use it instead and remove this from here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

windows has the name flipped MAX_PATH instead of PATH_MAX

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using MAX_PATH in my fork and all the CI/CD was passing.
You are probably speaking about compiling natively, not through MinGW, in theory, we shouldn't care about these platforms...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure where it is defined then... everything in google says on limits.h but it's not the case

#else
char* UserName;
#endif
char LocalhostName[32] = "\0", cwd[MAX_PATH] = "\0", UserName[32] = "\0";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend to initializate them with = {0} instead, it is equivalent to put all the fields with \0 directly

FILE *InputFile;
int result;
unsigned int FileDateStamp;
unsigned short FileVersion;
if ((InputFile = fopen(path, "rb")) != NULL) {
const char* fname = strrchr(path, PATHSEP);
if (fname == NULL) fname = path; else fname++;
if (upperconv) {
strncpy(tbuf, fname, sizeof(tbuf));
tbuf[sizeof(tbuf) - 1] = '\0';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you initialize with = {0} then this line shouldn't be required either

@israpps israpps force-pushed the romimg-fix branch 2 times, most recently from 1b630de to 8dbb79a Compare November 11, 2024 12:40
@fjtrujy fjtrujy merged commit ed5ead7 into ps2dev:master Nov 11, 2024
11 checks passed
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

Successfully merging this pull request may close these issues.

3 participants