Skip to content

Commit c848b03

Browse files
committed
fix romimg bundled on docker containers
instead of relying on strlen, go straight away with max bufsize
1 parent 67e800b commit c848b03

File tree

3 files changed

+16
-12
lines changed

3 files changed

+16
-12
lines changed

tools/romimg/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ int main(int argc, char **argv)
9999
}
100100
UnloadROMImg(&ROMImg);
101101
} else
102-
ERROR("(Internal fault) Can't create blank image file: %d. Please report.\n", result);
102+
ERROR("(Internal fault) Can't create blank image file: %d (%s). Please report.\n", result, strerror(result));
103103
} else if (argc >= 4 && strcmp(argv[1], "-a") == 0) {
104104
if ((result = LoadROMImg(&ROMImg, argv[2])) == 0) {
105105
for (i = 0, FilesAffected = 0; i < argc - 3; i++) {

tools/romimg/src/platform.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,8 @@ int GetCurrentWorkingDirectory(char *buffer, unsigned int BufferSize);
1212
#else
1313
#define PATHSEP '/'
1414
#endif
15+
#ifndef MAX_PATH
16+
#define MAX_PATH 4096
17+
#endif
1518

16-
#endif /* __PLATFORM_H__ */
19+
#endif /* __PLATFORM_H__ */

tools/romimg/src/romimg.c

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@
66
#include <stdio.h>
77
#include <stdlib.h>
88
#include <string.h>
9+
#include <unistd.h>
10+
#include <limits.h>
911

1012
#include "platform.h"
1113
#include "romimg.h"
1214
#include "SonyRX.h"
1315

16+
#define BUFCHK(X) (X[0] == '\0') ? "" : X
17+
#define IMAGE_COMMENT_BASESIZE 31
18+
1419
struct ROMImgStat
1520
{
1621
void *image;
@@ -139,12 +144,7 @@ static int GetExtInfoStat(const struct ROMImgStat *ImageStat, struct RomDirFileF
139144
int CreateBlankROMImg(const char *filename, ROMIMG *ROMImg)
140145
{
141146
unsigned int CommentLength;
142-
char LocalhostName[32], cwd[128];
143-
#if defined(_WIN32) || defined(WIN32)
144-
char UserName[32] = "";
145-
#else
146-
char* UserName;
147-
#endif
147+
char LocalhostName[32] = "\0", cwd[MAX_PATH] = "\0", UserName[32] = "\0";
148148
struct FileEntry *ResetFile;
149149
struct ExtInfoFieldEntry *ExtInfoEntry;
150150

@@ -154,14 +154,15 @@ int CreateBlankROMImg(const char *filename, ROMIMG *ROMImg)
154154
#if defined(_WIN32) || defined(WIN32)
155155
GetUsername(UserName, sizeof(UserName));
156156
#else
157-
UserName = getenv("USER");
157+
getlogin_r(UserName, sizeof(UserName));
158158
#endif
159159
GetLocalhostName(LocalhostName, sizeof(LocalhostName));
160160
GetCurrentWorkingDirectory(cwd, sizeof(cwd));
161161
/* Comment format: YYYYMMDD-XXXYYY,conffile,<filename>,<user>@<localhost>/<image path> */
162-
CommentLength = 31 + strlen(filename) + strlen(UserName) + strlen(LocalhostName) + strlen(cwd);
163-
ROMImg->comment = (char *)malloc(CommentLength);
164-
sprintf(ROMImg->comment, "%08x,conffile,%s,%s@%s/%s", ROMImg->date, filename, (UserName[0] =='\0')?"":UserName, LocalhostName, cwd);
162+
CommentLength = IMAGE_COMMENT_BASESIZE + strlen(filename) + sizeof(LocalhostName) + sizeof(UserName) + MAX_PATH;
163+
ROMImg->comment = (char *)calloc(0, CommentLength+1);
164+
if (!ROMImg->comment) return ENOMEM;
165+
snprintf(ROMImg->comment, CommentLength, "%08x,conffile,%s,%s@%s/%s", ROMImg->date, filename, BUFCHK(UserName), BUFCHK(LocalhostName), cwd);
165166

166167
// Create a blank RESET file.
167168
ROMImg->NumFiles = 1;

0 commit comments

Comments
 (0)