View Issue Details

IDProjectCategoryView StatusLast Update
0000693fileGeneralpublic2025-11-09 08:44
Reporterburlog Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Summary0000693: The ICS file is incorrectly reported as text/plain instead of text/calendar
DescriptionThe recent commit https://github.com/file/file/commit/08ab5f644bf9822c64faf5efa4e393b58d7bcd0e has introduced a regression where the attached ICS file is incorrectly identified as text/plain instead of text/calendar.

The issue stems from the addition of a conditional check around the offset update:

-                       ms->offset = ms->eoffset + offset;
+                       if (b->fd != -1)
+                               ms->offset = ms->eoffset + offset;

Commenting out the newly added condition restores the correct MIME type detection.
Steps To Reproduce╱ ~/git/github.com ❱ git clone git@github.com:file/file.git
Cloning into 'file'...
remote: Enumerating objects: 37190, done.
remote: Counting objects: 100% (4412/4412), done.
remote: Compressing objects: 100% (359/359), done.
remote: Total 37190 (delta 4259), reused 4056 (delta 4053), pack-reused 32778 (from 4)
Receiving objects: 100% (37190/37190), 11.59 MiB | 1.41 MiB/s, done.
Resolving deltas: 100% (26962/26962), done.
╱ ~/git/github.com ❱ cd file
╱ ~/git/github.com/file ❱ autoreconf --install --force
...
╱ ~/git/github.com/file ❱ mkdir build
╱ ~/git/github.com/file ❱ cd build
╱ git/github.com/file/build ❱ ../configure --prefix=/usr
...
╱ git/github.com/file/build ❱ make -j10
...
╱ git/github.com/file/build ❱ LD_LIBRARY_PATH=./src/.libs gcc /tmp/m.c -o m -lmagic
╱ git/github.com/file/build ❱ LD_LIBRARY_PATH=./src/.libs ./m /tmp/i.ics
MIME Type of '/tmp/i.ics': text/plain

╱ git/github.com/file/build ❱ git revert 08ab5f644bf9822c64faf5efa4e393b58d7bcd0e --strategy-option=theirs --no-edit
Auto-merging src/softmagic.c
[master 3a600386] Revert "PR/622: Odd_Bloke: Handle negative offsets in file_buffer(), when fd is not"
 Date: Sun Nov 9 09:12:35 2025 +0100
 2 files changed, 7 insertions(+), 18 deletions(-)
╱ git/github.com/file/build ❱ make -j10
...
╱ git/github.com/file/build ❱ LD_LIBRARY_PATH=./src/.libs gcc /tmp/m.c -o m -lmagic
╱ git/github.com/file/build ❱ LD_LIBRARY_PATH=./src/.libs ./m /tmp/i.ics
MIME Type of '/tmp/i.ics': text/calendar
╱ git/github.com/file/build ❱
TagsNo tags attached.

Activities

burlog

2025-11-09 08:25

reporter  

i.ics (882 bytes)   
BEGIN:VCALENDAR
PRODID:-//COVIDPass//www.covidpass.cz//EN
VERSION:2.0
METHOD:PUBLISH
BEGIN:VTIMEZONE
TZID:Central Europe Standard Time
BEGIN:STANDARD
DTSTART:16011028T030000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010325T020000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
UID:4a55be4a-c27b-4482-a304-98522a71f34d
CLASS:PUBLIC
DESCRIPTION:\n
DTSTART;TZID="Central Europe Standard Time":20211211T080000
DTEND;TZID="Central Europe Standard Time":20211211T081500
LOCATION:COVIDPASS - Budějovická 303\, Jesenice\, 252 42
PRIORITY:5
SEQUENCE:0
SUMMARY;LANGUAGE=cs:COVIDPass - Test na COVID
TRANSP:OPAQUE
BEGIN:VALARM
TRIGGER:-PT60M
ACTION:DISPLAY
DESCRIPTION:Reminder
END:VALARM
END:VEVENT
END:VCALENDAR
i.ics (882 bytes)   
m.c (1,663 bytes)   
#include <stdio.h>
#include <stdlib.h>
#include <magic.h>

#define READ_SIZE 4096 // We'll read the first 4KB of the file

int main(int argc, char *argv[]) {
    // 1. Check for command-line argument
    if (argc != 2) {
        fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
        return 1;
    }
    const char *filename = argv[1];

    // 2. Open the file and read into a buffer
    FILE *fp = fopen(filename, "rb");
    if (fp == NULL) {
        perror("Error opening file");
        return 1;
    }

    char buffer[READ_SIZE];
    size_t bytes_read = fread(buffer, 1, READ_SIZE, fp);
    fclose(fp);

    if (bytes_read == 0) {
        fprintf(stderr, "File is empty or could not be read.\n");
        return 1;
    }

    // 3. Initialize libmagic
    // MAGIC_MIME_TYPE tells libmagic to return the MIME type string
    magic_t magic_cookie = magic_open(MAGIC_MIME_TYPE);
    if (magic_cookie == NULL) {
        fprintf(stderr, "Error initializing libmagic\n");
        return 1;
    }

    // Load the default magic database files
    if (magic_load(magic_cookie, NULL) != 0) {
        fprintf(stderr, "Error loading magic database: %s\n", magic_error(magic_cookie));
        magic_close(magic_cookie);
        return 1;
    }

    // 4. Determine MIME type from the buffer
    const char *mime_type = magic_buffer(magic_cookie, buffer, bytes_read);

    // 5. Print the result
    if (mime_type == NULL) {
        fprintf(stderr, "Error identifying file type: %s\n", magic_error(magic_cookie));
    } else {
        printf("MIME Type of '%s': %s\n", filename, mime_type);
    }

    // 6. Clean up
    magic_close(magic_cookie);

    return 0;
}
m.c (1,663 bytes)   

burlog

2025-11-09 08:33

reporter   ~0004311

Related issue: https://bugs.astron.com/view.php?id=622

burlog

2025-11-09 08:44

reporter   ~0004312

This patch would fix the issue:

╱ git/github.com/file/build ❱ git diff
diff --git a/src/softmagic.c b/src/softmagic.c
index 54ebcde3..bdcd93ca 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -1580,8 +1580,10 @@ normal:
                        ms->offset = offset;
                        ms->eoffset = 0;
                } else {
- if (b->fd != -1)
- ms->offset = ms->eoffset + offset;
+ if (b->fd == -1)
+ ms->offset = offset;
+ else
+ ms->offset = ms->eoffset + offset;
                }
        }
        if ((ms->flags & MAGIC_DEBUG) != 0) {
fix.diff (460 bytes)   
diff --git a/src/softmagic.c b/src/softmagic.c
index 54ebcde3..bdcd93ca 100644
--- a/src/softmagic.c
+++ b/src/softmagic.c
@@ -1580,8 +1580,10 @@ normal:
 			ms->offset = offset;
 			ms->eoffset = 0;
 		} else {
-			if (b->fd != -1)
-				ms->offset = ms->eoffset + offset;
+            if (b->fd == -1)
+                ms->offset = offset;
+            else
+                ms->offset = ms->eoffset + offset;
 		}
 	}
 	if ((ms->flags & MAGIC_DEBUG) != 0) {
fix.diff (460 bytes)   

Issue History

Date Modified Username Field Change
2025-11-09 08:25 burlog New Issue
2025-11-09 08:25 burlog File Added: i.ics
2025-11-09 08:25 burlog File Added: m.c
2025-11-09 08:33 burlog Note Added: 0004311
2025-11-09 08:44 burlog Note Added: 0004312
2025-11-09 08:44 burlog File Added: fix.diff