Correct use of string_copy when building strings.

This commit is contained in:
Steveice10 2018-08-26 00:09:05 -07:00
parent 62e9632a43
commit 6a4a446df0
3 changed files with 28 additions and 20 deletions

View File

@ -22,8 +22,10 @@ bool string_is_empty(const char* str) {
}
void string_copy(char* dst, const char* src, size_t size) {
strncpy(dst, src, size - 1);
dst[size - 1] = '\0';
if(size > 0) {
strncpy(dst, src, size - 1);
dst[size - 1] = '\0';
}
}
void string_get_file_name(char* out, const char* file, u32 size) {

View File

@ -370,18 +370,18 @@ void action_install_url(const char* confirmMessage, const char* urls, const char
u32 len = currEnd - currStart;
if((len < 7 || strncmp(currStart, "http://", 7) != 0) && (len < 8 || strncmp(currStart, "https://", 8) != 0)) {
if(len > DOWNLOAD_URL_MAX - 7) {
len = DOWNLOAD_URL_MAX - 7;
if(len > DOWNLOAD_URL_MAX - 8) {
len = DOWNLOAD_URL_MAX - 8;
}
strncpy(data->urls[data->installInfo.total], "http://", 8);
strncpy(&data->urls[data->installInfo.total][7], currStart, len);
string_copy(data->urls[data->installInfo.total], "http://", 8);
string_copy(&data->urls[data->installInfo.total][7], currStart, len + 1);
} else {
if(len > DOWNLOAD_URL_MAX) {
len = DOWNLOAD_URL_MAX;
if(len > DOWNLOAD_URL_MAX - 1) {
len = DOWNLOAD_URL_MAX - 1;
}
strncpy(data->urls[data->installInfo.total], currStart, len);
string_copy(data->urls[data->installInfo.total], currStart, len + 1);
}
data->installInfo.total++;
@ -400,11 +400,11 @@ void action_install_url(const char* confirmMessage, const char* urls, const char
}
u32 len = currEnd - currStart;
if(len > FILE_PATH_MAX) {
len = FILE_PATH_MAX;
if(len > FILE_PATH_MAX - 1) {
len = FILE_PATH_MAX - 1;
}
strncpy(data->paths[i], currStart, len);
string_copy(data->paths[i], currStart, len + 1);
currStart = currEnd + 1;
}

View File

@ -92,7 +92,7 @@ static Result task_populate_titles_add_twl(populate_titles_data* data, FS_MediaT
Result res = 0;
u64 realTitleId = 0;
char productCode[12] = {'\0'};
char productCode[0x10] = {'\0'};
u16 version = 0;
u64 installedSize = 0;
@ -107,7 +107,7 @@ static Result task_populate_titles_add_twl(populate_titles_data* data, FS_MediaT
installedSize = entry.size;
} else if(R_SUCCEEDED(res = headerRes)) {
memcpy(&realTitleId, &header[0x230], sizeof(realTitleId));
memcpy(productCode, header, sizeof(productCode));
memcpy(productCode, header, 0xC);
version = header[0x01E];
u32 size = 0;
@ -127,7 +127,7 @@ static Result task_populate_titles_add_twl(populate_titles_data* data, FS_MediaT
if(titleInfo != NULL) {
titleInfo->mediaType = mediaType;
titleInfo->titleId = realTitleId;
string_copy(titleInfo->productCode, productCode, 12);
string_copy(titleInfo->productCode, productCode, sizeof(titleInfo->productCode));
titleInfo->version = version;
titleInfo->installedSize = installedSize;
titleInfo->twl = true;
@ -142,25 +142,31 @@ static Result task_populate_titles_add_twl(populate_titles_data* data, FS_MediaT
utf16_to_utf8((uint8_t*) title, bnr_select_title(bnr), sizeof(title) - 1);
if(strchr(title, '\n') == NULL) {
size_t len = strlen(title);
string_copy(item->name, title, len);
string_copy(titleInfo->meta.shortDescription, title, len);
string_copy(item->name, title, sizeof(item->name));
string_copy(titleInfo->meta.shortDescription, title, sizeof(titleInfo->meta.shortDescription));
} else {
char* destinations[] = {titleInfo->meta.shortDescription, titleInfo->meta.longDescription, titleInfo->meta.publisher};
u32 destinationLens[] = {sizeof(titleInfo->meta.shortDescription), sizeof(titleInfo->meta.longDescription), sizeof(titleInfo->meta.publisher)};
int currDest = 0;
char* last = title;
char* curr = NULL;
while(currDest < 3 && (curr = strchr(last, '\n')) != NULL) {
string_copy(destinations[currDest++], last, curr - last);
u32 copyLen = curr - last + 1;
if(copyLen > destinationLens[currDest]) {
copyLen = destinationLens[currDest];
}
string_copy(destinations[currDest++], last, copyLen);
last = curr + 1;
*curr = ' ';
}
string_copy(item->name, title, last - title);
if(currDest < 3) {
string_copy(destinations[currDest], last, strlen(title) - (last - title));
string_copy(destinations[currDest], last, destinationLens[currDest]);
}
}