[lxc-devel] [PATCH] Validate container name during creation
Robert Vogelgesang
vogel at users.sourceforge.net
Fri Feb 6 07:23:35 UTC 2015
Hi,
if you really want to enforce a certain character set, please note:
On Thu, Feb 05, 2015 at 08:06:14PM +0100, Robert Vogelgesang wrote:
> Hi Serge,
>
> On Thu, Feb 05, 2015 at 05:40:54PM +0000, Serge Hallyn wrote:
> > Quoting Robert Vogelgesang (vogel at users.sourceforge.net):
[snip]
> > > On Thu, Feb 05, 2015 at 05:50:39PM +0200, Joel Nider wrote:
[snip]
> > > > +/* Ensure requested hostname follows RFC 1123
> > > > + * In our case, that means simple host name (not FQDN)
> > > > + * characters in the set {[A-Z], [0-9], '-'} (no '.')
> > > > + * maximum length of 63 characters
> > > > + */
> > > > +static int validate_hostname(struct lxc_container *c)
> > > > +{
> > > > + char *a;
> > > > + int count = 0;
> > > > +
> > > > + if (!c)
> > > > + return MAX_LENGTH_HOSTNAME;
> > > > +
> > > > + a = c->name;
> > > > + while (*a) {
> > > > + count++;
> > > > + if (count > MAX_LENGTH_HOSTNAME)
> > > > + return MAX_LENGTH_HOSTNAME;
> > > > +
> > > > + if (!(isalnum(*a) | (*a == '-')))
> >
> > Would prefer to see this as
> >
> > if (!valid_hostname_char(*a))
> > return count;
> >
> > with valid_hostname_char(const char a) {
> > if (isalnum(a))
> > return true;
> > if (a == '-')
> > return true;
> > return false;
> > }
isalnum() checks according to the current locale. If you want to use
this to check for characters in the set {[A-Z], [0-9]}, then you have
to switch to the C locale before, and back to the original locale
after the check.
Robert
More information about the lxc-devel
mailing list