> According to the documentation on PrefGetAppPreferences:
> The function returns the size of the actual preferences record if it
exists
> or noPreferenceFound (#defined as -1) if it does not.

it says if the preference is not found, then noPreferenceFound is returned.
it does NOT say what is returned if the preference IS found.  when the
preference IS found, then the perf version is what gets returned.  (this has
been asked zillions of times on the newsgroups and this list, and can be
verified by reading old answers, looking at the OS source, looking at the
sample apps, or empirical testing).  in one place, the docs talk about the
"prefSize" out parameter, and say to compare the return value.  they mean
the return value of the out parameter prefSize, not the return value of the
function.  (out parameters are a pretty universal concept, and if one has
experience with them, i think one doesn't even notice that the docs are
sketchy there).


> The prefsSize parameter is a pointer to the size of your buffer and is
> necessary to prevent the function overflowing the buffer.
> You need to check that the returned value is not greater than *prefSize
> because, if it was, it would mean that the whole record could not be
> returned because your buffer was too small.

close.  you need to check that after returning, *prefSize is less than or
equal to what it was BEFORE you called the API.  prefSize is an "out"
parameter, which means it gets modified by the API.


> While using it this way seems to work OK (with a fairly simple preferences
> structure), I agree that the documentation is odd.  The version parameter
in

ack.  you've just been lucky.


> Mike Davis wrote:
> > 1) I think PrefGetAppPreferences returns version.

that is correct.


> > 2) is prefsSize a pointer that is used by PrefGetAppPreferences to
> > know how much to retrieve or is it a pointer where
> > PrefGetAppPreferences puts the retrieved size.

both.


> > 3) Then the docs say to check prefsSize against the returned value.
> > That's apples and oranges thing.

the docs say "If the preference resource was found, the application should
check that the value in prefsSize is equal or less than the return value. If
it's greater than the size passed, then some bytes were not retrieved."
when it says "return value", it means the value returned in the out
parameter prefSize.  (i'm not defending the docs, just explaining them).


> Can someone explain this function and the checking necessary.  If I
> have a Prefs structure that is, say 1000 bytes and my unsaved
> Preferences (the acutal data previously saved) is only 500 bytes (as
> in you changed the struct size and already had unsaved prefs db), do
> I have to check this?

it's not very safe, but yes you can do that.  it is more robust to do the
check, though.


Reply via email to