Skip to content

Commit

Permalink
v.in.dwg: avoid src and dst overlap in sprintf
Browse files Browse the repository at this point in the history
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 <ymdatta.work@gmail.com>
  • Loading branch information
ymdatta committed Aug 31, 2024
1 parent 04b5af7 commit 962be87
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions vector/v.in.dwg/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 962be87

Please sign in to comment.