trouble with g_stat

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

trouble with g_stat

GaryW

My use of g_stat causes a segfault, but regular stat works ok. I’m using mingw64 on Win10 under msys2.

Both of the following examples compile ok, but while this works:

//------------------

GString *fspec;

struct stat sB;

//fspec->str tested to hold the correct file spec…

if(stat(fspec->str,&sB)>0){… [as expected]

 

this doesn’t:

//------------------

GString *fspec;

GStatBuf *stBuf;

//fspec->str tested to hold the correct file spec…

if(g_stat(fspec->str,stBuf)<0){… [9476 Segmentation fault]

 

I’ve tried tinkering with various permutations, but nothing else compiles.

Incidentally, I am just getting stated with GTK+/GLib, and they look like just what I need; however, I’m concerned about the lack of examples on the web. I bought Foundations of GTK+ Development, and it has a lot of useful information, but it doesn’t have as many examples as I’d like, either. In the beginning, code examples are really key, so I’d welcome any URLs or suggestions. Thank you.

 


_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|

Re: trouble with g_stat

James Cameron-2
On Sun, Jun 03, 2018 at 10:27:48PM -0400, GaryW wrote:

> My use of g_stat causes a segfault, but regular stat works ok. I’m using
> mingw64 on Win10 under msys2.
> [...]
>
> GString *fspec;
>
> GStatBuf *stBuf;
>
> //fspec->str tested to hold the correct file spec…
>
> if(g_stat(fspec->str,stBuf)<0){… [9476 Segmentation fault]

stBuf is an uninitialised pointer.  g_stat will try to write to the
address; undefined behaviour results.

Try allocating some memory of the right size, and assign that address
to the pointer before calling g_stat?

--
James Cameron
http://quozl.netrek.org/
_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|

Re: trouble with g_stat

Hrvoje Niksic-2
In reply to this post by GaryW
On 06/04/2018 04:27 AM, GaryW wrote:

> Both of the following examples compile ok, but while this works:
>
> //------------------
>
> GString *fspec;
>
> struct stat sB;
>
> //fspec->str tested to hold the correct file spec…
>
> if(stat(fspec->str,&sB)>0){… [as expected]
>
> this doesn’t:
>
> //------------------
>
> GString *fspec;
>
> GStatBuf *stBuf;
>
> //fspec->str tested to hold the correct file spec…
>
> if(g_stat(fspec->str,stBuf)<0){… [9476 Segmentation fault]
>
> I’ve tried tinkering with various permutations, but nothing else compiles.

Have you tried:

GString *fspec;
GStatBuf stBuf;
if (g_stat(fspec->str, &stBuf) < 0) ...

That's how you use regular stat, after all.
_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list