When a file path longer than 100 characters is passed, crontab throws an error saying "No such file or directory"

$ touch aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaBBB
$ crontab aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaBBB
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: No such file or directory

This behavior seems quite unusual (noticing how it also truncates the path in the error message). I’m using Debian bullseye 11.

Is this a bug, or is there a specific reason why crontab has such a peculiar limitation?

I’m not able to replicate it on the docker image here: https://hub.docker.com/r/willfarrell/crontab

The version of crontab from Cygwin prints an explanatory error message:

file=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcd
touch "$file"
crontab "$file"
crontab: usage error: filename too long

echo "$file" | awk '{print length}'
108

The message addresses your concern, albeit without providing an explanation.

Unfortunately the version on Debian doesn’t explain well:

crontab "$file"
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU: No such file or directory

echo 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU' | awk '{print length}'
99

However, the source code (apt-get source crontab) gives a massive clue in cron.h:

#define  MAX_FNAME       100     /* max length of internally generated fn */

And then in crontab.c:

static  char            Filename[MAX_FNAME];
…
(void) strncpy (Filename, argv[optind], (sizeof Filename)-1);
Filename[(sizeof Filename)-1] = '';

In case it’s not obvious from these snippets, there’s a hard-coded 99 character limit on the length of the filename. I can’t see a reason for this other than an arbitrary "that should be long enough". The proper approach would probably have been to use PATH_MAX+1, but the author(s) didn’t do that. A comment notes that the code could have been written as early as 1988 (or as late as 1994), but quite possibly pre-POSIX where this constant was formalised.

Answered By: roaima
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.