ImageResizer: System.BadImageFormatException is causing Web app crash despite being in try ... catch - png

A user uploaded a PNG file which looks as if it is corrupted in some way. But the biggest problem is that any time our system tries to thumbnail it the entire app pool crashes and restarts. I've tried it using a direct URL and using code inside a try ... catch block.
Here's a copy of the image: https://drive.google.com/file/d/1GpmnIbia3rtqfKrhpg_nKHpjxdC-AxR3
If I load it into Paint.NET and save it out again it works fine so I'm content there is an issue with the image. But I need to stop any corrupt images from crashing my web app entirely. I'm happy to handle and log the errors, but I need everything to continue as normal.
For instance, running locally as a test, I can view the image directly without issue. The browser doesn't mind:
http://localhost:60148/Content/artwork/2019/06/img_0021-9.png
But if I try a thumbnailed version:
http://localhost:60148/Content/artwork/2019/06/img_0021-9.png?w=160&mode=max&otf=y&quality=90&format=jpg&bgcolor=white
Visual Studio will report the following and stop debugging:
System.BadImageFormatException: 'An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)'
I have some legacy code that will also write the thumbnail as a response stream. Even though that's in a try catch VS still stops debugging after reporting the error. I have local and global error handling so I wouldn't expect a crash.
try
{
ImageBuilder.Current.Build(pathname.StartsWith("/") ? "~" + pathname : pathname, outputStream,
new ResizeSettings(resizeSettings));
}
catch (Exception ex)
{
Console.WriteLine("Could not output to stream: " + ex.Message);
}
On this method I've tried adding the following attributes without any success:
[HandleProcessCorruptedStateExceptions]
and
[SecurityCritical]
I've tried putting it in its own Task in the hope it would localise it, but no joy. It's moot anyway. We use URLs for ImageResizer almost exclusively, and that's killing our web pool.
It's not just locally. It was noticed because we run this as a beta Web site and it was killing it.

System.BadImageFormatException never refers to images. It is an exception exclusively for .dll and .exe files, and refers to using the wrong bitness (like 64 vs 32) of file.
Something else is going on here. ImageResizer also is not known for killing the app pool. What plugins do you have installed? Posting the diagnostics page (/resizer.debug) would help.

Related

File download via LWP returns error 500 (probably timeout due to long waiting time)

I'm pretty new to Perl and just tried to use a simple and small script to download a file. It works on most websites, but it seems like it is not working on the one particular website I need to download a file from.
My code is:
use LWP::Simple;
my $status = getstore("http://www.regelleistung.net/download/ABGERUFENE_MRL_BETR_SOLL-WERTE.CSV", "file.csv");
if ( is_success($status) )
{
print "file downloaded correctly\n";
}
else
{
print "error downloading file: $status\n";
}
I always keep getting error status 500. The file is directly linked on
https://www.regelleistung.net/ext/data/ where you can click on "MRL", "SRL" and "RZ_SALDO".
Also, if I try to download the file via clicking the link in my browser it takes like forever to load before the actual download starts.
I feel like I need getstore() to wait until either it timeouts (say ~60 seconds) or the file is loaded.
Do you have any hint that could help me solve this problem? Using some other library or method? Even keyworks might be helpful, since I actually don't know what I could search for on Google.
Your code ran successfully the first time I tried it. I suspect that the site may have been busy when you first tested
To make the kind of changes that you are asking about you need the full LWP::UserAgent module, but I think your code should work for you if you keep trying a few times

PhoneGap - Writing to the iCloud directory?

My app is telling me that my iCloud ubiquity container is here:
file://localhost/private/var/mobile/Library/Mobile%20Documents/.../
When I try to get the directory with fileSystem.root.getDirectory, I error and don't get a DirectoryEntry. I tried window.resolveLocalFileSystemURI, but that also ends with error.
How do I get an entry for this directory, so that I can write to it?
Use a javascript bridge. iCloud has so much setup and error handling that it's not really possible to handle this entirely in javascript without a complex wrapper of some kind.

sqlite database disk image malformed on iPhone SDK

