On Fri, 2014-09-26 at 07:44 +0200, Pino Toscano wrote:
> On 2014-09-25 14:05, Svante Signell wrote:
> > Hi Pino
> >
> > On Thu, 2014-09-25 at 10:08 +0200, Pino Toscano wrote:
> > > On 2014-09-24 19:11, Philipp A. Hartmann wrote:
> > > > I would probably do it like this (untested):
> > > >
> > > > const char *path =
> > > > QFile::encodeName(fi.absoluteFilePath()).constData();
> > >
> > > QFile::encodeName returns a temporary QByteArray, and getting a
> > > pointer to its internal char* will cause it to be a dangling
> > > pointer.
> > >
> > > const QByteArray path = QFile::encodeName(fi.absoluteFilePath());
> > >
> > > and then use path.constData() whenever needed.
> > >
> > > > std::vector<char> s(sb.st_size + 1);
> > >
> > > Please use QByteArray instead.
> >
> > Ok now?
>
> You've forgot the second part of what I said.
Hi again (2.5 years later). Attached is a patch using QByteArray. Hopefully it
is OK now? (I'm not fluent in C++)
Thanks!
Index: kdiff3-0.9.98/src-QT4/fileaccess.cpp
===================================================================
--- kdiff3-0.9.98.orig/src-QT4/fileaccess.cpp
+++ kdiff3-0.9.98/src-QT4/fileaccess.cpp
@@ -235,14 +235,22 @@ void FileAccess::setFile( const QFileInf
d()->m_linkTarget = fi.readLink();
#else
// Unfortunately Qt4 readLink always returns an absolute path, even if the link is relative
- char s[PATH_MAX+1];
- int len = readlink(QFile::encodeName(fi.absoluteFilePath()).constData(), s, PATH_MAX);
- if ( len>0 )
+ const QByteArray path = QFile::encodeName(fi.absoluteFilePath());
+ struct stat sb;
+ bool resolved = false;
+
+ if ( lstat(path.constData(), &sb) != -1 )
{
- s[len] = '\0';
- d()->m_linkTarget = QFile::decodeName(s);
+ QByteArray s = QByteArray(path.constData(), sb.st_size + 1);
+ ssize_t len = readlink(path.constData(), s.data(), s.size());
+ s[len] = '\0';
+ if ( len > 0 && len <= sb.st_size )
+ {
+ resolved = true;
+ d()->m_linkTarget = QFile::decodeName(s);
+ }
}
- else
+ if ( !resolved )
{
d()->m_linkTarget = fi.readLink();
}