From 962be87deb471b1625935c03c26a683702ebc341 Mon Sep 17 00:00:00 2001 From: Mohan Yelugoti Date: Fri, 30 Aug 2024 19:27:41 -0400 Subject: [PATCH] v.in.dwg: avoid src and dst overlap in sprintf Currently, one instance of sprintf has same src and dst pointers, probably to append new information to the buffer. But, having overlapping src and dst pointers is undefined behavior. In the new set of changes, code is refactored to automatically start writing new data at the end of previous data. snprintf is also used over sprintf to be wary of the fact that buffer size is limited to 2000. Since, snprintf automatically adds a null character to the end, to find the remaining buffer space, we subtract existing buffer length as well as null character to find maximum possible space to write in the buffer. This translates to '2000 - buflen - 1'. Signed-off-by: Mohan Yelugoti --- vector/v.in.dwg/main.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/vector/v.in.dwg/main.c b/vector/v.in.dwg/main.c index e21602ecb90..a35bd9f84a7 100644 --- a/vector/v.in.dwg/main.c +++ b/vector/v.in.dwg/main.c @@ -137,8 +137,11 @@ int main(int argc, char *argv[]) if (!adInitAd2(path, &initerror)) { sprintf(buf, _("Unable to initialize OpenDWG Toolkit, error: %d: %s."), initerror, adErrorStr(initerror)); - if (initerror == AD_UNABLE_TO_OPEN_INIT_FILE) - sprintf(buf, _("%s Cannot open %s"), buf, path); + size_t buflen = strlen(buf); + if (initerror == AD_UNABLE_TO_OPEN_INIT_FILE) { + snprintf(buf + buflen, 2000 - buflen - 1, (" Cannot open %s"), + path); + } G_fatal_error(buf); } adSetupDwgRead();