From 0aa2e39e1d4ad87552bfc3811fb228374586abe3 Mon Sep 17 00:00:00 2001
From: Alexey I. Froloff <raorn@altlinux.org>
Date: Thu, 12 Mar 2009 14:34:16 +0300
Subject: [PATCH 2/2] Fix gcc warnings when compiling with -DFORTIFY_SOURCE=2 -fstack-protector
Some warnings may cause "glibc detected buffer overflow" runtime errors.
In function 'char* strncat(char*, const char*, size_t)',
inlined from 'virtual int32 Scumm::LogicHE::dispatch(int, int, int32*)' at engines/scumm/he/logic_he.cpp:89:
/usr/include/bits/string3.h:153: warning: call to char* __builtin___strncat_chk(char*, const char*, unsigned int, unsigned int) might overflow destination buffer
In function 'char* strncat(char*, const char*, size_t)',
inlined from 'void Saga::Script::sfStub(const char*, Saga::ScriptThread*, int)' at engines/saga/sfuncs.cpp:1573:
/usr/include/bits/string3.h:153: warning: call to char* __builtin___strncat_chk(char*, const char*, unsigned int, unsigned int) might overflow destination buffer
strncat(3) wants size_t n as "available space in destination buffer",
not total buffer size. Pass correct size to strncat(3).
backends/fs/posix/posix-fs-factory.cpp: In member function 'virtual AbstractFSNode* POSIXFilesystemFactory::makeCurrentDirectoryFileNode() const':
backends/fs/posix/posix-fs-factory.cpp:35: warning: ignoring return value of 'char* getcwd(char*, size_t)', declared with attribute warn_unused_result
Return NULL if getcwd(3) fails.
backends/midi/seq.cpp: In member function 'virtual void MidiDriver_SEQ::send(uint32)':
backends/midi/seq.cpp:145: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)', declared with attribute warn_unused_result
backends/midi/seq.cpp: In member function 'virtual void MidiDriver_SEQ::sysEx(const byte*, uint16)':
backends/midi/seq.cpp:170: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)', declared with attribute warn_unused_result
backends/midi/timidity.cpp: In member function 'char* MidiDriver_TIMIDITY::timidity_ctl_command(const char*, ...)':
backends/midi/timidity.cpp:336: warning: ignoring return value of 'ssize_t write(int, const void*, size_t)', declared with attribute warn_unused_result
Emit warning if write(2) fails.
Signed-off-by: Alexey I. Froloff <raorn@altlinux.org>
---
scummvm/backends/fs/posix/posix-fs-factory.cpp | 3 +--
scummvm/backends/midi/seq.cpp | 7 +++++--
scummvm/backends/midi/timidity.cpp | 4 +++-
scummvm/engines/saga/sfuncs.cpp | 4 ++--
scummvm/engines/scumm/he/logic_he.cpp | 4 ++--
5 files changed, 13 insertions(+), 9 deletions(-)
diff --git a/scummvm/backends/fs/posix/posix-fs-factory.cpp b/scummvm/backends/fs/posix/posix-fs-factory.cpp
index ad6e871..3dffb99 100644
a
|
b
|
AbstractFSNode *POSIXFilesystemFactory::makeRootFileNode() const {
|
32 | 32 | |
33 | 33 | AbstractFSNode *POSIXFilesystemFactory::makeCurrentDirectoryFileNode() const { |
34 | 34 | char buf[MAXPATHLEN]; |
35 | | getcwd(buf, MAXPATHLEN); |
36 | | return new POSIXFilesystemNode(buf); |
| 35 | return getcwd(buf, MAXPATHLEN) ? new POSIXFilesystemNode(buf) : NULL; |
37 | 36 | } |
38 | 37 | |
39 | 38 | AbstractFSNode *POSIXFilesystemFactory::makeFileNodePath(const Common::String &path) const { |
diff --git a/scummvm/backends/midi/seq.cpp b/scummvm/backends/midi/seq.cpp
index 0c3ee33..0795dca 100644
a
|
b
|
|
36 | 36 | |
37 | 37 | #include <fcntl.h> |
38 | 38 | #include <unistd.h> |
| 39 | #include <errno.h> |
39 | 40 | |
40 | 41 | //////////////////////////////////////// |
41 | 42 | // |
… |
… |
void MidiDriver_SEQ::send(uint32 b) {
|
142 | 143 | warning("MidiDriver_SEQ::send: unknown : %08x", (int)b); |
143 | 144 | break; |
144 | 145 | } |
145 | | write(device, buf, position); |
| 146 | if (write(device, buf, position) < 0) |
| 147 | warning("MidiDriver_SEQ::send: write(): %s", strerror(errno)); |
146 | 148 | } |
147 | 149 | |
148 | 150 | void MidiDriver_SEQ::sysEx (const byte *msg, uint16 length) { |
… |
… |
void MidiDriver_SEQ::sysEx (const byte *msg, uint16 length) {
|
167 | 169 | buf[position++] = _device_num; |
168 | 170 | buf[position++] = 0; |
169 | 171 | |
170 | | write (device, buf, position); |
| 172 | if (write (device, buf, position) < 0) |
| 173 | warning("MidiDriver_SEQ::sysEx: write(): %s", strerror(errno)); |
171 | 174 | } |
172 | 175 | |
173 | 176 | |
diff --git a/scummvm/backends/midi/timidity.cpp b/scummvm/backends/midi/timidity.cpp
index 9f119b6..8eeb7c8 100644
a
|
b
|
char *MidiDriver_TIMIDITY::timidity_ctl_command(const char *fmt, ...) {
|
333 | 333 | buff[len++] = '\n'; |
334 | 334 | |
335 | 335 | /* write command to control socket */ |
336 | | write(_control_fd, buff, len); |
| 336 | if (write(_control_fd, buff, len) < 0) { |
| 337 | warning("TiMidity: write(): %s", strerror(errno)); |
| 338 | } |
337 | 339 | } |
338 | 340 | |
339 | 341 | while (1) { |
diff --git a/scummvm/engines/saga/sfuncs.cpp b/scummvm/engines/saga/sfuncs.cpp
index cc80fbc..91f24ea 100644
a
|
b
|
void Script::sfStub(const char *name, ScriptThread *thread, int nArgs) {
|
1570 | 1570 | |
1571 | 1571 | for (int i = 0; i < nArgs; i++) { |
1572 | 1572 | snprintf(buf1, 100, "%d", thread->pop()); |
1573 | | strncat(buf, buf1, 256); |
| 1573 | strncat(buf, buf1, sizeof(buf) - strlen(buf) - 1); |
1574 | 1574 | if (i + 1 < nArgs) |
1575 | | strncat(buf, ", ", 256); |
| 1575 | strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1); |
1576 | 1576 | } |
1577 | 1577 | |
1578 | 1578 | debug(0, "%s)", buf); |
diff --git a/scummvm/engines/scumm/he/logic_he.cpp b/scummvm/engines/scumm/he/logic_he.cpp
index 255deea..f95b31c 100644
a
|
b
|
int32 LogicHE::dispatch(int op, int numArgs, int32 *args) {
|
86 | 86 | |
87 | 87 | for (int i = 1; i < numArgs; i++) { |
88 | 88 | snprintf(tmp, 32, ", %d", args[i]); |
89 | | strncat(str, tmp, 256); |
| 89 | strncat(str, tmp, sizeof(str) - strlen(str) - 1); |
90 | 90 | } |
91 | | strncat(str, "])", 256); |
| 91 | strncat(str, "])", sizeof(str) - strlen(str) - 1); |
92 | 92 | |
93 | 93 | debug(0, "%s", str); |
94 | 94 | #else |