RISC OS: fix up use of strncpy

Ensure that strings copied using strncpy are NUL terminated.

Additionally, replace use of strncpy entirely where we are writing
into non-indirected OS icon blocks (where an unterminated 12
character long string is perfectly valid).
This commit is contained in:
John-Mark Bell 2022-06-03 02:39:30 +01:00
parent 0d114e10b2
commit 617c6207bc
4 changed files with 20 additions and 13 deletions

View File

@ -313,8 +313,8 @@ gui_download_window_create(download_context *ctx, struct gui_window *gui)
/** @todo change this to take a reference to the nsurl and use
* that value directly rather than using a fixed buffer.
*/
strncpy(dw->url, nsurl_access(url), sizeof dw->url);
dw->url[sizeof dw->url - 1] = 0;
strncpy(dw->url, nsurl_access(url), sizeof(dw->url) - 1);
dw->url[sizeof(dw->url) - 1] = 0;
dw->status[0] = 0;
gettimeofday(&dw->start_time, 0);
@ -414,7 +414,8 @@ gui_download_window_create(download_context *ctx, struct gui_window *gui)
return 0;
}
else {
strncpy(dw->path, local_path, sizeof dw->path);
strncpy(dw->path, local_path, sizeof(dw->path) - 1);
dw->path[sizeof(dw->path)-1] = 0;
free(local_path);
}
@ -484,7 +485,8 @@ static void gui_download_window_error(struct gui_download_window *dw,
riscos_schedule(-1, ro_gui_download_update_status_wrapper, dw);
/* place error message in status icon in red */
strncpy(dw->status, error_msg, sizeof dw->status);
strncpy(dw->status, error_msg, sizeof(dw->status) - 1);
dw->status[sizeof(dw->status)-1] = 0;
error = xwimp_set_icon_state(dw->window,
ICON_DOWNLOAD_STATUS,
wimp_COLOUR_RED << wimp_ICON_FG_COLOUR_SHIFT,
@ -872,11 +874,11 @@ bool ro_gui_download_click(wimp_pointer *pointer)
ro_gui_drag_icon(x, y, sprite);
} else if (pointer->i == ICON_DOWNLOAD_DESTINATION) {
char command[256] = "Filer_OpenDir ";
char command[sizeof(dw->path) + 14 + 1] = "Filer_OpenDir ";
char *dot;
strncpy(command + 14, dw->path, 242);
command[255] = 0;
strncpy(command + 14, dw->path, sizeof(command) - 14 - 1);
command[sizeof(command) - 1] = 0;
dot = strrchr(command, '.');
if (dot) {
os_error *error;
@ -1384,7 +1386,8 @@ bool ro_gui_download_save(struct gui_download_window *dw,
}
dw->saved = true;
strncpy(dw->path, file_name, sizeof dw->path);
strncpy(dw->path, file_name, sizeof(dw->path) - 1);
dw->path[sizeof(dw->path)-1] = 0;
if (!dw->send_dataload || dw->save_message.data.data_xfer.est_size != -1)
ro_gui_download_remember_dir(file_name);

View File

@ -189,7 +189,8 @@ struct button_bar *ro_gui_button_bar_create(struct theme_descriptor *theme,
icon->bar_next = NULL;
strncpy(icon->sprite, buttons[def].icon,
BUTTONBAR_SPRITE_NAME_LENGTH);
BUTTONBAR_SPRITE_NAME_LENGTH - 1);
icon->sprite[BUTTONBAR_SPRITE_NAME_LENGTH-1] = 0;
snprintf(icon->validation, BUTTONBAR_VALIDATION_LENGTH,
"R5;S%s,p%s", icon->sprite, icon->sprite);

View File

@ -40,8 +40,10 @@
#include "utils/config.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utf8.h"
#include "utils/nsoption.h"
#include "utils/nsurl.h"
#include "utils/utf8.h"
#include "utils/utils.h"
#include "netsurf/browser_window.h"
#include "netsurf/window.h"
#include "netsurf/bitmap.h"
@ -60,7 +62,6 @@
#include "riscos/menus.h"
#include "riscos/message.h"
#include "riscos/mouse.h"
#include "utils/nsoption.h"
#include "riscos/query.h"
#include "riscos/save.h"
#include "riscos/save_draw.h"
@ -257,7 +258,8 @@ ro_gui_save_create_thumbnail(struct hlcache_handle *h, const char *name)
}
sprite_header = (osspriteop_header *)(area + 1);
strncpy(sprite_header->name, name, 12);
memset(sprite_header->name, 0, 12);
memcpy(sprite_header->name, name, min(strlen(name), 12));
/* we can't resize the saveas sprite area because it may move

View File

@ -3594,7 +3594,8 @@ static void gui_window_set_title(struct gui_window *g, const char *title)
title, scale_disp);
}
} else {
strncpy(g->title, title, sizeof(g->title));
strncpy(g->title, title, sizeof(g->title) - 1);
g->title[sizeof(g->title)-1] = 0;
}
ro_gui_set_window_title(g->window, g->title);