From: Nathan Lynch <ntl-e+AXbWqSrlAAvxtiuMwx3w@public.gmane.org>
To: Dan Smith <danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
Cc: containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org
Subject: Re: [PATCH] Add a ckpt_read_string() function (v3)
Date: Tue, 04 Aug 2009 16:56:34 -0500 [thread overview]
Message-ID: <m3eirr450t.fsf@pobox.com> (raw)
In-Reply-To: <1249418420-807-1-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org> (Dan Smith's message of "Tue\, 4 Aug 2009 13\:40\:20 -0700")
Hi Dan,
Dan Smith <danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org> writes:
> +/**
> + * ckpt_read_string - read a string (variable length)
> + * @ctx: checkpoint context
> + * @str: pointer to buffer to store allocated string (caller must kfree())
> + * @max: maximum acceptable length
> + *
> + * This can be used to read a variable-length string from the checkpoint
> + * stream. @max limits the size of the resulting buffer. Returns zero on
> + * success, negative on failure.
> + */
> +int ckpt_read_string(struct ckpt_ctx *ctx, char **str, int max)
> +{
> + int len;
> + int ret = 0;
> +
> + *str = NULL;
> +
> + len = _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
> + if (len < 0)
> + return len;
> + else if (len > max)
> + return -EINVAL;
> +
> + *str = kzalloc(len + 1, GFP_KERNEL);
> + if (!*str)
> + return -ENOMEM;
> +
> + ret = ckpt_kread(ctx, *str, len);
> + if (ret < 0) {
> + kfree(*str);
> + *str = NULL;
> + }
> +
> + return ret;
> +}
Maybe I'm missing the utility this provides, but I think this helper is
trying to do too much. It's often more straightforward to allow (or
force, depending on your POV) the caller to handle the allocation if
it's going to have to free it anyway. It also allows the caller to
choose its own allocation method, e.g. on-stack for small things, or
maybe some filesystem-related code will need to use GFP_NOFS.
So I imagine a simple wrapper to probe the length of the string would
suffice? Something like
ssize_t ckpt_next_string_length(struct ckpt_ctx *ctx)
{
return _ckpt_read_obj_type(ctx, NULL, 0, CKPT_HDR_STRING);
}
...
len = ckpt_next_string_length(ctx);
if (len < 1 || len > max)
return -EINVAL;
buf = kzalloc(len, GFP_KERNEL);
if (!buf)
return -ENOMEM;
rc = ckpt_kread(ctx, buf, len);
if (rc < 0) {
kfree(buf)
return -EINVAL;
}
kfree(buf);
next prev parent reply other threads:[~2009-08-04 21:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-08-04 20:40 [PATCH] Add a ckpt_read_string() function (v3) Dan Smith
[not found] ` <1249418420-807-1-git-send-email-danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2009-08-04 20:58 ` Serge E. Hallyn
2009-08-04 21:56 ` Nathan Lynch [this message]
[not found] ` <m3eirr450t.fsf-e+AXbWqSrlAAvxtiuMwx3w@public.gmane.org>
2009-08-04 22:04 ` Dan Smith
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m3eirr450t.fsf@pobox.com \
--to=ntl-e+axbwqsrlaavxtiumwx3w@public.gmane.org \
--cc=containers-qjLDD68F18O7TbgM5vRIOg@public.gmane.org \
--cc=danms-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.