Is there an elixir guard clause for iodata - erlang

I have a function that can take any io_data and nothing else, because it's going to send it over a http connection. At the moment I have an is_binary guard clause but this is too restrictive.
So is there a guard clause that checks if something is io_data or not?
e.g.
def do_the_stuff(content) when is_iodata(content) do
# e.g.
IO.puts(content)
end

There's no specific guard for that. Usually libraries use
when is_binary(iodata) or is_list(iodata)

There is no way to catch invalid iodata terms in a guard. is_binary(content) or is_list(content) is as close as you get.
If you want to check that the term is valid iodata before sending it, you can call IO.iodata_length, and check that it doesn't raise an error. This is cheaper than converting the iodata to a binary, but does impose a cost that wasn't there before. It might not be worth it, considering that the underlying library should raise an error if the data is invalid.

Related

Most efficient way to handle failure retry with multiple possibilities in Objective-C

I am adding code to my iOS app to deal with errors when it interact with a server. The error handling varies based on type of failure.
For e.g: The error could be receipt of a non 200:OK response. In which case, I want to use the response code to determine the retry algorithm
Or the error could be a timeout
Or the error could be based on the value of a field in the JSON body returned from the server.
Also, the error handling would be different depending on whether the failure occurred during registration or during data transfer etc.
I am thinking of having a generic function to avoid repeating code in several places
Basically it will take two arguments
1. A failure code that identifies sub-type of error and
2. A JSON body whose content will depend on the failure code argument
Is this approach ok or is it better split it into individual functions?
The approach with one function is OK, but as you said, sometimes you will have message as body from JSON response, sometimes you will have error as timeout, sometimes as key from the 200 OK response.. It means you need a parser function which will decide what to pass as the error message, because I'm afraid that you will end up with huge function doing everything at once. I would:
a) Split this function to parser & error show
or
b) Do the parsing in the network handler, but it might copy the code across the application.

RtlCopyMemory and exception handling

Im reading in an address from user-space and using RtlCopyMemory() to copy data over to my output buffer and although I have it working, it lacks any safe guarding against invalid addresses being read which will throw a bug check in my face.
I know that I need to use SEH to do this but I am not sure what exceptions I need to be handling or if RtlCopyMemory even throws any exceptions. If not, how can I check that the address is valid before I pass it into RtlCopyMemory()?
Read about ProbeForRead and ProbeForWrite, additionally I suggest you the following reading:
http://download.microsoft.com/download/d/1/d/d1dd7745-426b-4cc3-a269-abbbe427c0ef/sys-t774_ddc08.pptx
and
http://www.codeproject.com/Articles/9575/Driver-Development-Part-Introduction-to-Implemen

How to get the string that caused parse error?

Suppose I have this code:
(handler-case (read ...)
(parse-error (condition)
(format t "What text was I reading last to get this error? ~s~&"
(how-to-get-this-text? condition))))
I can only see the parse-namestring accessors, but it gives the message of the error, not the text it was parsing.
EDIT
In my case the problem is less generic, so an alternative solution not involving the entire string that failed to parse can be good too.
Imagine this example code I'm trying to parse:
prefix(perhaps (nested (symbolic)) expressions))suffix
In some cases I need to stop on "suffix" and in others, I need to continue, the suffix itself has no other meaning but just being an indicator of the action the parser should take next.
READ parses from a stream, not a string. The s-expression can be arbitrarily long. Should READ keep a string of what's been read?
What you might need is a special stream. In standard Common Lisp there is no mechanism for user defined streams. But in real life every implementation has such extensible streams. See for example 'gray streams'.
http://www.sbcl.org/1.0/manual/Gray-Streams.html
There's no standard function to do it. You might be able to brute-force something with read-from-string, but whatever you do, it will require some extra work.

passing regular expressions as params into ruby

so i would like to expose regular expression queries on a field in my model, such that user could ask for
http://localhost:3000/myview.json?field=^hello, (there|world).*
so i know i'll have to change my routes to recognise the wildcard characters etc, and i can easily do a Regexp.new() inside my controller to convert this to a real regular expression (i'm using mongomapper in the back).
the issue is the potentially huge security hole with XSS.
should i be worried about this? how could i safely enable users to query with regular expression strings.
(i'm not too bothered about the user hammering the database... yet)
Regular expressions won't be able to perform arbitrary code execution unless there is something really wrong with Regexp.new. So if we assume that Regexp.new will either make a valid regular expression or fail or do something else sane you are safe already without having to sanitize the incoming string.

NS_ERROR_FAILURE with absent property

I trying to capture pageLoad event in progressListener, in onLocationChange i check URI of fresh page, and in case URI = about:blank i got
Error: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIURI.host]
As i understand this URI don't have host at all.
How can i check that host is available without raw parsing and exception catching? Is there any conventional way to check that atribute is present?
Your best bet is to filter certain protocols (filter on scheme). It's best to use a whitelist in this case of the protocols you care about.
In general there's no way to know for sure without doing a try/catch, but currently an nsIStandardURL is the only sort of URI that will have a host.
When I faced this problem my solution was:
To indicate an observer flag aWebProgress.NOTIFY_LOCATION
aWebProgress.addProgressListener(this.listener, aWebProgress.NOTIFY_LOCATION);
Before I had aWebProgress.NOTIFY_LOCATION and there was an exception you desc

Resources