I'm having an issue with a new application on the iPhone SDK using SQLite as the DB backend.
Occasionally, my app will stop loading data to my UITableViews and after downloading the device DB via the Organizer I can access the SQLite DB via the command line. I can query certain tables fine but not others without getting an "SQL error: database disk image is malformed" error. See a sqlite session below:
SQLite version 3.6.17
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from user;
1|cpjolicoeur#gmail.com|cpjolicoeur||4d055e38bb1d3758|image/gif|cartoonme_avatar.gif||Craig|Jolicoeur|1|1
sqlite> select * from item;
SQL error: database disk image is malformed
sqlite>
In this example my user table works fine but my item table is malformed, which corresponds with what I am seeing in my app where the items dont load. The app doesnt crash, the data just doesnt load because of this malformed error.
Any ideas why this is happening? My only thought is that maybe the DB is being corrupted because I am writing to the SQLite DB via a background thread within the app. I download data from a webserver via an NSOperationQueue in a background thread and update the SQLite DB with the data downloaded. Would writing to the DB in a background thread (while potentially reading from the main thread) corrupt the DB, or is it something else?
You have to be very careful about background threads accessing the database while debugging! This is because when the debugger halts processing (such as at a breakpoint) all threads are paused, including threads that may be in the middle of a database call, somewhere in between a database "open" and a database "close" call.
If you are halted at a breakpoint, and click the stop sign in Xcode, your app exits immediately. This very often causes errors such as the one you saw, or the "corrupted database" error.
There really isn't any solution (because there is no way to modify the behavior of "stop tasks", but I have evolved some techniques to mitigate it:
1. Add code to detect the app entering the background and have your db operations gracefully stop.
2. Never use the stop sign to halt processing while debugging. Instead, when done with a breakpoint then "continue", hit the home button on the simulator or device (which should trigger the code you added in step 1), wait for the app to background, THEN you can stop the run.
In my case this had to do with iOS 7: On iOS 7 Core Data now uses the SQLite WAL journaling mode which writes data to a .db-wal file instead of directly to the .db file. In my app, I would copy a prepared .db file into Library/Application Support during an app update. The problem was that an old .db-wal file was still in that directory and I only replaced the .db file. That way, I ended up with a .db file that was out of sync with the old .db-wal file.
There are two solutions to this problem:
Make sure the .db, .db-wal and .db-shm files are deleted before you copy your new .db file into place.
Go back to the old pre-iOS 7 behavior like so: https://stackoverflow.com/a/18870738/171933
Depends on how SQLite is compiled, it may or may not be thread-safe. If you're using the built-in one, it may not have the compile-time options you're looking for.
For our app, we had to roll our own SQLite to add full text search. Take a look at this page.

iPhone application cache and XMLHttpRequest

I have a WebApp that I've been try to make work offline. The WebApp is too big, even minified, to simply use the application cache (things download but I eventually get a window.applicationCache error). I'm trying to use XMLHttpRequest to get the larger scripts and main html and keep them in localStorage and just keep a small loader script in the application cache. The problem I'm seeing is that the XMLHttpRequest returns a network error when the loader script is being served locally. When the the cache is downloading no error is returned and it works fine. When I turn off the application cache the loader works fine, but of course then I need the network to get the loader.
I tried setRequestHeader("Cache-Control", "no-cache") but that didn't help.
Anybody have a clue?
What does your network: section in your manifest look like?
I found that if I weren't allowing wildcard network traffic it wouldn't load with XMLHttpRequest. So changing it to:
Network:
*
did the trick for us.
I think I found a solution. It would probably work for others.
I split the loader into two separate HTML files: one that uses XMLHttpRequest to get all the required files and put them in localStorage (the loader) and another that simply reads the files from localStorage and writes them into the document (the booter) with appropriate wrappers (e.g. ). The booter has a manifest file to keep it in the application cache. The loader does not. The user first invokes the booter. If the booter finds files already in localStorage it does it's thing. Otherwise, it uses location.replace() to invoke the loader. The loader loads the files from the server using XMLHttpRequest and puts them in localStorage, and then re-invokes the booter using location.replace(). This seems to not cause an network error.
In order to run offline, the user must invoke the booter in the iPhone Safari browser (which invokes the loader, which re-ivokes the booter) which boots the WebApp. In Safari, the user must then add the WebApp (the booter link) to their Home Screen (using the "+" button at the bottom). When offline the user can get to the app from the Home Screen icon. It takes a few seconds to re-render, but it's fully functional after that. It's the same delay when online. Invoking the link from the iPhone Safari browser will not work offline, though it will work online.
The booter monitors the application cache's "updateready" event so that when online and the when iPhone detects a change in the booter's manifest file and downloads a new booter, it will swap the new cache (window.applicationCache.swapCache()) and invoke the loader using location.replace() again. I also add an alert() to let the user know something funky is going on. So changing the manifest file (I mean making some bytes different, not just tweaking the modify time) will cause clients to get new files when online.
Interestingly, I noticed that localStorage set up in Safari is not available to the same page served from invoking the Home Screen icon, even though the cookies transfer! So the first time the booter is invoked from the icon it will reload the files even though they were previously loaded in Safari. Also, I had to explicitly prevent the loader from being cached as it was not reloading from the server when the rest of the files were updated.
You are correct. Ultimately it was the network section in the manifest.
I thought the site where the application was loaded from was included automatically and you didn't need to mess with it, but it's not true. You need to put the site in the network section.

Frame load interrupted error while loading a word document in UIWebView

I want to load a word document using UIWebView. I used the code provided in
http://developer.apple.com/iphone/library/qa/qa2008/qa1630.html
to load the document.
But not all the documents load successfully. Sometimes I get an error
Error Domain=WebKitErrorDomain Code=102 UserInfo=0x145bc10 "Frame load interrupted"
The error seems to be very sporadic and I get this error only for some documents.
However the same document loads correctly in mail.app.
What am I missing?
The reason was, the file I tried to load doesn't contain a extension.
I was purposely removing the file extension (.doc and .ppt) which I think confused the UIWebView. Seems like iPhone ignores the OS 9 legacy typecodes completely. :)
I submitted a Radar regarding this issue. It's resolved in the latest version of iPhone OS 4.0.
http://openradar.appspot.com/radar?id=381401