Your message dated Sun, 21 Feb 2010 15:15:46 +0100
with message-id <[email protected]>
and subject line Re: Bug#569728: No bug
has caused the Debian Bug report #569728,
regarding libmagic-dev: comparing magic_file() result string fails
to be marked as done.

This means that you claim that the problem has been dealt with.
If this is not the case it is now your responsibility to reopen the
Bug report if necessary, and/or fix the problem forthwith.

(NB: If you are a system administrator and have no idea what this
message is talking about, this may indicate a serious mail system
misconfiguration somewhere. Please contact [email protected]
immediately.)


-- 
569728: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=569728
Debian Bug Tracking System
Contact [email protected] with problems
--- Begin Message ---
Package: libmagic-dev
Version: 5.04-1
Severity: normal


The following program does a magic_file() call and then strncmp compares
the result. When the result string is copied, this works fine, but when 
the resulting 'const char*' is used directly, the strncmp gives incorrect
results.

I'm not sure whether this is a bug, that some documentation is missing
on the storage persistence of the result string, or maybe I overlooked 
something.

Jaap Eldering

-- System Information:
Debian Release: squeeze/sid
  APT prefers testing
  APT policy: (500, 'testing')
Architecture: i386 (x86_64)

Kernel: Linux 2.6.32-trunk-amd64 (SMP w/3 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash

Versions of packages libmagic-dev depends on:
ii  libmagic1               5.04-1           File type determination library us
ii  zlib1g-dev              1:1.2.3.4.dfsg-3 compression library - development

libmagic-dev recommends no packages.

Versions of packages libmagic-dev suggests:
ii  file                          5.04-1     Determines file type using "magic"

-- no debconf information
#include <stdio.h>
#include <string.h>
#include <magic.h>

int file_istext(char *filename);

int main(int argc, char **argv)
{
	char *filename;

	if ( argc!=2 ) {
		printf("require a filename argument\n");
		return 1;
	}
	filename = argv[1];

	if ( !file_istext(filename) ) {
		printf("file is detected as binary/data\n");
	} else {
		printf("file is detected as text\n");
	}

	return 0;
}

int file_istext(char *filename)
{
	magic_t cookie;
	const char *filetype;
	char *str;
	int res;

	if ( (cookie = magic_open(MAGIC_MIME))==NULL ) goto error;

	if ( magic_load(cookie,NULL)!=0 ) goto error;

	if ( (filetype = magic_file(cookie,filename))==NULL ) goto error;

	// For some reason, strncmp'ing the original return value gives
	// wrong results; making a local copy works.
//	str = strdup(filetype);
	str = filetype;

	printf("mime-type is '%s'\n",str);

	magic_close(cookie);

	res = (strncmp(str,"text/",5)==0);
//	free(str);
	return res;

error:
	printf("error %d: %s\n",magic_errno(cookie),magic_error(cookie));

	return 1; // return 'text' by default on error
}

--- End Message ---
--- Begin Message ---
On 02/17/2010 04:39 PM, Jaap Eldering wrote:
I found that this was not a bug, but a resource allocation problem: I
close the magic cookie before doing the string compare, hence the
allocated description string is not valid anymore.

no problem, closing then.

--
Address:        Daniel Baumann, Burgunderstrasse 3, CH-4562 Biberist
Email:          [email protected]
Internet:       http://people.panthera-systems.net/~daniel-baumann/


--- End Message ---

Reply via email to