After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 664043 - Add-data-based-content-type-guessing-for-win32.patch
Add-data-based-content-type-guessing-for-win32.patch
Status: RESOLVED OBSOLETE
Product: glib
Classification: Platform
Component: win32
unspecified
Other Windows
: Normal normal
: ---
Assigned To: gtk-win32 maintainers
gtk-win32 maintainers
Depends on:
Blocks: 661936 730453
 
 
Reported: 2011-11-14 15:05 UTC by Hib Eris
Modified: 2018-05-24 13:31 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
0001-Add-data-based-content-type-guessing-for-win32.patch (2.88 KB, patch)
2011-11-14 15:06 UTC, Hib Eris
none Details | Review
0001-Add-data-based-content-type-guessing-for-win32.patch (2.91 KB, patch)
2011-11-21 19:17 UTC, Hib Eris
none Details | Review
0001-Add-data-based-content-type-guessing-for-win32.patch (2.94 KB, patch)
2011-11-22 15:02 UTC, Hib Eris
needs-work Details | Review
0001-Add-data-based-content-type-guessing-for-win32.patch (3.45 KB, patch)
2011-12-07 13:42 UTC, Hib Eris
none Details | Review
0001-Add-data-based-content-type-guessing-for-win32.patch (3.35 KB, patch)
2013-02-06 10:08 UTC, Hib Eris
none Details | Review

Description Hib Eris 2011-11-14 15:05:26 UTC
The windows port of glib currently lacks content type guessing from file data.
This patch will add it.

It does not link in urlmon.dll directly because the mingw32 compiler does not provide an urlmon library to link against afaik. Instead it uses LoadLibrary() for dynamic loading.
Comment 1 Hib Eris 2011-11-14 15:06:58 UTC
Created attachment 201373 [details] [review]
0001-Add-data-based-content-type-guessing-for-win32.patch
Comment 2 Alexander Larsson 2011-11-21 14:22:12 UTC
The initialization (the LoadLibrary stuff) is not threadsafe, it needs some g_once action.

Also, why are you copying the data before passing it to FindMimeFromData?
Comment 3 Hib Eris 2011-11-21 19:17:31 UTC
Created attachment 201853 [details] [review]
0001-Add-data-based-content-type-guessing-for-win32.patch

(In reply to comment #2)
> The initialization (the LoadLibrary stuff) is not threadsafe, it needs some
> g_once action.

Is this new patch better?

> Also, why are you copying the data before passing it to FindMimeFromData?

Because FindMimeFromData needs a (void *) data, not a (const void *) or (const gchar *) data. I do not know why it needs this data to be non-const, but just passing in a (const gchar *) causes a segfault.
Comment 4 Hib Eris 2011-11-22 15:02:32 UTC
Created attachment 201928 [details] [review]
0001-Add-data-based-content-type-guessing-for-win32.patch

Updated patch to check for NULL return value.
Comment 5 Alexander Larsson 2011-11-25 08:10:34 UTC
Review of attachment 201928 [details] [review]:

::: gio/gcontenttype.c
@@ +355,3 @@
+    {
+       static gsize initialized = FALSE;
+       static t_FindMimeFromData p_FindMimeFromData = NULL;

You can't use a separate static that is not the one used in the g_once.., then there is nothing that protects it. Use the gsize (an int that fits a pointer) to store the actual function pointer, and use a separate value, like e.g. 1 to mark that initialization failed (so it won't be run again).

@@ +364,3 @@
+            if (urlmon != NULL)
+              p_FindMimeFromData = (t_FindMimeFromData) GetProcAddress (
+	 	urlmon, "FindMimeFromData");

There is some possibility to allow glib to run arbitrary code here, by placing
a urlmon.dll in the PATH. We should probably do something similar to:
http://git.gnome.org/browse/gtk+/commit?id=88f54ea47d4a55bbbf9e34a7a0502f365eb69ae5 to avoid this.

@@ +374,3 @@
+
+            LPVOID pBuffer = (LPVOID) g_malloc (data_size);
+            memcpy (pBuffer, data, data_size);

This really should not be necessary. (void *) or (const void *) are identical in the resulting binary, its purely a language construct. If it did crash we need to figure out why, not just work around it by adding inefficiencies.
Comment 6 Hib Eris 2011-12-07 13:42:26 UTC
Created attachment 202988 [details] [review]
0001-Add-data-based-content-type-guessing-for-win32.patch

New patch taking into account Alex's remarks.

(In reply to comment #5)
> Review of attachment 201928 [details] [review]:
> 
> ::: gio/gcontenttype.c
> @@ +355,3 @@
> +    {
> +       static gsize initialized = FALSE;
> +       static t_FindMimeFromData p_FindMimeFromData = NULL;
> 
> You can't use a separate static that is not the one used in the g_once.., then
> there is nothing that protects it. Use the gsize (an int that fits a pointer)
> to store the actual function pointer, and use a separate value, like e.g. 1 to
> mark that initialization failed (so it won't be run again).

In the new patch I use g_once () instead of g_once_init_enter () in the hope that this will work out right in a multi-threaded setting. As I am very new to g_once... and multi-threading, please review carefully again.

> @@ +364,3 @@
> +            if (urlmon != NULL)
> +              p_FindMimeFromData = (t_FindMimeFromData) GetProcAddress (
> +         urlmon, "FindMimeFromData");
> 
> There is some possibility to allow glib to run arbitrary code here, by placing
> a urlmon.dll in the PATH. We should probably do something similar to:
> http://git.gnome.org/browse/gtk+/commit?id=88f54ea47d4a55bbbf9e34a7a0502f365eb69ae5
> to avoid this.

I created a new function _g_win32_load_system_library_W () for this. Maybe this fuction could be placed in a more general location so that other code can benefit from it as well?

> 
> @@ +374,3 @@
> +
> +            LPVOID pBuffer = (LPVOID) g_malloc (data_size);
> +            memcpy (pBuffer, data, data_size);
> 
> This really should not be necessary. (void *) or (const void *) are identical
> in the resulting binary, its purely a language construct. If it did crash we
> need to figure out why, not just work around it by adding inefficiencies.

According to (a user comment on) http://msdn.microsoft.com/en-us/library/ms775107%28v=vs.85%29.aspx
the pBuffer must be writable or else FindMimeFromData() will fail/crash. That is exactly what I experienced in testing. So really, it seems to be necessary.
But the same user comment also states that only a maximum of 256 bytes will be used from pBuffer, so I adjusted the code to only copy the first 256 bytes.
Comment 7 Hib Eris 2013-02-06 10:08:52 UTC
Created attachment 235298 [details] [review]
0001-Add-data-based-content-type-guessing-for-win32.patch

Patch updated to current master (2.35.7)
Comment 8 GNOME Infrastructure Team 2018-05-24 13:31:11 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/glib/issues/477.