Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/getdef.c: Introduce getlong_normalized #884

Closed

Conversation

stoeckmann
Copy link
Contributor

@stoeckmann stoeckmann commented Dec 27, 2023

Stay compatible with current configuration files which allow all negative values, but convert them to -1 on the fly.

Print a warning so these values can be modified by an administrator for a future release which won't have this support anymore.

As discussed in #877 (comment)

Stay compatible with current configuration files which allow all
negative values, but convert them to -1 on the fly.

Print a warning so these values can be modified by an administrator
for a future release which won't have this support anymore.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
@alejandro-colomar
Copy link
Collaborator

alejandro-colomar commented Dec 29, 2023

"normalizing" is not something intrinsic to long (what is normal for a long?). I'm not convinced by this function. Rejecting negative values other than -1 is more appropriate to do in functions that handle specific values. I don't like this change very much.

@alejandro-colomar
Copy link
Collaborator

alejandro-colomar commented Dec 29, 2023

A getlong() variant that handles a range (similar to strtoi(3bsd)) is something I have in mind.

Here's what I have in mind:

inline int
getlong(const char *s, long *restrict num, char **endptr, int base, long min, long max)
{
	int  status;

	*num = strtoi(s, endptr, base, min, max, &status);
	if (status != 0) {
		errno = status;
		return -1;
	}

	return 0;
}

inline int
getl(const char *restrict s, long *restrict num)
{
	return getlong(s, num, NULL, 0, LONG_MIN, LONG_MAX);
}

The current getlong() would be (almost) my getl() suggestion, and getlong() would be the argumentful version.

You could call getlong(..., -1, LONG_MAX) for rejecting negative values except -1, and then the caller could report any errors or do whatever is more appropriate.

@stoeckmann stoeckmann closed this Dec 30, 2023
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 6, 2024
This is a variant of get[u]l() that accepts a few more parameters.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 6, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 6, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 7, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 7, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 7, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 7, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 8, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 8, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 8, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 8, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 11, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 11, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 11, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 16, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
alejandro-colomar added a commit to alejandro-colomar/shadow that referenced this pull request Jan 16, 2024
…terms of get[u]long()

This is a variant of get[u]l() that accepts a few more parameters.

get[u]l() is rewritten to be implemented in terms of get[u]long().
There are a few side effects of this in the behavior of get[u]l():

-  get[u]l() now sets errno in a similar way to how strtoi(3bsd) sets
   &status.  This replaces the old behavior, which was strtol(3)-like.
   The strtoi(3bsd) errors are much easier to differentiate.

-  get[u]l() now sets the number, even if the call failed.  The old
   behavior was to leave it uninitialized.

Link: <shadow-maint#884>
Cc: Tobias Stoeckmann <tobias@stoeckmann.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
@alejandro-colomar
Copy link
Collaborator

This PR is superseeded by #899.

The commits are:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants